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

Allow signature help argument count to be equal to argument index #58203

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/services/signatureHelp.ts
Expand Up @@ -287,9 +287,6 @@ function getArgumentOrParameterListInfo(node: Node, position: number, sourceFile
const { list, argumentIndex } = info;

const argumentCount = getArgumentCount(checker, list);
if (argumentIndex !== 0) {
Debug.assertLessThan(argumentIndex, argumentCount);
}
Comment on lines -290 to -292
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While it might look weird that this allows what usually isn't allowed - it all depends on the definition of those values. I think it makes sense to treat them as follows:

  • argument index - a min argument index at the position
  • argument count - a minimum count of known arguments

A spread element doesn't always increase such an argument count but the argument index at its positions gets increased

Copy link
Member

Choose a reason for hiding this comment

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

But what should an editor do when the argument index exceeds the count? The index has to be capped, doesn't it?

Copy link
Member

Choose a reason for hiding this comment

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

Out of bounds indexes are just ignored; on Python I did this intentionally, too (previously used a negative number until some rust-based LSP clients started getting picky). See: microsoft/language-server-protocol#1271

But, I'm more confused by how a spread element would change the number here; we're definitely indexing them correctly left to right, correct? Like if we have (x, {y, z}), we don't go out of bounds on z because it's "at index 2" but the number of real params is 2? I assume not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But what should an editor do when the argument index exceeds the count? The index has to be capped, doesn't it?

The added baseline here shows how we can still highlight the parameter in the signature - at least in this case 😉

But, I'm more confused by how a spread element would change the number here

function test(...args: any[]) {}

const twoNums = [2, 3] as const
test(1, ...twoNums)

In the situation above the rest adds 2 to the count (and not 1). See the PR that changed this: #56372

Choose a reason for hiding this comment

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

Nitpick: That's a spread, not a rest. Rest is what you do when destructuring, spread is what you do at construction ("rest" doesn't make sense in that context because you can have multiple).

const argumentsSpan = getApplicableSpanForArguments(list, sourceFile);
return { list, argumentIndex, argumentCount, argumentsSpan };
}
Expand Down Expand Up @@ -660,9 +657,6 @@ function createSignatureHelpItems(
const callTargetDisplayParts = callTargetSymbol ? symbolToDisplayParts(typeChecker, callTargetSymbol, useFullPrefix ? sourceFile : undefined, /*meaning*/ undefined) : emptyArray;
const items = map(candidates, candidateSignature => getSignatureHelpItem(candidateSignature, callTargetDisplayParts, isTypeParameterList, typeChecker, enclosingDeclaration, sourceFile));

if (argumentIndex !== 0) {
Debug.assertLessThan(argumentIndex, argumentCount);
}
let selectedItemIndex = 0;
let itemsSeen = 0;
for (let i = 0; i < items.length; i++) {
Expand Down