From 162d9975b44c0402351490d9644f0b2c23874aed Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 22 Nov 2024 01:59:46 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20implicit=20pa?= =?UTF-8?q?ths=20in=20`id`=20attribute=20(#1640)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/node/ResourceLocationNode.ts | 1 + packages/core/src/processor/binder/builtin.ts | 7 ++++- .../mcdoc/src/runtime/attribute/builtin.ts | 27 +++++++++---------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/core/src/node/ResourceLocationNode.ts b/packages/core/src/node/ResourceLocationNode.ts index a97d2d135..2f207b714 100644 --- a/packages/core/src/node/ResourceLocationNode.ts +++ b/packages/core/src/node/ResourceLocationNode.ts @@ -16,6 +16,7 @@ export type ResourceLocationOptions = requireCanonical?: boolean usageType?: SymbolUsageType namespacePathSep?: ':' | '.' + implicitPath?: string } & ({ category: ResourceLocationCategory diff --git a/packages/core/src/processor/binder/builtin.ts b/packages/core/src/processor/binder/builtin.ts index e09099490..985bb5b53 100644 --- a/packages/core/src/processor/binder/builtin.ts +++ b/packages/core/src/processor/binder/builtin.ts @@ -138,11 +138,16 @@ export const dispatchSync = SyncBinder.create((node, ctx) => { export const resourceLocation = SyncBinder.create((node, ctx) => { const raw = ResourceLocationNode.toString(node, 'full') - const sanitizedRaw = ResourceLocation.lengthen( + let sanitizedRaw = ResourceLocation.lengthen( node.options.namespacePathSep === '.' ? raw.replace(/\./g, ResourceLocation.NamespacePathSep) : raw, ) + if (node.options.implicitPath) { + const sepIndex = sanitizedRaw.indexOf(ResourceLocation.NamespacePathSep) + sanitizedRaw = sanitizedRaw.substring(0, sepIndex + 1) + node.options.implicitPath + + sanitizedRaw.substring(sepIndex + 1) + } if (node.options.category) { ctx.symbols.query( ctx.doc, diff --git a/packages/mcdoc/src/runtime/attribute/builtin.ts b/packages/mcdoc/src/runtime/attribute/builtin.ts index 2e508c3dd..7f673dbed 100644 --- a/packages/mcdoc/src/runtime/attribute/builtin.ts +++ b/packages/mcdoc/src/runtime/attribute/builtin.ts @@ -8,6 +8,7 @@ interface IdConfig { tags?: 'allowed' | 'implicit' | 'required' definition?: boolean prefix?: '!' + path?: string empty?: 'allowed' exclude?: string[] } @@ -19,6 +20,7 @@ const idValidator = validator.alternatives( tags: validator.optional(validator.options('allowed', 'implicit', 'required')), definition: validator.optional(validator.boolean), prefix: validator.optional(validator.options('!')), + path: validator.optional(validator.string), empty: validator.optional(validator.options('allowed')), exclude: validator.optional(validator.alternatives( validator.map(validator.string, v => [v]), @@ -36,7 +38,7 @@ const idValidator = validator.alternatives( ) function getResourceLocationOptions( - { registry, tags, definition }: IdConfig, + { registry, tags, definition, path }: IdConfig, requireCanonical: boolean, ctx: core.ContextBase, typeDef?: core.DeepReadonly, @@ -60,23 +62,20 @@ function getResourceLocationOptions( registry = `tag/${registry}` } if (tags === 'allowed' || tags === 'required') { - if (core.TaggableResourceLocationCategory.is(registry)) { - return { - category: registry, - requireCanonical, - allowTag: true, - requireTag: tags === 'required', - } - } - } else if (core.ResourceLocationCategory.is(registry)) { return { - category: registry, + category: registry as core.TaggableResourceLocationCategory, requireCanonical, - usageType: definition ? 'definition' : 'reference', + allowTag: true, + requireTag: tags === 'required', + implicitPath: path, } } - ctx.logger.warn(`[mcdoc id] Unhandled registry ${registry}`) - return undefined + return { + category: registry as core.ResourceLocationCategory, + requireCanonical, + usageType: definition ? 'definition' : 'reference', + implicitPath: path, + } } interface IntegerConfig {