Skip to content

Commit

Permalink
fix: signature help no longer flickers on and off
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Sep 28, 2024
1 parent 6a518ba commit 140f6e4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ test("signature help when given more parameters than are supported", async () =>
});
const result = await ls.doSignatureHelp(document, Position.create(0, 18));

assert.deepStrictEqual(result, null);
assert.deepStrictEqual(result, {
activeParameter: 2,
activeSignature: 0,
signatures: [],
});
});

test("is not confused by using a function as a parameter", async () => {
Expand Down Expand Up @@ -557,3 +561,69 @@ test("provides signature help for sass built-ins with named parameters in signat
activeSignature: 0,
});
});

test("provides signature help with numeric parameter", async () => {
const document = fileSystemProvider.createDocument(
[
"@use 'sass:color';",
".foo {",
" $_primary: #303030;",
" background-color: color.adjust($_primary, 100, 1);",
"}",
],
{ uri: "builtins.scss" },
);

const help = await ls.doSignatureHelp(document, Position.create(3, 50));

assert.deepStrictEqual(help, {
signatures: [
{
documentation: {
kind: "markdown",
value:
"Increases or decreases one or more properties of `$color` by fixed amounts. All optional arguments must be numbers.\n\nIt's an error to specify an RGB property at the same time as an HSL property, or either of those at the same time as an HWB property.\n\n[Sass reference](https://sass-lang.com/documentation/modules/color#adjust)",
},
label:
"adjust($color, $red: null, $green: null, $blue: null, $hue: null, $saturation: null, $lightness: null, $whiteness: null, $blackness: null, $alpha: null, $space: null)",
parameters: [
{
label: "$color",
},
{
label: "$red",
},
{
label: "$green",
},
{
label: "$blue",
},
{
label: "$hue",
},
{
label: "$saturation",
},
{
label: "$lightness",
},
{
label: "$whiteness",
},
{
label: "$blackness",
},
{
label: "$alpha",
},
{
label: "$space",
},
],
},
],
activeParameter: 2,
activeSignature: 0,
});
});
40 changes: 29 additions & 11 deletions packages/language-services/src/features/do-signature-help.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNodeAtOffset } from "@somesass/vscode-css-languageservice";
import { getNodeAtOffset, Range } from "@somesass/vscode-css-languageservice";
import { sassBuiltInModules } from "../facts/sass";
import { LanguageFeature } from "../language-feature";
import {
Expand Down Expand Up @@ -33,25 +33,39 @@ export class DoSignatureHelp extends LanguageFeature {
node.type !== NodeType.Function &&
node.type !== NodeType.MixinReference
) {
if (
!node.parent ||
(node.parent.type !== NodeType.Function &&
node.parent.type !== NodeType.MixinReference)
) {
const parent = node.findAParent(
NodeType.Function,
NodeType.MixinReference,
);
if (!parent) {
return null;
}

node = node.parent as Function | MixinReference;
node = parent as Function | MixinReference;
}

const result: SignatureHelp = {
const result: Required<SignatureHelp> = {
activeSignature: 0,
activeParameter: 0,
signatures: [],
};
const identifier = node.getIdentifier()!.getText();
const parameters = node.getArguments().getChildren();
result.activeParameter = parameters.length;
if (parameters.length) {
result.activeParameter = parameters.length - 1;

// Figure out how to se if we have a , after the last parameter. If so, add one to result.activeParameter.
const lastParamEndOffset = parameters[parameters.length - 1].end;
const lastParamEndPosition = document.positionAt(lastParamEndOffset);
const characterAfterLastParam = document.getText(
Range.create(lastParamEndPosition, {
line: lastParamEndPosition.line,
character: lastParamEndPosition.character + 1,
}),
);
if (characterAfterLastParam === ",") {
result.activeParameter = result.activeParameter + 1;
}
}

const definition = await this.ls.findDefinition(
document,
Expand All @@ -63,7 +77,11 @@ export class DoSignatureHelp extends LanguageFeature {
if (!symbol) return result;

const allParameters = getParametersFromDetail(symbol.detail);
if (allParameters.length >= (result.activeParameter || 0)) {
// activeParameter is 0 index
if (
allParameters.length === 0 ||
allParameters.length > result.activeParameter
) {
const signatureInfo = SignatureInformation.create(
`${identifier}${symbol.detail || "()"}`,
);
Expand Down

0 comments on commit 140f6e4

Please sign in to comment.