From 2c6c2970f578bb131fafe94c4448a6ed1fd01e37 Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 5 May 2024 21:41:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20simple=20macro=20parser=20and?= =?UTF-8?q?=20colorizer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mcfunction/src/colorizer/index.ts | 9 +++++++++ packages/mcfunction/src/completer/index.ts | 3 ++- packages/mcfunction/src/node/command.ts | 13 +++++++++++++ packages/mcfunction/src/node/entry.ts | 4 ++-- packages/mcfunction/src/parser/entry.ts | 15 +++++++++++++-- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/mcfunction/src/colorizer/index.ts b/packages/mcfunction/src/colorizer/index.ts index d769a9342..afe707a78 100644 --- a/packages/mcfunction/src/colorizer/index.ts +++ b/packages/mcfunction/src/colorizer/index.ts @@ -1,10 +1,15 @@ import * as core from '@spyglassmc/core' import type { + CommandMacroNode, LiteralCommandChildNode, TrailingCommandChildNode, } from '../node/index.js' export function register(meta: core.MetaRegistry) { + meta.registerColorizer( + 'mcfunction:command_macro', + macro, + ) meta.registerColorizer( 'mcfunction:command_child/literal', core.colorizer.literal, @@ -14,3 +19,7 @@ export function register(meta: core.MetaRegistry) { core.colorizer.error, ) } + +export const macro: core.Colorizer = (node, ctx) => { + return [core.ColorToken.create(node, 'string')] +} diff --git a/packages/mcfunction/src/completer/index.ts b/packages/mcfunction/src/completer/index.ts index 9c0d43701..c5dc0dd8c 100644 --- a/packages/mcfunction/src/completer/index.ts +++ b/packages/mcfunction/src/completer/index.ts @@ -1,6 +1,7 @@ import type { DeepReadonly } from '@spyglassmc/core' import * as core from '@spyglassmc/core' import type { McfunctionNode } from '../node/index.js' +import { CommandMacroNode } from '../node/index.js' import { CommandNode } from '../node/index.js' import type { ArgumentTreeNode, RootTreeNode } from '../tree/index.js' import { @@ -26,7 +27,7 @@ export function entry( return (node, ctx) => { const tree = CommandTreeRegistry.instance.get(commandTreeName) const childNode = core.AstNode.findChild(node, ctx.offset, true) - if (core.CommentNode.is(childNode)) { + if (core.CommentNode.is(childNode) || CommandMacroNode.is(childNode)) { return [] } else { return command(tree, getMockNodes)( diff --git a/packages/mcfunction/src/node/command.ts b/packages/mcfunction/src/node/command.ts index ba904767f..8d68a9514 100644 --- a/packages/mcfunction/src/node/command.ts +++ b/packages/mcfunction/src/node/command.ts @@ -19,6 +19,19 @@ export namespace CommandNode { } } +export interface CommandMacroNode extends core.AstNode { + type: 'mcfunction:command_macro' +} + +export const CommandMacroNode = Object.freeze({ + is | undefined>( + obj: T, + ): obj is core.NodeIsHelper { + return (obj as CommandMacroNode | undefined)?.type === + 'mcfunction:command_macro' + }, +}) + export interface CommandChildNode extends core.AstNode { type: 'mcfunction:command_child' /** diff --git a/packages/mcfunction/src/node/entry.ts b/packages/mcfunction/src/node/entry.ts index a9789d7f0..b85c1c11b 100644 --- a/packages/mcfunction/src/node/entry.ts +++ b/packages/mcfunction/src/node/entry.ts @@ -1,8 +1,8 @@ import type * as core from '@spyglassmc/core' -import type { CommandNode } from './command.js' +import type { CommandMacroNode, CommandNode } from './command.js' export interface McfunctionNode - extends core.SequenceNode + extends core.SequenceNode { type: 'mcfunction:entry' } diff --git a/packages/mcfunction/src/parser/entry.ts b/packages/mcfunction/src/parser/entry.ts index 8d1622559..c4522e9aa 100644 --- a/packages/mcfunction/src/parser/entry.ts +++ b/packages/mcfunction/src/parser/entry.ts @@ -1,5 +1,9 @@ import * as core from '@spyglassmc/core' -import type { CommandNode, McfunctionNode } from '../node/index.js' +import type { + CommandMacroNode, + CommandNode, + McfunctionNode, +} from '../node/index.js' import { CommandTreeRegistry } from '../tree/index.js' import type { ArgumentParserGetter } from './argument.js' import { command } from './command.js' @@ -19,9 +23,16 @@ export function entry( } while (src.skipWhitespace().canReadInLine()) { - let result: core.CommentNode | CommandNode + let result: core.CommentNode | CommandNode | CommandMacroNode if (src.peek() === '#') { result = comment(src, ctx) as core.CommentNode + } else if (src.peek() === '$') { + const start = src.cursor + src.skipLine() + result = { + type: 'mcfunction:command_macro', + range: core.Range.create(start, src), + } } else { result = command( CommandTreeRegistry.instance.get(commandTreeName),