From b65d8b17c5854b3708270877b0550858e028100c Mon Sep 17 00:00:00 2001 From: William Killerud Date: Sun, 11 Aug 2024 15:47:50 +0200 Subject: [PATCH] fix: add prefix support to forwarded sass built-ins --- .../__tests__/do-complete-modules.test.ts | 1 + .../src/features/do-complete.ts | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/language-services/src/features/__tests__/do-complete-modules.test.ts b/packages/language-services/src/features/__tests__/do-complete-modules.test.ts index 7f49a690..069b54a5 100644 --- a/packages/language-services/src/features/__tests__/do-complete-modules.test.ts +++ b/packages/language-services/src/features/__tests__/do-complete-modules.test.ts @@ -135,6 +135,7 @@ test("suggest sass built-ins that are forwarded with a prefix", async () => { }, }, ); + assert.ok(items.find((a) => a.label === "math-clamp")); }); test("should suggest symbol from a different document via @use", async () => { diff --git a/packages/language-services/src/features/do-complete.ts b/packages/language-services/src/features/do-complete.ts index 03c2c3bf..0965dfc4 100644 --- a/packages/language-services/src/features/do-complete.ts +++ b/packages/language-services/src/features/do-complete.ts @@ -755,6 +755,7 @@ export class DoComplete extends LanguageFeature { document, context, docs, + link.as ? prefix + link.as : "", ); items.push(...suggestions); } @@ -1072,12 +1073,25 @@ export class DoComplete extends LanguageFeature { document: TextDocument, context: CompletionContext, moduleDocs: SassBuiltInModule, + prefix: string = "", ): CompletionItem[] { const items: CompletionItem[] = []; for (const [name, docs] of Object.entries(moduleDocs.exports)) { const { description, signature, parameterSnippet, returns } = docs; + const kind = signature + ? CompletionItemKind.Function + : CompletionItemKind.Variable; + + let label = name; + if (kind === CompletionItemKind.Variable) { + // Avoid ending up with namespace.prefix-$variable + label = `$${prefix}${asDollarlessVariable(name)}`; + } else { + label = `${prefix}${name}`; + } + // Client needs the namespace as part of the text that is matched, - const filterText = `${context.namespace}.${name}`; + const filterText = `${context.namespace}.${label}`; // Inserted text needs to include the `.` which will otherwise // be replaced (except when we're embedded in Vue, Svelte or Astro). @@ -1085,10 +1099,10 @@ export class DoComplete extends LanguageFeature { const isEmbedded = this.isEmbedded(document); const insertText = context.currentWord.includes(".") - ? `${isEmbedded ? "" : "."}${name}${ + ? `${isEmbedded ? "" : "."}${label}${ signature ? `(${parameterSnippet})` : "" }` - : name; + : label; items.push({ documentation: { @@ -1103,7 +1117,7 @@ export class DoComplete extends LanguageFeature { kind: signature ? CompletionItemKind.Function : CompletionItemKind.Variable, - label: name, + label, labelDetails: { detail: signature && returns ? `${signature} => ${returns}` : signature,