From 2463d6df148c10b64f881eafd9f1f6033fe281cf Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 21 Jun 2024 17:10:27 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20completions=20for=20empty=20s?= =?UTF-8?q?trings,=20structs,=20and=20lists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/json/src/completer/index.ts | 52 +++++++++++++++++-- packages/mcdoc/src/runtime/completer/index.ts | 9 ++++ packages/nbt/src/completer/index.ts | 37 ++++++++++++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/packages/json/src/completer/index.ts b/packages/json/src/completer/index.ts index c52d1ffe0..c3ae67d3e 100644 --- a/packages/json/src/completer/index.ts +++ b/packages/json/src/completer/index.ts @@ -41,8 +41,8 @@ const object = core.completer.record({ detail: mcdoc.McdocType.toString(field.type as core.Mutable), 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,' : ''}`, }) ) }, @@ -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) diff --git a/packages/mcdoc/src/runtime/completer/index.ts b/packages/mcdoc/src/runtime/completer/index.ts index c07d91dde..0953db7eb 100644 --- a/packages/mcdoc/src/runtime/completer/index.ts +++ b/packages/mcdoc/src/runtime/completer/index.ts @@ -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 [] } diff --git a/packages/nbt/src/completer/index.ts b/packages/nbt/src/completer/index.ts index ed197755d..c999ee5bd 100644 --- a/packages/nbt/src/completer/index.ts +++ b/packages/nbt/src/completer/index.ts @@ -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), @@ -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` @@ -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 }