diff --git a/packages/java-edition/src/mcfunction/index.ts b/packages/java-edition/src/mcfunction/index.ts index 34ce81e2a..60b1cdc8a 100644 --- a/packages/java-edition/src/mcfunction/index.ts +++ b/packages/java-edition/src/mcfunction/index.ts @@ -1,4 +1,4 @@ -import type * as core from '@spyglassmc/core' +import * as core from '@spyglassmc/core' import * as mcf from '@spyglassmc/mcfunction' import type { McmetaCommands, ReleaseVersion } from '../dependency/index.js' import * as checker from './checker/index.js' @@ -22,17 +22,14 @@ export const initialize = ( ) => { const { meta } = ctx + const tree = core.merge(commands, getPatch(releaseVersion)) + mcf.initialize(ctx) - mcf.CommandTreeRegistry.instance.register( - releaseVersion, - commands, - getPatch(releaseVersion), - ) meta.registerLanguage('mcfunction', { extensions: ['.mcfunction'], - parser: mcf.entry(releaseVersion, parser.argument, true), - completer: mcf.completer.entry(releaseVersion, completer.getMockNodes), + parser: mcf.entry(tree, parser.argument, true), + completer: mcf.completer.entry(tree, completer.getMockNodes), triggerCharacters: [ ' ', '[', @@ -56,7 +53,7 @@ export const initialize = ( meta.registerParser( 'mcfunction:command', mcf.command( - mcf.CommandTreeRegistry.instance.get(releaseVersion), + tree, parser.argument, ), ) @@ -66,5 +63,5 @@ export const initialize = ( completer.register(meta) meta.registerInlayHintProvider(inlayHintProvider) - meta.registerSignatureHelpProvider(signatureHelpProvider(releaseVersion)) + meta.registerSignatureHelpProvider(signatureHelpProvider(tree)) } diff --git a/packages/java-edition/src/mcfunction/signatureHelpProvider.ts b/packages/java-edition/src/mcfunction/signatureHelpProvider.ts index ee2a6bb77..0653f3175 100644 --- a/packages/java-edition/src/mcfunction/signatureHelpProvider.ts +++ b/packages/java-edition/src/mcfunction/signatureHelpProvider.ts @@ -1,15 +1,14 @@ import * as core from '@spyglassmc/core' import * as mcf from '@spyglassmc/mcfunction' +import type { RootTreeNode } from '../dependency' /** * Only command options that can be satisfied by the current command node will be listed in `signatures`. * Only parameters at and immediately after the `offset` will be listed in `parameters`. */ export function signatureHelpProvider( - commandTreeName: string, + rootTreeNode: RootTreeNode, ): core.SignatureHelpProvider> { - const rootTreeNode = mcf.CommandTreeRegistry.instance.get(commandTreeName) - return (fileNode, ctx) => { if (fileNode.children[0]?.type !== 'mcfunction:entry') { // Not mcfunction. diff --git a/packages/mcfunction/src/completer/index.ts b/packages/mcfunction/src/completer/index.ts index 84af390b8..43706f756 100644 --- a/packages/mcfunction/src/completer/index.ts +++ b/packages/mcfunction/src/completer/index.ts @@ -6,7 +6,6 @@ import { CommandNode } from '../node/index.js' import type { ArgumentTreeNode, RootTreeNode } from '../tree/index.js' import { categorizeTreeChildren, - CommandTreeRegistry, redirect, resolveParentTreeNode, } from '../tree/index.js' @@ -21,11 +20,10 @@ export type MockNodesGetter = ( * will be used for completing the argument. */ export function entry( - commandTreeName: string, + tree: RootTreeNode, getMockNodes: MockNodesGetter, ): core.Completer { return (node, ctx) => { - const tree = CommandTreeRegistry.instance.get(commandTreeName) const childNode = core.AstNode.findChild(node, ctx.offset, true) if (core.CommentNode.is(childNode) || CommandMacroNode.is(childNode)) { return [] diff --git a/packages/mcfunction/src/parser/entry.ts b/packages/mcfunction/src/parser/entry.ts index e5d6dcfba..703bf879d 100644 --- a/packages/mcfunction/src/parser/entry.ts +++ b/packages/mcfunction/src/parser/entry.ts @@ -4,7 +4,7 @@ import type { CommandNode, McfunctionNode, } from '../node/index.js' -import { CommandTreeRegistry } from '../tree/index.js' +import type { RootTreeNode } from '../tree/index.js' import type { ArgumentParserGetter } from './argument.js' import { command } from './command.js' @@ -12,7 +12,7 @@ import { command } from './command.js' * @throws When there's no command tree associated with `commandTreeName`. */ function mcfunction( - commandTreeName: string, + commandTree: RootTreeNode, argument: ArgumentParserGetter, ): core.Parser { return (src, ctx) => { @@ -35,7 +35,7 @@ function mcfunction( } } else { result = command( - CommandTreeRegistry.instance.get(commandTreeName), + commandTree, argument, )(src, ctx) } @@ -58,11 +58,11 @@ const comment = core.comment({ * Disabled by default. */ export const entry = ( - commandTreeName: string, + commandTree: RootTreeNode, argument: ArgumentParserGetter, supportsBackslashContinuation = false, ) => { - const parser = mcfunction(commandTreeName, argument) + const parser = mcfunction(commandTree, argument) return supportsBackslashContinuation ? core.concatOnTrailingBackslash(parser) : parser diff --git a/packages/mcfunction/src/tree/index.ts b/packages/mcfunction/src/tree/index.ts index 9f07290a1..67ceecd42 100644 --- a/packages/mcfunction/src/tree/index.ts +++ b/packages/mcfunction/src/tree/index.ts @@ -1,3 +1,2 @@ -export * from './registry.js' export * from './type.js' export * from './util.js' diff --git a/packages/mcfunction/src/tree/registry.ts b/packages/mcfunction/src/tree/registry.ts deleted file mode 100644 index 6d9fadcfa..000000000 --- a/packages/mcfunction/src/tree/registry.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { merge } from '@spyglassmc/core' -import type { PartialRootTreeNode, RootTreeNode } from './type.js' - -/* istanbul ignore next */ -/** - * The registry for mcfunction command trees. - * This is a singleton; use the `instance` static property to get an instance. - */ -export class CommandTreeRegistry { - readonly #trees = new Map() - - /** - * Register command tree for an arbitrary version. - * - * @param version The game version. e.g. `1.15-tdn`. - * @param tree The command tree for this version. - * @param treePatch A custom command tree patch that will be merged onto `tree`. - */ - public register( - version: string, - tree: RootTreeNode, - treePatch?: PartialRootTreeNode, - ): void { - this.#trees.set(version, treePatch ? merge(tree, treePatch) : tree) - } - - /** - * @throws When there's no command tree associated with the version. - */ - public get(version: string): RootTreeNode { - const ans = this.#trees.get(version) - if (!ans) { - throw new Error(`No command tree exist for version “${version}”`) - } - return ans - } - - /** - * An instance of `CommandTreeRegistry`. - */ - public static get instance(): CommandTreeRegistry { - return this._instance ?? (this._instance = new CommandTreeRegistry()) - } - private constructor() { - if (CommandTreeRegistry._instance) { - throw new Error( - 'Use the `instance` static property to get an instance.', - ) - } - } - private static _instance: CommandTreeRegistry -}