Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add completions for empty strings, structs, and lists #1359

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions packages/json/src/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const object = core.completer.record<JsonStringNode, JsonNode, JsonObjectNode>({
detail: mcdoc.McdocType.toString(field.type as core.Mutable<mcdoc.McdocType>),
deprecated: field.deprecated,
sortText: field.optional ? '$b' : '$a', // sort above hardcoded $schema
filterText: `"${key}"`,
insertText: `"${key}"${iv ? ': ' : ''}${ipe ? '$1,' : ''}`,
filterText: formatKey(key),
insertText: `${formatKey(key)}${iv ? ': ' : ''}${ipe ? '$1,' : ''}`,
})
)
},
Expand Down Expand Up @@ -84,15 +84,57 @@ function getValues(
): core.CompletionItem[] {
return mcdoc.runtime.completer.getValues(typeDef, ctx)
.map(({ value, detail, kind, completionKind }) =>
core.CompletionItem.create(value, range, {
core.CompletionItem.create(formatLabel(value, kind), range, {
kind: completionKind ?? core.CompletionKind.Value,
detail,
filterText: kind === 'string' ? `"${value}"` : value,
insertText: kind === 'string' ? `"${value}"` : value,
filterText: formatValue(value, kind),
insertText: formatValue(value, kind),
})
)
}

function formatKey(key: string) {
return `"${core.completer.escapeString(key, '"')}"`
}

function formatLabel(value: string, kind?: mcdoc.McdocType['kind']) {
if (value.length === 0) {
switch (kind) {
case 'string':
return '""'
case 'struct':
return '{}'
case 'list':
case 'tuple':
case 'byte_array':
case 'int_array':
case 'long_array':
return '[]'
}
}
return value
}

function formatValue(value: string, kind?: mcdoc.McdocType['kind']) {
switch (kind) {
case 'string':
if (value.length === 0) {
return '"$1"'
}
return `"${core.completer.escapeString(value, '"')}"`
case 'struct':
return '{$1}'
case 'list':
case 'tuple':
case 'byte_array':
case 'int_array':
case 'long_array':
return '[$1]'
default:
return value
}
}

export function register(meta: core.MetaRegistry): void {
meta.registerCompleter('json:array', array)
meta.registerCompleter('json:boolean', primitive)
Expand Down
9 changes: 9 additions & 0 deletions packages/mcdoc/src/runtime/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ export function getValues(
detail: v.identifier,
kind: typeDef.enumKind ?? 'string',
}))
case 'struct':
case 'list':
case 'tuple':
case 'byte_array':
case 'int_array':
case 'byte_array':
case 'string':
// in case getStringCompletions at the top of this function returned an empty list
return [{ value: '', kind: typeDef.kind }]
default:
return []
}
Expand Down
37 changes: 36 additions & 1 deletion packages/nbt/src/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function getValues(
): core.CompletionItem[] {
return mcdoc.runtime.completer.getValues(typeDef, ctx)
.map(({ value, detail, kind, completionKind }) =>
core.CompletionItem.create(value, range, {
core.CompletionItem.create(formatLabel(value, kind), range, {
kind: completionKind ?? core.CompletionKind.Value,
detail,
filterText: formatValue(value, kind),
Expand All @@ -101,9 +101,33 @@ function formatKey(key: string, quote?: core.Quote) {
return q + core.completer.escapeString(key, q) + q
}

function formatLabel(value: string, kind?: mcdoc.McdocType['kind']) {
if (value.length === 0) {
switch (kind) {
case 'string':
return '""'
case 'struct':
return '{}'
case 'list':
case 'tuple':
return '[]'
case 'byte_array':
return '[B;]'
case 'int_array':
return '[I;]'
case 'long_array':
return '[L;]'
}
}
return value
}

function formatValue(value: string, kind?: mcdoc.McdocType['kind']) {
switch (kind) {
case 'string':
if (value.length === 0) {
return '"$1"'
}
return `"${core.completer.escapeString(value, '"')}"`
case 'byte':
return `${value}b`
Expand All @@ -113,6 +137,17 @@ function formatValue(value: string, kind?: mcdoc.McdocType['kind']) {
return `${value}L`
case 'float':
return `${value}f`
case 'struct':
return '{$1}'
case 'list':
case 'tuple':
return '[$1]'
case 'byte_array':
return '[B;$1]'
case 'int_array':
return '[I;$1]'
case 'long_array':
return '[L;$1]'
default:
return value
}
Expand Down
Loading