forked from Bard-Gaming/Minecraft-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover-docs.mjs
More file actions
379 lines (359 loc) · 11.5 KB
/
Copy pathhover-docs.mjs
File metadata and controls
379 lines (359 loc) · 11.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
import * as vscode from 'vscode'
/** @typedef {{ syntax: string, detail?: string, description: string }} HoverDoc */
/** @type {Record<string, HoverDoc>} */
export const KEYWORDS = {
var: {
syntax: 'var <name> = <value>',
detail: 'keyword',
description: 'Declares a mutable variable.',
},
const: {
syntax: 'const <name> = <value>',
detail: 'keyword',
description: 'Declares a constant binding.',
},
set: {
syntax: 'set <name> = <value>',
detail: 'keyword',
description: 'Assigns a new value to an existing variable.',
},
if: {
syntax: 'if (<condition>) { ... } else { ... }',
detail: 'keyword',
description: 'Conditional branch.',
},
else: {
syntax: 'else { ... }',
detail: 'keyword',
description: 'Alternative branch for an `if` statement.',
},
for: {
syntax: 'for (<name> in <iterable>) { ... }',
detail: 'keyword',
description: 'Iterates over each item in a list or range.',
},
in: {
syntax: 'for (<name> in <iterable>)',
detail: 'keyword',
description: 'Used with `for` to iterate over a list or `range()` result.',
},
while: {
syntax: 'while (<condition>) { ... }',
detail: 'keyword',
description: 'Repeats a block while the condition is true.',
},
async: {
syntax: 'async while (<condition>) { ... }',
detail: 'keyword',
description: 'Runs a while loop asynchronously across ticks.',
},
function: {
syntax: 'function <name>(<params>) { ... }',
detail: 'keyword',
description: 'Declares a user function. Use `on` after the signature to bind an advancement event.',
},
on: {
syntax: 'function <name>() on "<advancement criterion>" { ... }',
detail: 'keyword',
description: 'Registers the function to run when the given advancement criterion triggers.',
},
return: {
syntax: 'return <value>',
detail: 'keyword',
description: 'Returns a value from the current function.',
},
import: {
syntax: 'import "<path>" [as <alias>]',
detail: 'keyword',
description: 'Imports functions from another `.mcs` file.',
},
as: {
syntax: 'import "<path>" as <alias>',
detail: 'keyword',
description: 'Gives an imported module a local alias.',
},
true: {
syntax: 'true',
detail: 'literal',
description: 'Boolean true value.',
},
false: {
syntax: 'false',
detail: 'literal',
description: 'Boolean false value.',
},
null: {
syntax: 'null',
detail: 'literal',
description: 'Null value.',
},
}
/** @type {Record<string, HoverDoc>} */
export const BUILTINS = {
command: {
syntax: 'function command(command: string): null',
detail: 'builtin',
description: 'Runs a raw Minecraft command string. The string is validated by Spyglass.',
},
log: {
syntax: 'function log(...values: any[]): null',
detail: 'builtin',
description: 'Logs up to 5 values to chat (via tellraw) when compiled.',
},
tellraw: {
syntax: 'function tellraw(target: string, message: TextComponent): null',
detail: 'builtin',
description: 'Sends a text component message to the given target selector.',
},
text: {
syntax: 'function text(initial?: string): TextComponent',
detail: 'builtin',
description: 'Creates a text component. Chain methods like `.text()`, `.color()`, and `.bold()`.',
},
title: {
syntax: 'function title(target: string, mode: string, message: TextComponent): null',
detail: 'builtin',
description: 'Shows a title, subtitle, or actionbar. `mode` is `"title"`, `"subtitle"`, or `"actionbar"`.',
},
title_times: {
syntax: 'function title_times(target: string, fadeIn: number, stay: number, fadeOut: number): null',
detail: 'builtin',
description: 'Sets title fade-in, stay, and fade-out durations in ticks.',
},
give_item: {
syntax: 'function give_item(item: string, components?: string, count?: number): null',
detail: 'builtin',
description: 'Gives the executing player an item. `components` is SNBT without outer braces.',
},
give_clickable_item: {
syntax: 'function give_clickable_item(onClick: function, name?: string, customModelData?: string | number): null',
detail: 'builtin',
description: 'Gives a clickable item that runs the given function when used.',
},
concatenate: {
syntax: 'function concatenate(a: string, b: string): string',
detail: 'builtin',
description: 'Concatenates two strings and returns the result.',
},
append: {
syntax: 'function append(list: list, value: any): null',
detail: 'builtin',
description: 'Appends a value to the end of a list.',
},
range: {
syntax: 'function range(bound: number): list',
detail: 'builtin',
description: 'Returns a list of numbers from `0` to `bound - 1`.',
},
get_block: {
syntax: 'function get_block(x: string | number, y: string | number, z: string | number): string',
detail: 'builtin',
description: 'Reads the block ID at the given coordinates.',
},
set_block: {
syntax: 'function set_block(block: string, x: string | number, y: string | number, z: string | number): null',
detail: 'builtin',
description: 'Places a block at the given coordinates.',
},
raycast_block: {
syntax: 'function raycast_block(onHit: function, distance: number, onStep?: function): null',
detail: 'builtin',
description: 'Raycasts along the executor\'s look direction and calls `onHit` when a block is found.',
},
raycast_entity: {
syntax: 'function raycast_entity(onHit: function, distance: number, onStep?: function): null',
detail: 'builtin',
description: 'Raycasts along the executor\'s look direction and calls `onHit` when an entity is found.',
},
}
/** @type {Record<string, HoverDoc>} */
export const METHODS = {
text: {
syntax: '.text(content: string): TextComponent',
detail: 'TextComponent method',
description: 'Sets the text content of this component.',
},
color: {
syntax: '.color(name: string): TextComponent',
detail: 'TextComponent method',
description: 'Sets a named color (for example `"gold"` or `"#FFAA00"`).',
},
font: {
syntax: '.font(id: string): TextComponent',
detail: 'TextComponent method',
description: 'Sets the font resource location.',
},
insertion: {
syntax: '.insertion(text: string): TextComponent',
detail: 'TextComponent method',
description: 'Sets text inserted into chat when the component is shift-clicked.',
},
bold: {
syntax: '.bold(): TextComponent',
detail: 'TextComponent method',
description: 'Enables bold formatting.',
},
italic: {
syntax: '.italic(): TextComponent',
detail: 'TextComponent method',
description: 'Enables italic formatting.',
},
underlined: {
syntax: '.underlined(): TextComponent',
detail: 'TextComponent method',
description: 'Enables underline formatting.',
},
strikethrough: {
syntax: '.strikethrough(): TextComponent',
detail: 'TextComponent method',
description: 'Enables strikethrough formatting.',
},
obfuscated: {
syntax: '.obfuscated(): TextComponent',
detail: 'TextComponent method',
description: 'Enables obfuscated (random character) formatting.',
},
translate: {
syntax: '.translate(key: string): TextComponent',
detail: 'TextComponent method',
description: 'Uses a translation key instead of literal text.',
},
append: {
syntax: '.append(other: TextComponent): TextComponent',
detail: 'TextComponent method',
description: 'Appends another text component to this one.',
},
click_run: {
syntax: '.click_run(command: string): TextComponent',
detail: 'TextComponent method',
description: 'Runs a command when the text is clicked.',
},
click_suggest: {
syntax: '.click_suggest(command: string): TextComponent',
detail: 'TextComponent method',
description: 'Suggests a command in chat when the text is clicked.',
},
click_open_url: {
syntax: '.click_open_url(url: string): TextComponent',
detail: 'TextComponent method',
description: 'Opens a URL when the text is clicked.',
},
click_copy: {
syntax: '.click_copy(text: string): TextComponent',
detail: 'TextComponent method',
description: 'Copies text to the clipboard when clicked.',
},
hover_text: {
syntax: '.hover_text(content: string | TextComponent): TextComponent',
detail: 'TextComponent method',
description: 'Shows hover text when the pointer is over this component.',
},
hover_item: {
syntax: '.hover_item(item: string, count?: number): TextComponent',
detail: 'TextComponent method',
description: 'Shows an item tooltip on hover.',
},
}
/** @type {Record<string, HoverDoc>} */
const SELECTORS = {
'@a': {
syntax: '@a[...]',
detail: 'target selector',
description: 'All players.',
},
'@e': {
syntax: '@e[...]',
detail: 'target selector',
description: 'All entities.',
},
'@p': {
syntax: '@p[...]',
detail: 'target selector',
description: 'Nearest player.',
},
'@r': {
syntax: '@r[...]',
detail: 'target selector',
description: 'Random player.',
},
'@s': {
syntax: '@s[...]',
detail: 'target selector',
description: 'The executing entity (self).',
},
}
const SELECTOR_PATTERN = /@(?:a|e|p|r|s)(?:\[[^\]]*\])?/g
const IDENTIFIER_PATTERN = /[A-Za-z_][A-Za-z0-9_-]*/
/**
* @param {vscode.TextDocument} document
* @param {vscode.Position} position
* @returns {{ range: vscode.Range, doc: HoverDoc } | undefined}
*/
function findHoverTarget(document, position) {
const line = position.line
const lineText = document.lineAt(line).text
const column = position.character
for (const match of lineText.matchAll(SELECTOR_PATTERN)) {
const start = match.index ?? 0
const end = start + match[0].length
if (column < start || column >= end) continue
const selectorKey = match[0].split('[')[0]
const doc = SELECTORS[selectorKey]
if (!doc) continue
return {
range: new vscode.Range(line, start, line, end),
doc,
}
}
const wordRange = document.getWordRangeAtPosition(position, IDENTIFIER_PATTERN)
if (!wordRange) return undefined
const word = document.getText(wordRange)
const charBefore = wordRange.start.character > 0 ? lineText[wordRange.start.character - 1] : ''
if (charBefore === '.') {
const methodDoc = METHODS[word]
if (methodDoc) {
return {
range: new vscode.Range(line, wordRange.start.character - 1, line, wordRange.end.character),
doc: methodDoc,
}
}
}
const builtinDoc = BUILTINS[word]
if (builtinDoc) return { range: wordRange, doc: builtinDoc }
const keywordDoc = KEYWORDS[word]
if (keywordDoc) return { range: wordRange, doc: keywordDoc }
return undefined
}
/**
* @param {HoverDoc} doc
* @returns {vscode.MarkdownString}
*/
function formatHoverMarkdown(doc) {
const markdown = new vscode.MarkdownString()
markdown.appendMarkdown('```mcs\n')
markdown.appendMarkdown(doc.syntax)
markdown.appendMarkdown('\n```\n\n')
if (doc.detail) markdown.appendMarkdown(`*${doc.detail}*\n\n`)
markdown.appendMarkdown(doc.description)
return markdown
}
/**
* @param {vscode.TextDocument} document
* @param {vscode.Position} position
* @returns {vscode.Hover | undefined}
*/
export function provideMcsHover(document, position) {
const target = findHoverTarget(document, position)
if (!target) return undefined
return new vscode.Hover(formatHoverMarkdown(target.doc), target.range)
}
/**
* @returns {vscode.HoverProvider}
*/
export function createMcsHoverProvider() {
return {
provideHover(document, position) {
return provideMcsHover(document, position)
},
}
}