|
| 1 | +// This is the code for a custom Syntax Highlighter and for Snippets for the Minecraft Script programming language. |
| 2 | +// Editor library used: Microsoft Monaco |
| 3 | + |
| 4 | +monaco.languages.register({ id: 'mcscript' }); |
| 5 | +monaco.languages.register({ id: 'mcfunction' }); |
| 6 | + |
| 7 | +// Register a tokens provider for the language |
| 8 | +monaco.languages.setMonarchTokensProvider('mcscript', { |
| 9 | +defaultOptions: {}, |
| 10 | +entitys: ["@a","@p","@s","@r","@e"], |
| 11 | +tokenizer: { |
| 12 | + root: [ |
| 13 | + // [/[a-zA-Z_\x80-\xFF][\w\x80-\xFF]*/, { |
| 14 | + // cases: { '@entitys': 'entity'} }], |
| 15 | + [/\/(?:advancement|ban|banlist|data|clear|clone|debug|defaultgamemode|deop|difficulty|effect|execute|experience|fill|function|gamemode|gamerule|give|help|kick|kill|list|locate|me|msg|op|pardon|pardon-ip|particle|playsound|publish|recipe|reload|replaceitem|save-all|save-off|save-on|say|scoreboard|seed|setblock|setidletimeout|setworldspawn|spawnpoint|spreadplayers|stop|stopsound|summon|tag|team|teleport|tell|tellraw|time|title|trigger|w|weather|whitelist|worldborder|xp)\b/, "command"], |
| 16 | + [/\s*\/\/.*/, "comment"], |
| 17 | + [/(\=|\+\=|\-\=|\*\=|\/\=|\%\=|\+\+|\-\-|\*|\/|\+|\-|\%|\>|\<|\>\=|\<\=)/, "operator"], |
| 18 | + [/"(?:[^\\]|\\.)*?"/, "string"], |
| 19 | + [/'(?:[^\\]|\\.)*?'/, "string"], |
| 20 | + [/(\s*#file:|\s*#extend:)(\s+)([\w\d$\/\._-]*)/, "operator"], |
| 21 | + [/\s*#.*/, "comment"], |
| 22 | + [/\b[0-9\.\-]+\b/, "number"], |
| 23 | + [/(?:area_effect_cloud|armor_stand|arrow|bat|blaze|boat|cave_spider|chest_minecart|chicken|cod_mob|commandblock_minecart|cow|creeper|dolphin|donkey|dragon_fireball|drowned|drowned|egg|elder_guardian|ender_crystal|ender_dragon|ender_pearl|enderman|endermite|evocation_fangs|evocation_illager|eye_of_ender_signal|falling_block|fireball|fireworks_rocket|furnace_minecart|ghast|giant|guardian|hopper_minecart|horse|husk|illusion_illager|item|item_frame|leash_knot|lightning_bolt|llama|llama_spit|magma_cube|minecart|mooshroom|mule|ocelot|painting|parrot|phantom|pig|polar_bear|potion|puffer_fish|rabbit|salmon_mob|sheep|shulker|shulker_bullet|silverfish|skeleton|skeleton_horse|slime|small_fireball|snowball|snowman|spawner_minecart|spectral_arrow|spider|squid|stray|tnt|tnt_minecart|trident|tropical_fish|turtle|vex|villager|villager_golem|vindication_illager|witch|wither|wither_skeleton|wither_skull|wolf|xp_bottle|xp_orb|zombie|zombie_horse|zombie_pigman|zombie_villager)\b/, "property"], |
| 24 | + [/(?:if|~|then|else|true|false|as|at|asat|positioned|align|dimension|rotated|anchored|while|do|forEach|for|raycast|stop|continue|switch|case|default|var|bool|boolean|tag|score|const)/, "keyword"], |
| 25 | + [/(?:grant|revoke|start|stop|set|add|remove|merge|entity|block|nbt|gamemode|level|dx|dy|dz|distance|x_rotation|y_rotation|limit|tag|type|sort|scores)/, "keyword2"], |
| 26 | + [/(modal)(\s+)([\w\d$\/\._-]*)/, "keyword"], |
| 27 | + [/(\$[\w\-]*)/, "variable"], |
| 28 | + [/([a-zA-Z0-9]+)(?=\s*\(.*)/, "variable"], |
| 29 | + ] |
| 30 | +} |
| 31 | +}); |
| 32 | +monaco.languages.setLanguageConfiguration('mcscript', { |
| 33 | +defaultOptions: {}, |
| 34 | +brackets: [ |
| 35 | + ["{","}"], |
| 36 | + ["(",")"], |
| 37 | + ["[","]"], |
| 38 | +], |
| 39 | +comments: { |
| 40 | + lineComment: "//", |
| 41 | +}, |
| 42 | +folding: { |
| 43 | + offSide: true |
| 44 | +} |
| 45 | +}) |
| 46 | +monaco.languages.setMonarchTokensProvider('mcfunction', { |
| 47 | +tokenizer: { |
| 48 | + root: [ |
| 49 | + // [/[a-zA-Z_\x80-\xFF][\w\x80-\xFF]*/, { |
| 50 | + // cases: { '@entitys': 'entity'} }], |
| 51 | + [/(?:advancement|ban|banlist|data|clear|clone|debug|defaultgamemode|deop|difficulty|effect|execute|experience|fill|function|gamemode|gamerule|give|help|kick|kill|list|locate|pardon|pardon-ip|particle|playsound|publish|recipe|reload|replaceitem|save-all|save-off|save-on|say|scoreboard|seed|setblock|setidletimeout|setworldspawn|spawnpoint|spreadplayers|stop|stopsound|summon|tag|team|teleport|tell|tellraw|time|title|trigger|weather|whitelist|worldborder)\b/, "command"], |
| 52 | + [/(\=|\+\=|\-\=|\*\=|\/\=|\%\=|\+\+|\-\-|\*|\/|\+|\-|\%|\>|\<|\>\=|\<\=)/, "operator"], |
| 53 | + [/"(?:[^\\]|\\.)*?"/, "string"], |
| 54 | + [/'(?:[^\\]|\\.)*?'/, "string"], |
| 55 | + [/(\s*#file:|\s*#extend:)(\s+)([\w\d$\/\._-]*)/, "operator"], |
| 56 | + [/\s*\/\/.*/, "comment"], |
| 57 | + [/\s*#.*/, "comment"], |
| 58 | + [/\b[0-9\.\-]+\b/, "number"], |
| 59 | + [/(?:area_effect_cloud|armor_stand|arrow|bat|blaze|boat|cave_spider|chest_minecart|chicken|cod_mob|commandblock_minecart|cow|creeper|dolphin|donkey|dragon_fireball|drowned|drowned|egg|elder_guardian|ender_crystal|ender_dragon|ender_pearl|enderman|endermite|evocation_fangs|evocation_illager|eye_of_ender_signal|falling_block|fireball|fireworks_rocket|furnace_minecart|ghast|giant|guardian|hopper_minecart|horse|husk|illusion_illager|item|item_frame|leash_knot|lightning_bolt|llama|llama_spit|magma_cube|minecart|mooshroom|mule|ocelot|painting|parrot|phantom|pig|polar_bear|potion|puffer_fish|rabbit|salmon_mob|sheep|shulker|shulker_bullet|silverfish|skeleton|skeleton_horse|slime|small_fireball|snowball|snowman|spawner_minecart|spectral_arrow|spider|squid|stray|tnt|tnt_minecart|trident|tropical_fish|turtle|vex|villager|villager_golem|vindication_illager|witch|wither|wither_skeleton|wither_skull|wolf|xp_bottle|xp_orb|zombie|zombie_horse|zombie_pigman|zombie_villager)\b/, "property"], |
| 60 | + [/(?:if|~|then|else|true|false|as|at|asat|positioned|align|dimension|rotated|anchored|while|do|forEach|for|raycast|stop|continue|switch|case|default|var|bool|boolean|tag|score|const)/, "keyword"], |
| 61 | + [/(?:grant|revoke|start|stop|set|add|remove|merge|entity|block|nbt|gamemode|level|dx|dy|dz|distance|x_rotation|y_rotation|limit|tag|type|sort|scores)/, "keyword2"], |
| 62 | + [/(modal)(\s+)([\w\d$\/\._-]*)/, "keyword"], |
| 63 | + [/(\$[\w\-]*)/, "variable"], |
| 64 | + [/([a-zA-Z0-9]+)(?=\s*\(.*)/, "variable"], |
| 65 | + ] |
| 66 | +} |
| 67 | +}); |
| 68 | + |
| 69 | +// Define a new theme that constains only rules that match this language |
| 70 | +monaco.editor.defineTheme('mcfunction', { |
| 71 | +base: 'vs-dark', |
| 72 | +inherit: true, |
| 73 | +rules: [ |
| 74 | +{ token: 'command', foreground: 'FF4500' }, |
| 75 | +{ token: 'number', foreground: '02ABFD' }, |
| 76 | +{ token: 'comment', foreground: '6F6F6F' }, |
| 77 | +{ token: 'operator', foreground: 'FF5B5B' }, |
| 78 | +{ token: 'keyword', foreground: 'FF8040' }, |
| 79 | +{ token: 'keyword2', foreground: '209CDF' }, |
| 80 | +{ token: 'entity', foreground: 'FF5B5B' }, |
| 81 | +{ token: 'string', foreground: '8ED200' }, |
| 82 | +{ token: 'property', foreground: '8FB900' }, |
| 83 | +] |
| 84 | +}); |
| 85 | + |
| 86 | +// Register a completion item provider for the new language |
| 87 | +monaco.languages.registerCompletionItemProvider('mcscript', { |
| 88 | +provideCompletionItems: () => { |
| 89 | +return [ |
| 90 | +{ |
| 91 | + label: 'file', |
| 92 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 93 | + insertText: { |
| 94 | + value: '#file: ./${1:filename}', |
| 95 | + }, |
| 96 | + documentation: 'add a new file' |
| 97 | +}, |
| 98 | +{ |
| 99 | + label: 'ifelse', |
| 100 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 101 | + insertText: { |
| 102 | + value: [ |
| 103 | + 'if (${1:condition}) {', |
| 104 | + '\t$0', |
| 105 | + '} else {', |
| 106 | + '\t', |
| 107 | + '}' |
| 108 | + ].join('\n') |
| 109 | + }, |
| 110 | + documentation: 'If-Else Statement' |
| 111 | +},{ |
| 112 | + label: 'as', |
| 113 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 114 | + insertText: { |
| 115 | + value: [ |
| 116 | + 'as (${1:entity}) {', |
| 117 | + '\t$0', |
| 118 | + '}' |
| 119 | + ].join('\n') |
| 120 | + }, |
| 121 | + documentation: 'as execute subcommand group' |
| 122 | +},{ |
| 123 | + label: 'at', |
| 124 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 125 | + insertText: { |
| 126 | + value: [ |
| 127 | + 'at (${1:entity}) {', |
| 128 | + '\t$0', |
| 129 | + '}' |
| 130 | + ].join('\n') |
| 131 | + }, |
| 132 | + documentation: 'at execute subcommand group' |
| 133 | +},{ |
| 134 | + label: 'positioned', |
| 135 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 136 | + insertText: { |
| 137 | + value: [ |
| 138 | + 'positioned (${1:string}) {', |
| 139 | + '\t$0', |
| 140 | + '}' |
| 141 | + ].join('\n') |
| 142 | + }, |
| 143 | + documentation: 'positioned execute subcommand group' |
| 144 | +},{ |
| 145 | + label: 'align', |
| 146 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 147 | + insertText: { |
| 148 | + value: [ |
| 149 | + 'align (${1:string}) {', |
| 150 | + '\t$0', |
| 151 | + '}' |
| 152 | + ].join('\n') |
| 153 | + }, |
| 154 | + documentation: 'align execute subcommand group' |
| 155 | +},{ |
| 156 | + label: 'rotated', |
| 157 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 158 | + insertText: { |
| 159 | + value: [ |
| 160 | + 'rotated (${1:string}) {', |
| 161 | + '\t$0', |
| 162 | + '}' |
| 163 | + ].join('\n') |
| 164 | + }, |
| 165 | + documentation: 'rotated execute subcommand group' |
| 166 | +},{ |
| 167 | + label: 'dimension', |
| 168 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 169 | + insertText: { |
| 170 | + value: [ |
| 171 | + 'dimension (${1:string}) {', |
| 172 | + '\t$0', |
| 173 | + '}' |
| 174 | + ].join('\n') |
| 175 | + }, |
| 176 | + documentation: 'in execute subcommand group' |
| 177 | +},{ |
| 178 | + label: 'anchored', |
| 179 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 180 | + insertText: { |
| 181 | + value: [ |
| 182 | + 'anchored (${1:string}) {', |
| 183 | + '\t$0', |
| 184 | + '}' |
| 185 | + ].join('\n') |
| 186 | + }, |
| 187 | + documentation: 'anchored execute subcommand group' |
| 188 | +},{ |
| 189 | + label: 'asat', |
| 190 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 191 | + insertText: { |
| 192 | + value: [ |
| 193 | + 'asat (${1:entity}) {', |
| 194 | + '\t$0', |
| 195 | + '}' |
| 196 | + ].join('\n') |
| 197 | + }, |
| 198 | + documentation: 'as at @s execute subcommand group' |
| 199 | +},{ |
| 200 | + label: 'if', |
| 201 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 202 | + insertText: { |
| 203 | + value: [ |
| 204 | + 'if (${1:condition}) {', |
| 205 | + '\t$0', |
| 206 | + '}' |
| 207 | + ].join('\n') |
| 208 | + }, |
| 209 | + documentation: 'If Statement. Insert a condition' |
| 210 | +}, |
| 211 | +{ |
| 212 | + label: 'switch', |
| 213 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 214 | + insertText: { |
| 215 | + value: [ |
| 216 | + 'switch (${1:variable}) {', |
| 217 | + '\tcase ${2:value}: {', |
| 218 | + '\t\t$0', |
| 219 | + '\t}', |
| 220 | + '}' |
| 221 | + ].join('\n') |
| 222 | + }, |
| 223 | + documentation: `switch([var_name]){ |
| 224 | + case <=|<|==|>|>= [other_var]|[number] { |
| 225 | + [actions] |
| 226 | + }, |
| 227 | + default(optional) { |
| 228 | + [default actions] |
| 229 | + } |
| 230 | + }` |
| 231 | +},{ |
| 232 | + label: 'case', |
| 233 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 234 | + insertText: { |
| 235 | + value: [ |
| 236 | + 'case ${1:value}: {', |
| 237 | + '\t$0', |
| 238 | + '}', |
| 239 | + ].join('\n') |
| 240 | + }, |
| 241 | + documentation: ` |
| 242 | + case <=|<|==|>|>= [other_var]|[number] { |
| 243 | + [actions] |
| 244 | + }, |
| 245 | + ` |
| 246 | +},{ |
| 247 | + label: 'for-loop', |
| 248 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 249 | + insertText: { |
| 250 | + value: [ |
| 251 | + 'for(${1:from},${2:to}) {', |
| 252 | + '\t$0', |
| 253 | + '}', |
| 254 | + ].join('\n') |
| 255 | + }, |
| 256 | + documentation: `The simple for loop. |
| 257 | +for(from, to, [varname]){ |
| 258 | +
|
| 259 | +} |
| 260 | + ` |
| 261 | +},{ |
| 262 | + label: 'while', |
| 263 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 264 | + insertText: { |
| 265 | + value: [ |
| 266 | + 'while(${1:condition}) {', |
| 267 | + '\t$0', |
| 268 | + '}', |
| 269 | + ].join('\n') |
| 270 | + }, |
| 271 | + documentation: `A simple loop. |
| 272 | +while(condition){ |
| 273 | +
|
| 274 | +} |
| 275 | + ` |
| 276 | +},{ |
| 277 | + label: 'do-while', |
| 278 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 279 | + insertText: { |
| 280 | + value: [ |
| 281 | + 'do {', |
| 282 | + '\t$0', |
| 283 | + '} while(${1:condition})', |
| 284 | + ].join('\n') |
| 285 | + }, |
| 286 | + documentation: `A simple do-while loop. |
| 287 | +do { |
| 288 | +
|
| 289 | +} while(condition) |
| 290 | + ` |
| 291 | +},{ |
| 292 | + label: 'modal', |
| 293 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 294 | + insertText: { |
| 295 | + value: [ |
| 296 | + 'modal ${1:name} (${2:arguments}) {', |
| 297 | + '\t$0', |
| 298 | + '}', |
| 299 | + ].join('\n') |
| 300 | + }, |
| 301 | + documentation: `Modal. Like a function or method` |
| 302 | +},{ |
| 303 | + label: 'forEach', |
| 304 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 305 | + insertText: { |
| 306 | + value: [ |
| 307 | + 'forEach(var ${1:varname} = ${2:startValue}; ${1:varname} ${3:condition}; ${1:varname}++) {', |
| 308 | + '\t$0', |
| 309 | + '}', |
| 310 | + ].join('\n') |
| 311 | + }, |
| 312 | + documentation: `The more advanced forEach loop to count in a loop. |
| 313 | +forEach(var [var_name] = [start value]; [var_name] ==|>|<|<=|>=|!= [other_var]|[number]; [varname]++){ |
| 314 | +
|
| 315 | +} |
| 316 | + ` |
| 317 | +},{ |
| 318 | + label: 'raycast', |
| 319 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 320 | + insertText: { |
| 321 | + value: [ |
| 322 | + 'raycast {', |
| 323 | + '\t$0', |
| 324 | + '}', |
| 325 | + ].join('\n') |
| 326 | + }, |
| 327 | + documentation: `A very simple raycast method` |
| 328 | +},{ |
| 329 | + label: 'raycast-advanced', |
| 330 | + kind: monaco.languages.CompletionItemKind.Snippet, |
| 331 | + insertText: { |
| 332 | + value: [ |
| 333 | + 'raycast(${1:distance}, ${2:blocks}, ${3:target}) {', |
| 334 | + '\t$0', |
| 335 | + '},{', |
| 336 | + '\t$0', |
| 337 | + '}', |
| 338 | + ].join('\n') |
| 339 | + }, |
| 340 | + documentation: `A very simple raycast method` |
| 341 | +}, |
| 342 | +] |
| 343 | +} |
| 344 | +}); |
0 commit comments