Skip to content

Signature help turns off current-parameter display for non-trailing rest parameters #42592

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

Merged
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
23 changes: 17 additions & 6 deletions src/services/signatureHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ namespace ts.SignatureHelp {
if (argumentIndex !== 0) {
Debug.assertLessThan(argumentIndex, argumentCount);
}

let selectedItemIndex = 0;
let itemsSeen = 0;
for (let i = 0; i < items.length; i++) {
Expand All @@ -541,8 +540,19 @@ namespace ts.SignatureHelp {
}

Debug.assert(selectedItemIndex !== -1); // If candidates is non-empty it should always include bestSignature. We check for an empty candidates before calling this function.

return { items: flatMapToMutable(items, identity), applicableSpan, selectedItemIndex, argumentIndex, argumentCount };
const help = { items: flatMapToMutable(items, identity), applicableSpan, selectedItemIndex, argumentIndex, argumentCount };
const selected = help.items[selectedItemIndex];
if (selected.isVariadic) {
const firstRest = findIndex(selected.parameters, p => !!p.isRest);
if (-1 < firstRest && firstRest < selected.parameters.length - 1) {
// We don't have any code to get this correct; instead, don't highlight a current parameter AT ALL
help.argumentIndex = selected.parameters.length;
Comment on lines +546 to +549
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also make this conditional upon argumentIndex >= firstRest if we wanted parameter highlighting to work up until the first rest, where the ambiguity begins. But maybe that would be a worse experience than consistently seeing no highlighting. I personally feel like parameter highlighting is pretty useful when parameters have JSDoc description, and it might be worthwhile to show them as often as we can. But it might be pretty confusing for it to stop working halfway through typing call arguments. Don’t have a super strong opinion here.

}
else {
help.argumentIndex = Math.min(help.argumentIndex, selected.parameters.length - 1);
}
}
return help;
}

function createTypeHelpItems(
Expand Down Expand Up @@ -638,15 +648,16 @@ namespace ts.SignatureHelp {
const param = checker.symbolToParameterDeclaration(parameter, enclosingDeclaration, signatureHelpNodeBuilderFlags)!;
printer.writeNode(EmitHint.Unspecified, param, sourceFile, writer);
});
const isOptional = checker.isOptionalParameter(<ParameterDeclaration>parameter.valueDeclaration);
return { name: parameter.name, documentation: parameter.getDocumentationComment(checker), displayParts, isOptional };
const isOptional = checker.isOptionalParameter(parameter.valueDeclaration as ParameterDeclaration);
const isRest = !!((parameter as TransientSymbol).checkFlags & CheckFlags.RestParameter);
return { name: parameter.name, documentation: parameter.getDocumentationComment(checker), displayParts, isOptional, isRest };
}

function createSignatureHelpParameterForTypeParameter(typeParameter: TypeParameter, checker: TypeChecker, enclosingDeclaration: Node, sourceFile: SourceFile, printer: Printer): SignatureHelpParameter {
const displayParts = mapToDisplayParts(writer => {
const param = checker.typeParameterToDeclaration(typeParameter, enclosingDeclaration, signatureHelpNodeBuilderFlags)!;
printer.writeNode(EmitHint.Unspecified, param, sourceFile, writer);
});
return { name: typeParameter.symbol.name, documentation: typeParameter.symbol.getDocumentationComment(checker), displayParts, isOptional: false };
return { name: typeParameter.symbol.name, documentation: typeParameter.symbol.getDocumentationComment(checker), displayParts, isOptional: false, isRest: false };
}
}
1 change: 1 addition & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ namespace ts {
documentation: SymbolDisplayPart[];
displayParts: SymbolDisplayPart[];
isOptional: boolean;
isRest?: boolean;
}

export interface SelectionRange {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6027,6 +6027,7 @@ declare namespace ts {
documentation: SymbolDisplayPart[];
displayParts: SymbolDisplayPart[];
isOptional: boolean;
isRest?: boolean;
}
interface SelectionRange {
textSpan: TextSpan;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6027,6 +6027,7 @@ declare namespace ts {
documentation: SymbolDisplayPart[];
displayParts: SymbolDisplayPart[];
isOptional: boolean;
isRest?: boolean;
}
interface SelectionRange {
textSpan: TextSpan;
Expand Down
32 changes: 32 additions & 0 deletions tests/cases/fourslash/signatureHelpLeadingRestTuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path='fourslash.ts' />

////export function leading(...args: [...names: string[], allCaps: boolean]): void {
////}
////
////leading(/*1*/);
////leading("ok", /*2*/);
////leading("ok", "ok", /*3*/);

verify.signatureHelp(
{
marker: "1",
text: "leading(...names: string[], allCaps: boolean): void",
overloadsCount: 1,
parameterCount: 2,
isVariadic: true,
},
{
marker: "2",
text: "leading(...names: string[], allCaps: boolean): void",
overloadsCount: 1,
parameterCount: 2,
isVariadic: true,
},
{
marker: "3",
text: "leading(...names: string[], allCaps: boolean): void",
overloadsCount: 1,
parameterCount: 2,
isVariadic: true,
},
);
38 changes: 38 additions & 0 deletions tests/cases/fourslash/signatureHelpTrailingRestTuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path='fourslash.ts' />

////export function leading(allCaps: boolean, ...names: string[]): void {
////}
////
////leading(/*1*/);
////leading(false, /*2*/);
////leading(false, "ok", /*3*/);

verify.signatureHelp(
{
marker: "1",
text: "leading(allCaps: boolean, ...names: string[]): void",
overloadsCount: 1,
parameterCount: 2,
parameterName: "allCaps",
parameterSpan: "allCaps: boolean",
isVariadic: true,
},
{
marker: "2",
text: "leading(allCaps: boolean, ...names: string[]): void",
overloadsCount: 1,
parameterCount: 2,
parameterName: "names",
parameterSpan: "...names: string[]",
isVariadic: true,
},
{
marker: "3",
text: "leading(allCaps: boolean, ...names: string[]): void",
overloadsCount: 1,
parameterCount: 2,
parameterName: "names",
parameterSpan: "...names: string[]",
isVariadic: true,
},
);