Skip to content

Commit

Permalink
fix: completions bug where symbols in first document weren't included
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Jul 29, 2024
1 parent 1333e13 commit c7a9342
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const ls = getLanguageService({ fileSystemProvider, ...rest });

beforeEach(() => {
ls.clearCache();
ls.configure({}); // Reset any configuration to default
ls.configure({
completionSettings: {
suggestFromUseOnly: true,
},
}); // Reset any configuration to default
});

test("suggests built-in sass modules", async () => {
Expand Down Expand Up @@ -84,6 +88,62 @@ test("should suggest symbol from a different document via @use", async () => {
);
});

test("should suggest symbols from the document we use when it also forwards another document with symbols", async () => {
const one = fileSystemProvider.createDocument("$primary: limegreen;", {
uri: "one.scss",
});
const two = fileSystemProvider.createDocument(
['@forward "./one";', "$secondary: red;"],
{
uri: "two.scss",
},
);
const three = fileSystemProvider.createDocument(
['@use "./two";', ".a { color: two."],
{
uri: "three.scss",
},
);

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

const { items } = await ls.doComplete(three, Position.create(1, 16));
assert.notEqual(
0,
items.length,
"Expected to find a completion item for $primary",
);
assert.deepStrictEqual(
items.find((annotation) => annotation.label === "$secondary"),
{
commitCharacters: [";", ","],
documentation: "red\n____\nVariable declared in two.scss",
filterText: "two.$secondary",
insertText: ".$secondary",
kind: CompletionItemKind.Color,
label: "$secondary",
sortText: undefined,
tags: [],
},
);
assert.deepStrictEqual(
items.find((annotation) => annotation.label === "$primary"),
{
commitCharacters: [";", ","],
documentation: "limegreen\n____\nVariable declared in one.scss",
filterText: "two.$primary",
insertText: ".$primary",
kind: CompletionItemKind.Color,
label: "$primary",
sortText: undefined,
tags: [],
},
);
});

test("should not suggest symbols from a module used by the one we use", async () => {
ls.configure({
completionSettings: {
Expand Down Expand Up @@ -887,6 +947,11 @@ test("given both required and optional parameters should suggest two variants of
});

test("should suggest all symbols as legacy @import may be in use", async () => {
ls.configure({
completionSettings: {
suggestFromUseOnly: false,
},
});
const one = fileSystemProvider.createDocument("$primary: limegreen;", {
uri: "one.scss",
});
Expand Down
10 changes: 9 additions & 1 deletion packages/language-services/src/language-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ export abstract class LanguageFeature {
}

let result: T[] = [];

// gather the results from the initial document if any
if (Array.isArray(callbackResult)) {
result.push(...callbackResult);
} else if (callbackResult) {
result.push(callbackResult);
}

for (const link of links) {
if (!link.target || link.target === currentDocument.uri) {
continue;
Expand Down Expand Up @@ -244,7 +252,7 @@ export abstract class LanguageFeature {
visited,
depth + 1,
);
result = result.concat(linkResult);
result.push(...linkResult);
}

return result;
Expand Down

0 comments on commit c7a9342

Please sign in to comment.