Skip to content

Commit

Permalink
✨ Mcdoc binding improvements (#1673)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored Dec 13, 2024
1 parent ad19c1b commit 8e17b0c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 64 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/json/src/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const object = core.completer.record<JsonStringNode, JsonNode, JsonObjectNode>({
core.CompletionItem.create(key, pair?.key ?? range, {
kind: core.CompletionKind.Field,
detail: mcdoc.McdocType.toString(field.type as core.Mutable<mcdoc.McdocType>),
documentation: field.desc,
deprecated: field.deprecated,
sortText: field.optional ? '$b' : '$a', // sort above hardcoded $schema
filterText: `"${key}"`,
Expand Down Expand Up @@ -83,11 +84,14 @@ function getValues(
ctx: core.CompleterContext,
): core.CompletionItem[] {
return mcdoc.runtime.completer.getValues(typeDef, ctx)
.map(({ value, labelSuffix, detail, kind, completionKind, insertText, sortText }) =>
.map((
{ value, labelSuffix, detail, documentation, kind, completionKind, insertText, sortText },
) =>
core.CompletionItem.create(value, range, {
kind: completionKind ?? core.CompletionKind.Value,
labelSuffix,
detail,
documentation,
filterText: kind === 'string' ? `"${value}"` : value,
insertText: kind === 'string' ? `"${insertText ?? value}"` : insertText ?? value,
sortText,
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/util/toLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function completionItem(
kind: completion.kind,
...(completion.labelSuffix ? { labelDetails: { detail: completion.labelSuffix } } : {}),
detail: completion.detail,
documentation: completion.documentation,
documentation: completion.documentation ? markupContent(completion.documentation) : undefined,
filterText: completion.filterText,
sortText: completion.sortText,
textEdit,
Expand Down
10 changes: 5 additions & 5 deletions packages/mcdoc-cli/src/commands/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export async function localeCommand(args: Args) {
collect(name, member)
})
} else if (type.kind === 'enum') {
// for (const field of type.values) {
// if (field.desc) {
// add(`${name}.${field.identifier}`, field.desc)
// }
// }
for (const field of type.values) {
if (field.desc) {
add(`${name}.${field.identifier}`, field.desc)
}
}
} else if (type.kind === 'list') {
collect(name, type.item)
} else if (type.kind === 'tuple') {
Expand Down
26 changes: 21 additions & 5 deletions packages/mcdoc/src/binder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
traversePreOrder,
} from '@spyglassmc/core'
import { localeQuote, localize } from '@spyglassmc/locales'
import type { AdditionalContext } from '../common.js'
import type {
AnyTypeNode,
AttributeValueNode,
Expand Down Expand Up @@ -96,10 +95,12 @@ import type {
StructTypeField,
StructTypePairField,
StructTypeSpreadField,
UseStatementBindingData,
} from '../type/index.js'

interface McdocBinderContext extends BinderContext, AdditionalContext {}
interface McdocBinderContext extends BinderContext {
moduleIdentifier: string
isHoisting?: boolean
}

interface ModuleSymbolData {
nextAnonymousIndex: number
Expand All @@ -124,6 +125,10 @@ export namespace TypeDefSymbolData {
}
}

interface UseStatementBindingData {
target: readonly string[]
}

export const fileModule = AsyncBinder.create<ModuleNode>(async (node, ctx) => {
const moduleIdentifier = uriToIdentifier(ctx.doc.uri, ctx)
if (!moduleIdentifier) {
Expand All @@ -145,7 +150,7 @@ export async function module_(node: ModuleNode, ctx: McdocBinderContext): Promis
data: { data },
})

hoist(node, ctx)
hoist(node, { ...ctx, isHoisting: true })

for (const child of node.children) {
switch (child.type) {
Expand Down Expand Up @@ -847,6 +852,11 @@ function convertTypeArgBlock(node: TypeArgBlockNode, ctx: McdocBinderContext): M
function convertEnum(node: EnumNode, ctx: McdocBinderContext): McdocType {
const { block, enumKind, identifier } = EnumNode.destruct(node)

// Return reference if the enum has been hoisted
if (identifier && !ctx.isHoisting) {
return { kind: 'reference', path: `${ctx.moduleIdentifier}::${identifier.value}` }
}

// Shortcut if the typeDef has been added to the enum symbol.
const symbol = identifier?.symbol ?? node.symbol
if (symbol && TypeDefSymbolData.is(symbol.data) && symbol.data.typeDef.kind === 'enum') {
Expand All @@ -862,9 +872,10 @@ function convertEnumBlock(node: EnumBlockNode, ctx: McdocBinderContext): EnumTyp
}

function convertEnumField(node: EnumFieldNode, ctx: McdocBinderContext): EnumTypeField {
const { attributes, identifier, value } = EnumFieldNode.destruct(node)
const { attributes, docComments, identifier, value } = EnumFieldNode.destruct(node)
return {
attributes: convertAttributes(attributes, ctx),
desc: DocCommentsNode.asText(docComments),
identifier: identifier.value,
value: convertEnumValue(value, ctx),
}
Expand All @@ -881,6 +892,11 @@ function convertEnumValue(node: EnumValueNode, ctx: McdocBinderContext): string
function convertStruct(node: StructNode, ctx: McdocBinderContext): McdocType {
const { block, identifier } = StructNode.destruct(node)

// Return reference if the struct has been hoisted
if (identifier && !ctx.isHoisting) {
return { kind: 'reference', path: `${ctx.moduleIdentifier}::${identifier.value}` }
}

// Shortcut if the typeDef has been added to the struct symbol.
const symbol = identifier?.symbol ?? node.symbol
if (symbol && TypeDefSymbolData.is(symbol.data) && symbol.data.typeDef.kind === 'struct') {
Expand Down
4 changes: 0 additions & 4 deletions packages/mcdoc/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ export function identifierToSeg(identifier: string): Segments {
export function segToIdentifier(seg: Segments): string {
return `::${seg.join('::')}`
}

export interface AdditionalContext {
moduleIdentifier: string
}
8 changes: 7 additions & 1 deletion packages/mcdoc/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,15 @@ export interface EnumFieldNode extends AstNode {
export namespace EnumFieldNode {
export function destruct(
node: EnumFieldNode,
): { attributes: AttributeNode[]; identifier: IdentifierNode; value: EnumValueNode } {
): {
attributes: AttributeNode[]
docComments?: DocCommentsNode
identifier: IdentifierNode
value: EnumValueNode
} {
return {
attributes: node.children.filter(AttributeNode.is),
docComments: node.children.find(DocCommentsNode.is),
identifier: node.children.find(IdentifierNode.is)!,
value: node.children.find(EnumValueNode.is)!,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/mcdoc/src/runtime/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function getFields(
export type SimpleCompletionValue = {
value: string
detail?: string
documentation?: string
labelSuffix?: string
kind?: McdocType['kind']
completionKind?: core.CompletionKind
Expand Down Expand Up @@ -131,6 +132,7 @@ export function getValues(
value: `${v.value}`,
detail: v.identifier,
kind: typeDef.enumKind ?? 'string',
documentation: v.desc,
}))
case 'byte':
case 'short':
Expand Down
5 changes: 1 addition & 4 deletions packages/mcdoc/src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface EnumType extends McdocBaseType {
export interface EnumTypeField extends McdocBaseType {
identifier: string
value: string | number
desc?: string
}

export interface ReferenceType extends McdocBaseType {
Expand Down Expand Up @@ -611,7 +612,3 @@ export namespace McdocType {
return attributesString + typeString
}
}

export interface UseStatementBindingData {
target: readonly string[]
}
7 changes: 6 additions & 1 deletion packages/nbt/src/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const compound = core.completer.record<NbtStringNode, NbtNode, NbtCompoundNode>(
core.CompletionItem.create(key, pair?.key ?? range, {
kind: core.CompletionKind.Field,
detail: mcdoc.McdocType.toString(field.type as core.Mutable<mcdoc.McdocType>),
documentation: field.desc,
deprecated: field.deprecated,
sortText: field.optional ? '$b' : '$a', // sort above hardcoded $schema
filterText: formatKey(key, pair?.key?.quote),
Expand Down Expand Up @@ -126,6 +127,7 @@ function getPathKeys(
core.CompletionItem.create(key, range, {
kind: core.CompletionKind.Field,
detail: mcdoc.McdocType.toString(field.type as core.Mutable<mcdoc.McdocType>),
documentation: field.desc,
deprecated: field.deprecated,
sortText: field.optional ? '$b' : '$a', // sort above hardcoded $schema
filterText: formatKey(key, quote),
Expand All @@ -140,11 +142,14 @@ function getValues(
ctx: mcdoc.runtime.completer.McdocCompleterContext,
): core.CompletionItem[] {
return mcdoc.runtime.completer.getValues(typeDef, ctx)
.map(({ value, labelSuffix, detail, kind, completionKind, insertText, sortText }) =>
.map((
{ value, labelSuffix, detail, documentation, kind, completionKind, insertText, sortText },
) =>
core.CompletionItem.create(value, range, {
kind: completionKind ?? core.CompletionKind.Value,
labelSuffix,
detail,
documentation,
filterText: formatValue(value, kind),
insertText: formatValue(insertText ?? value, kind),
sortText,
Expand Down

0 comments on commit 8e17b0c

Please sign in to comment.