-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathData.ts
More file actions
658 lines (593 loc) · 16.5 KB
/
Copy pathData.ts
File metadata and controls
658 lines (593 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import { markRaw } from 'vue'
import { MoLang } from 'molang'
import type { languages } from 'monaco-editor'
import { generateCommandSchemas } from '../../Compiler/Worker/Plugins/CustomCommands/generateSchemas'
import { RequiresMatcher } from '../../Data/RequiresMatcher/RequiresMatcher'
import { RefSchema } from '../../JSONSchema/Schema/Ref'
import { strMatchArray } from './strMatch'
import { App } from '/@/App'
import { Signal } from '/@/components/Common/Event/Signal'
import { SelectorArguments } from './TargetSelector/SelectorArguments'
import { useMonaco } from '../../../utils/libs/useMonaco'
import { ResolvedCommandArguments } from './ResolvedCommandArguments'
/**
* An interface that describes a command
*/
export interface ICommand {
commandName: string
description: string
arguments: ICommandArgument[]
}
export interface ISelectorArgument extends ICommandArgument {
additionalData?: {
schemaReference?: string
values?: string[]
multipleInstancesAllowed?: 'always' | 'whenNegated' | 'never'
supportsNegation?: boolean
}
}
/**
* Type holding all possible argument types
*/
export type TArgumentType =
| 'string'
| 'number'
| 'boolean'
| 'selector'
| 'molang'
| 'blockState'
| 'jsonData'
| 'coordinate'
| 'command'
| 'scoreData'
| 'subcommand'
| 'integerRange'
| `$${string}`
/**
* An interface that describes a command argument
*/
export interface ICommandArgument {
argumentName: string
description: string
type: TArgumentType
allowMultiple?: boolean
additionalData?: {
schemaReference?: string
values?: string[]
}
isOptional: boolean
}
export interface ICompletionItem {
label?: string
insertText: string
documentation?: string
kind: languages.CompletionItemKind
insertTextRules?: languages.CompletionItemInsertTextRule
}
/**
* A class that stores data on all Minecraft commands
*/
export class CommandData extends Signal<void> {
public readonly selectorArguments = new SelectorArguments(this)
protected _data?: any
async loadCommandData(packageName: string) {
const app = await App.getApp()
this._data = markRaw(
await app.dataLoader.readJSON(
`data/packages/${packageName}/language/mcfunction/main.json`
)
)
const customTypes: Record<string, ICommandArgument[]> =
this._data.$customTypes ?? {}
for (const { commands = [], subcommands = [] } of this._data.vanilla) {
const allCommands = commands.concat(
subcommands.map((subcommand: any) => subcommand.commands).flat()
)
for (const command of allCommands) {
if (!command.arguments) continue
command.arguments = command.arguments
.map((argument: ICommandArgument) => {
if (!argument.type || !argument.type.startsWith('$'))
return argument
const replaceWith = customTypes[argument.type.slice(1)]
if (!replaceWith) return argument
return replaceWith.map((replaceArgument) => ({
...argument,
...replaceArgument,
}))
})
.flat()
}
}
this.dispatch()
}
get shouldIgnoreCustomCommands() {
return App.getApp().then(
(app) => !app.projectManager.projectReady.hasFired
)
}
protected async getSchema() {
if (!this._data)
throw new Error(`Acessing commandData before it was loaded.`)
const validEntries: any[] = []
const requiresMatcher = new RequiresMatcher()
await requiresMatcher.setup()
for await (const entry of this._data.vanilla) {
if (requiresMatcher.isValid(entry.requires))
validEntries.push(entry)
else if (!entry.requires) validEntries.push(entry)
}
return validEntries
}
protected async getCommandsSchema(
ignoreCustomCommands = false
): Promise<ICommand[]> {
return (await this.getSchema())
.map((entry: any) => entry.commands)
.flat()
.concat(ignoreCustomCommands ? [] : await generateCommandSchemas())
.filter((command: unknown) => command !== undefined)
}
async getSelectorArgumentsSchema(): Promise<ISelectorArgument[]> {
return (await this.getSchema())
.map((entry: any) => entry.selectorArguments)
.flat()
.filter(
(selectorArgument: unknown) => selectorArgument !== undefined
)
}
async getSubcommands(commandName: string): Promise<ICommand[]> {
const schemas = await this.getSchema()
return schemas
.map(
(schema) =>
schema.subcommands?.filter(
(subcommand: any) =>
subcommand.commandName === commandName
) ?? []
)
.flat(1)
.map((subcommands) => subcommands.commands)
.flat(1)
}
allCommands(query?: string, ignoreCustomCommands = false) {
return this.getCommandsSchema(ignoreCustomCommands).then((schema) => [
...new Set<string>(
schema
.map((command: any) => command?.commandName)
.filter(
(commandName: string) =>
!query || commandName?.includes(query)
)
),
])
}
allSelectorArguments() {
return this.getSelectorArgumentsSchema().then((schema) => [
...new Set<string>(
schema.map(
(selectorArgument: any) => selectorArgument?.argumentName
)
),
])
}
async getCommandCompletionItems(
query?: string,
ignoreCustomCommands = false
) {
const { languages } = await useMonaco()
const schema = await this.getCommandsSchema(ignoreCustomCommands)
const completionItems: ICompletionItem[] = []
schema
.filter(
(command: ICommand) =>
!query || command.commandName?.includes(query)
)
.forEach((command) => {
if (
completionItems.some(
(item) => item.label === command.commandName
)
)
return
completionItems.push({
insertText: command.commandName,
label: command.commandName,
documentation: command.description,
kind: languages.CompletionItemKind.Method,
})
})
return completionItems
}
/**
* Given a commandName, return all matching command definitions
*/
async getCommandDefinitions(
commandName: string,
ignoreCustomCommands: boolean
) {
const commands = await this.getCommandsSchema(
ignoreCustomCommands
).then((schema) => {
return schema.filter(
(command: any) => command.commandName === commandName
)
})
return commands
}
async getNextCompletionItems(path: string[]): Promise<ICompletionItem[]> {
if (path.length <= 1)
return await this.getCommandCompletionItems(
path[0],
await this.shouldIgnoreCustomCommands
)
const commandName = path.shift()
const currentCommands = await this.getCommandDefinitions(
commandName!,
await this.shouldIgnoreCustomCommands
)
if (!currentCommands || currentCommands.length === 0) return []
const completionItems: ICompletionItem[] = []
;(
await Promise.all(
currentCommands.map(async (currentCommand: ICommand) => {
// Get command argument
const args = (
await this.getNextCommandArgument(currentCommand, [
...path,
])
).arguments
if (args.length === 0) return []
// Return possible completion items for the argument
return await Promise.all(
args.map((arg) =>
this.getCompletionItemsForArgument(
arg,
currentCommand.commandName
)
)
)
})
)
)
.flat(2)
.forEach((item: ICompletionItem) => {
if (
completionItems.some(
(completionItem) => completionItem.label === item.label
)
)
return
completionItems.push(item)
})
return completionItems
}
/**
* Given a sequence of command arguments & the currentCommand, return possible values for the next argument
*/
protected async getNextCommandArgument(
currentCommand: ICommand,
path: string[]
): Promise<ResolvedCommandArguments> {
if (!currentCommand.arguments || currentCommand.arguments.length === 0)
return new ResolvedCommandArguments([], 0, false)
const args = currentCommand.arguments ?? []
let argumentIndex = 0
let subcommandStopArg = null
let shouldProposeStopArg = false
for (let i = 0; i < path.length; i++) {
const currentStr = path[i]
if (currentStr === '') continue
const matchType = await this.isArgumentType(
currentStr,
args[argumentIndex],
currentCommand.commandName
)
if (matchType === 'none')
return new ResolvedCommandArguments([], i, false)
else if (matchType === 'partial')
return new ResolvedCommandArguments(
path.length === i + 1 ? [args[argumentIndex]] : [],
i
)
// Propose arguments from nested command when necessary
if (args[argumentIndex].type === 'command' && i + 1 < path.length) {
return ResolvedCommandArguments.from(
(
await Promise.all(
(
await this.getCommandDefinitions(
currentStr,
await this.shouldIgnoreCustomCommands
)
).map((command) =>
this.getNextCommandArgument(
command,
path.slice(i + 1)
)
)
)
).flat()
)
} else if (args[argumentIndex].type === 'subcommand') {
const subcommands = await this.getSubcommands(
currentCommand.commandName
)
// Try to find stop argument within path
subcommandStopArg = args[argumentIndex + 1] ?? null
let foundStopArg = false
let stopArgIndex = i
while (
subcommandStopArg &&
!foundStopArg &&
stopArgIndex < path.length
) {
stopArgIndex++
foundStopArg =
(await this.isArgumentType(
path[stopArgIndex],
subcommandStopArg,
currentCommand.commandName
)) === 'full'
}
// Stop argument was entered, skip to next argument after stop argument
if (foundStopArg) {
argumentIndex += 2
i = stopArgIndex
continue
}
const validSubcommands = subcommands.filter(
(subcommand) => subcommand.commandName === path[i]
)
if (validSubcommands.length === 0)
return new ResolvedCommandArguments([], i)
const resolvedArgs = ResolvedCommandArguments.from(
await Promise.all(
validSubcommands.map((validSubcommand) =>
this.getNextCommandArgument(
validSubcommand,
path.slice(i + 1)
)
)
)
)
const nextArgs = resolvedArgs.arguments
// We have no more arguments for the subcommand
if (nextArgs.length === 0) {
// If multiple subcommands are valid, we should propose the next subcommand
if (args[argumentIndex].allowMultiple) {
i += resolvedArgs.lastParsedIndex + 1
shouldProposeStopArg = true
} else {
// If only one subcommand is valid, we should propose the next argument
argumentIndex++
}
continue
}
return resolvedArgs
}
// Check next argument
argumentIndex++
// If argumentIndex to large, return
if (argumentIndex >= args.length)
return new ResolvedCommandArguments([], i)
}
if (!args[argumentIndex])
return new ResolvedCommandArguments([], path.length - 1)
// If we are here, we are at the next argument, return it
return new ResolvedCommandArguments(
[args[argumentIndex]].concat(
subcommandStopArg && shouldProposeStopArg
? [subcommandStopArg]
: []
),
path.length - 1
)
}
/**
* Given an argument type, test whether a string matches the type
*/
async isArgumentType(
testStr: string,
commandArgument: ICommandArgument,
commandName?: string
): Promise<'none' | 'partial' | 'full'> {
switch (commandArgument.type) {
case 'string': {
if (!commandArgument.additionalData?.values) return 'full'
const values =
commandArgument.additionalData?.values ??
this.resolveDynamicReference(
commandArgument.additionalData.schemaReference!
) ??
[]
return strMatchArray(testStr, values)
}
case 'command': {
return strMatchArray(
testStr,
await this.allCommands(
undefined,
await this.shouldIgnoreCustomCommands
)
)
}
case 'number':
return !isNaN(Number(testStr)) ? 'full' : 'none'
case 'selector':
return testStr.startsWith('@') ? 'full' : 'none'
case 'boolean':
return testStr === 'true' || testStr === 'false'
? 'full'
: 'none'
case 'coordinate':
return testStr.startsWith('~') ||
testStr.startsWith('^') ||
!isNaN(Number(testStr))
? 'full'
: 'none'
case 'jsonData':
return /^\{.*\}$/.test(testStr) ? 'full' : 'none'
case 'blockState':
return /^\[.*\]$/.test(testStr) ? 'full' : 'none'
case 'molang': {
const molang = new MoLang()
try {
molang.parse(testStr)
} catch (e) {
return 'none'
}
return 'full'
}
case 'subcommand': {
const subcommands = commandName
? await this.getSubcommands(commandName)
: []
return strMatchArray(
testStr,
subcommands.map((subcommand) => subcommand.commandName)
)
}
case 'integerRange':
return /^([\d]*)?([.]{2})?([\d]*)?$/.test(testStr)
? 'full'
: 'none'
default:
return 'none'
}
}
/**
* Given a commandArgument, return completion items for it
*/
async getCompletionItemsForArgument(
commandArgument: ICommandArgument,
commandName?: string
): Promise<ICompletionItem[]> {
const { languages } = await useMonaco()
// If argument type isn't defined, set it to "string" to use "additonalData" property
commandArgument.type ??= 'string'
switch (commandArgument.type) {
case 'command':
return this.mergeCompletionItems(
await this.getCommandCompletionItems(),
{ documentation: commandArgument.description }
)
case 'selector':
return this.toCompletionItem(
['@a', '@e', '@p', '@s', '@r', '@initiator'],
commandArgument.description,
languages.CompletionItemKind.TypeParameter
)
case 'boolean':
return this.toCompletionItem(
['true', 'false'],
commandArgument.description,
languages.CompletionItemKind.Value
)
case 'number':
return this.toCompletionItem(
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
commandArgument.description,
languages.CompletionItemKind.Value
)
case 'coordinate':
return this.toCompletionItem(
['~', '^'],
commandArgument.description,
languages.CompletionItemKind.Operator
)
case 'string': {
let values: string[] = []
if (commandArgument.additionalData?.values)
values = values.concat(
commandArgument.additionalData.values
)
if (commandArgument.additionalData?.schemaReference)
values = values.concat(
<string[]>(
this.resolveDynamicReference(
commandArgument.additionalData.schemaReference
).map(({ value }) => value)
)
)
if (values.length === 0) return []
return this.toCompletionItem(
values,
commandArgument.description,
languages.CompletionItemKind.Constant
)
}
case 'jsonData':
return this.toCompletionItem(
[['{}', '{${1:json data}}']],
commandArgument.description,
languages.CompletionItemKind.Struct
)
case 'blockState':
return this.toCompletionItem(
[['[]', '[${1:block states}]']],
commandArgument.description,
languages.CompletionItemKind.Struct
)
case 'scoreData':
return this.toCompletionItem(
[['{}', '{${1:scores}}']],
commandArgument.description,
languages.CompletionItemKind.Struct
)
case 'subcommand':
return this.toCompletionItem(
commandName
? (await this.getSubcommands(commandName)).map(
(command) => command.commandName
)
: [],
undefined,
languages.CompletionItemKind.Constant
)
case 'integerRange':
return this.toCompletionItem(
['0', '1', '2', '3', '..0', '0..', '0..1'],
commandArgument.description,
languages.CompletionItemKind.Value
)
}
return []
}
async toCompletionItem(
strings: (string | [string, string])[],
documentation?: string,
kind?: languages.CompletionItemKind
): Promise<ICompletionItem[]> {
const { languages } = await useMonaco()
if (!kind) kind = languages.CompletionItemKind.Text
return strings.map((str) => ({
label: Array.isArray(str) ? `${str[0]}` : `${str}`,
insertText: Array.isArray(str) ? `${str[1]}` : `${str}`,
kind: kind!,
documentation,
insertTextRules: Array.isArray(str)
? languages.CompletionItemInsertTextRule.InsertAsSnippet
: undefined,
}))
}
protected mergeCompletionItems(
completionItems: ICompletionItem[],
partialItem: Partial<ICompletionItem>
) {
return completionItems.map((item) => ({
...item,
documentation: partialItem.documentation
? `${partialItem.documentation}\n\n${item.documentation ?? ''}`
: item.documentation,
}))
}
/**
* Dynamic references hook into our JSON schema engine to pull in dynamic data such as entity events
*/
protected resolveDynamicReference(reference: string) {
const refSchema = new RefSchema(reference, '$ref', reference)
// Return completions items from refSchema
return refSchema.getCompletionItems({})
}
}