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

fix: add prefix support to forwarded sass built-ins #202

Merged
merged 2 commits into from
Aug 11, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,51 @@ test("suggest sass built-ins that are forwarded by the stylesheet that is @used"
);
});

test("suggest sass built-ins that are forwarded with a prefix", async () => {
const one = fileSystemProvider.createDocument(
['@forward "sass:math" as math-*;'],
{
uri: "test.scss",
},
);
const two = fileSystemProvider.createDocument([
'@use "./test";',
"$var: test.",
]);

// emulate scanner of language service which adds workspace documents to the cache
ls.parseStylesheet(one);
ls.parseStylesheet(two);

const { items } = await ls.doComplete(two, Position.create(1, 11));
assert.notEqual(
items.length,
0,
"Expected to get completions from the sass:math module",
);

// Quick sampling of the results
assert.deepStrictEqual(
items.find((annotation) => annotation.label === "$math-pi"),
{
documentation: {
kind: "markdown",
value:
"The value of the mathematical constant **π**.\n\n[Sass documentation](https://sass-lang.com/documentation/modules/math#$pi)",
},
filterText: "test.$math-pi",
insertText: ".$math-pi",
insertTextFormat: InsertTextFormat.PlainText,
kind: CompletionItemKind.Variable,
label: "$math-pi",
labelDetails: {
detail: undefined,
},
},
);
assert.ok(items.find((a) => a.label === "math-clamp"));
});

test("should suggest symbol from a different document via @use", async () => {
const one = fileSystemProvider.createDocument("$primary: limegreen;", {
uri: "one.scss",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test("should suggest variable in @return", async () => {
const { items } = await ls.doComplete(one, Position.create(3, 40));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare")); // allow for recursion
assert.ok(items.find((item) => item.label === "compare")); // allow for recursion
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -160,7 +160,7 @@ test("should suggest function in @return", async () => {
const { items } = await ls.doComplete(one, Position.create(3, 39));

assert.ok(items.find((item) => item.label === "compare")); // allow for recursion
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -186,7 +186,7 @@ test("should suggest variable in @if", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 5));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -212,7 +212,7 @@ test("should suggest function in @if", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 4));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -239,7 +239,7 @@ test("should suggest variable in @else if", async () => {
const { items } = await ls.doComplete(one, Position.create(5, 12));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -266,7 +266,7 @@ test("should suggest function in @else if", async () => {
const { items } = await ls.doComplete(one, Position.create(5, 12));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand Down Expand Up @@ -315,7 +315,7 @@ test("should suggest variable in for @each $foo in", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 15));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -341,7 +341,7 @@ test("should suggest function in for @each $foo in", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 15));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand Down Expand Up @@ -390,7 +390,7 @@ test("should suggest variable in @for $i from ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 15));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -416,7 +416,7 @@ test("should suggest function in @for $i from ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 15));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -442,7 +442,7 @@ test("should suggest variable @for $i from 1 to ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 19));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -468,7 +468,7 @@ test("should suggest function @for $i from 1 to ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 23));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -494,7 +494,7 @@ test("should suggest variable in @for $i from 1 through ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 24));

assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "compare"));
assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand All @@ -520,7 +520,7 @@ test("should suggest function in @for $i from 1 through ", async () => {
const { items } = await ls.doComplete(one, Position.create(4, 24));

assert.ok(items.find((item) => item.label === "compare"));
assert.isUndefined(items.find((item) => item.label === "$name"));
assert.ok(items.find((item) => item.label === "$name"));
assert.isUndefined(items.find((item) => item.label === "mixin"));
assert.isUndefined(items.find((item) => item.label === "%placeholder"));
});
Expand Down
64 changes: 39 additions & 25 deletions packages/language-services/src/features/do-complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,23 +447,29 @@ export class DoComplete extends LanguageFeature {
reError.test(lineBeforePosition) ||
reWarn.test(lineBeforePosition);

if ((isControlFlow || isPropertyValue) && !isEmptyValue && !isQuotes) {
if (context.namespace && currentWord.endsWith(".")) {
if (isControlFlow) {
context.isVariableContext = true;
context.isFunctionContext = true;
return context;
}

if (reMixinReference.test(lineBeforePosition)) {
context.isMixinContext = true;
if (reCompletedMixinWithParametersReference.test(lineBeforePosition)) {
context.isMixinContext = false;
context.isVariableContext = true;
} else {
context.isVariableContext = currentWord.includes("$");
context.isFunctionContext = true;
}
} else if (isQuotes) {
context.isVariableContext = isInterpolation;
} else {
context.isVariableContext =
currentWord.startsWith("$") || isInterpolation || isEmptyValue;
return context;
}

if ((isControlFlow || isPropertyValue) && !isEmptyValue && !isQuotes) {
if (isPropertyValue && !isEmptyValue && !isQuotes) {
if (context.namespace) {
context.isFunctionContext = true;
context.isVariableContext = true;
} else {
context.isVariableContext = currentWord.includes("$");

const lastChar = lineBeforePosition.charAt(
lineBeforePosition.length - 1,
);
Expand All @@ -475,18 +481,12 @@ export class DoComplete extends LanguageFeature {
}
}
} else if (isQuotes) {
context.isVariableContext = isInterpolation;
context.isFunctionContext = isInterpolation;
} else if (isPropertyValue && isEmptyValue) {
context.isFunctionContext = true;
}

if (!isPropertyValue && reMixinReference.test(lineBeforePosition)) {
context.isMixinContext = true;
if (reCompletedMixinWithParametersReference.test(lineBeforePosition)) {
context.isMixinContext = false;
context.isVariableContext = true;
context.isFunctionContext = true;
}
} else {
context.isVariableContext =
currentWord.startsWith("$") || isInterpolation || isEmptyValue;
context.isFunctionContext = isPropertyValue && isEmptyValue;
}

return context;
Expand Down Expand Up @@ -755,6 +755,7 @@ export class DoComplete extends LanguageFeature {
document,
context,
docs,
link.as ? prefix + link.as : "",
);
items.push(...suggestions);
}
Expand Down Expand Up @@ -1072,23 +1073,36 @@ 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).
// Example result: .floor(${1:number})
const isEmbedded = this.isEmbedded(document);

const insertText = context.currentWord.includes(".")
? `${isEmbedded ? "" : "."}${name}${
? `${isEmbedded ? "" : "."}${label}${
signature ? `(${parameterSnippet})` : ""
}`
: name;
: label;

items.push({
documentation: {
Expand All @@ -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,
Expand Down
Loading