-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Align inlay hints with trailing tuple arguments #64410
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
Open
Jake Bailey (jakebailey)
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
jakebailey:inlay-hints-tuple-rest-reuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a079981
Test inlay hints with trailing tuple rest elements
jakebailey 755ff35
Align inlay hints with trailing tuple arguments
jakebailey 96b101a
Test inlay hints with multiple variadic tuple elements
jakebailey edf2f7b
Align trailing tuple hints after generic spreads
jakebailey 6bc1039
Test trailing tuple hints after spreads
jakebailey 02b185f
Avoid incorrect trailing tuple hints after spreads
jakebailey 3f6f1b8
Test ambiguous tuple spread parameter hints
jakebailey c6ca73c
Omit uncertain hints after tuple spreads
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tsc/internal/fourslash/tests/inlayHintsRestTrailingSpreadEdgeCases_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/TypeScript/tsc/internal/fourslash" | ||
| "github.com/microsoft/TypeScript/tsc/internal/ls/lsutil" | ||
| "github.com/microsoft/TypeScript/tsc/internal/testutil" | ||
| ) | ||
|
|
||
| func TestInlayHintsOptionalTrailingTupleAfterSpread(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `declare function optional<T extends unknown[]>(...args: [...head: T, first?: number, second?: string]): void; | ||
| function forward<T extends unknown[]>(x: T) { | ||
| optional<T>(...x, 1); | ||
| optional<T>(...x, 1, "two"); | ||
| }` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyNoErrors(t) | ||
| f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{InlayHints: lsutil.InlayHintsPreferences{IncludeInlayParameterNameHints: lsutil.IncludeInlayParameterNameHintsAll}}) | ||
| } | ||
|
|
||
| func TestInlayHintsSingleRestWithSpreadAndSuffix(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `declare function f(...args: [...head: string[], tail: string]): void; | ||
| declare const xs: string[]; | ||
| f(...xs, "middle", "end");` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyNoErrors(t) | ||
| f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{InlayHints: lsutil.InlayHintsPreferences{IncludeInlayParameterNameHints: lsutil.IncludeInlayParameterNameHintsAll}}) | ||
| } | ||
|
|
||
| func TestInlayHintsOptionalTrailingAfterTupleSpread(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `declare function optional<T extends unknown[]>(...args: [...head: T, first?: number, second?: string]): void; | ||
| function forward<T extends unknown[]>(x: [...T]) { | ||
| optional<T>(...x, 1); | ||
| }` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyNoErrors(t) | ||
| f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{InlayHints: lsutil.InlayHintsPreferences{IncludeInlayParameterNameHints: lsutil.IncludeInlayParameterNameHintsAll}}) | ||
| } | ||
|
|
||
| func TestInlayHintsOptionalParameterAfterTupleSpread(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `declare function g(a: number, b?: number, ...rest: string[]): void; | ||
| declare const x: [number, number?]; | ||
| g(...x, "end");` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyNoErrors(t) | ||
| f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{InlayHints: lsutil.InlayHintsPreferences{IncludeInlayParameterNameHints: lsutil.IncludeInlayParameterNameHintsAll}}) | ||
| } |
32 changes: 32 additions & 0 deletions
32
tsc/internal/fourslash/tests/inlayHintsTupleRestTrailing_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/TypeScript/tsc/internal/fourslash" | ||
| "github.com/microsoft/TypeScript/tsc/internal/ls/lsutil" | ||
| "github.com/microsoft/TypeScript/tsc/internal/testutil" | ||
| ) | ||
|
|
||
| func TestInlayHintsTupleRestTrailing(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `declare function fn(prefix: boolean, ...args: [first: number, ...middle: string[], last: string]): void; | ||
| fn(true, 1, "a", "b", "end"); | ||
| fn(true, 1, "end"); | ||
| declare function onlyTrailing(...args: [...head: number[], tail: string]): void; | ||
| onlyTrailing(1, 2, "end"); | ||
| onlyTrailing("end"); | ||
| declare function noTrailing(...args: [first: number, ...rest: string[]]): void; | ||
| noTrailing(1, "a", "b"); | ||
| declare function multi<T extends string[], U extends string[]>(...args: [first: number, ...left: T, ...right: U, penultimate: string, last: boolean]): void; | ||
| function combined<T extends string[], U extends string[]>(middle: [...T, ...U]) { | ||
| multi<T, U>(1, ...middle, "end", true); | ||
| } | ||
| declare function skip({ x }: { x: number }, next: number): void; | ||
| skip({ x: 1 }, 2);` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyNoErrors(t) | ||
| f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{InlayHints: lsutil.InlayHintsPreferences{IncludeInlayParameterNameHints: lsutil.IncludeInlayParameterNameHintsAll}}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ package ls | |
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "strings" | ||
| "unicode" | ||
|
|
||
|
|
@@ -157,37 +156,48 @@ func (s *inlayHintState) visitCallOrNewExpression(expr *ast.CallOrNewExpression) | |
| return | ||
| } | ||
|
|
||
| // A spread makes the effective argument count unknown, but explicit arguments after the last | ||
| // spread still have a known offset from the end. | ||
| argumentCount := len(args) | ||
| lastSpreadIndex := -1 | ||
| for i, arg := range args { | ||
| if ast.IsSpreadElement(ast.SkipParentheses(arg)) { | ||
| argumentCount = -1 | ||
| lastSpreadIndex = i | ||
| } | ||
| } | ||
| signatureParamPos := 0 | ||
| for _, originalArg := range args { | ||
| hasUncertainTupleSpread := false | ||
| for i, originalArg := range args { | ||
| arg := ast.SkipParentheses(originalArg) | ||
| if shouldShowLiteralParameterNameHintsOnly(s.preferences) && !isHintableLiteral(arg) { | ||
| signatureParamPos++ | ||
| continue | ||
| } | ||
|
|
||
| spreadArgs := 0 | ||
| skipSpreadHint := false | ||
| if ast.IsSpreadElement(arg) { | ||
| spreadType := s.checker.GetTypeAtLocation(arg.Expression()) | ||
| if spreadType.IsTupleType() { | ||
| elementFlags := spreadType.Target().AsTupleType().ElementFlags() | ||
| fixedLength := spreadType.Target().AsTupleType().FixedLength() | ||
| if fixedLength == 0 { | ||
| continue | ||
| } | ||
| firstOptionalIndex := slices.IndexFunc(elementFlags, func(f checker.ElementFlags) bool { | ||
| return f&checker.ElementFlagsRequired == 0 | ||
| }) | ||
| requiredArgs := core.IfElse(firstOptionalIndex < 0, fixedLength, firstOptionalIndex) | ||
| if requiredArgs > 0 { | ||
| spreadArgs = requiredArgs | ||
| } | ||
| tupleType := spreadType.Target().AsTupleType() | ||
| spreadArgs = getRequiredTupleElementCount(tupleType) | ||
| // Optional or variable tuple elements make subsequent positional hints ambiguous. | ||
| hasUncertainTupleSpread = hasUncertainTupleSpread || spreadArgs < len(tupleType.ElementInfos()) | ||
| skipSpreadHint = tupleType.FixedLength() == 0 | ||
| } | ||
| } | ||
| if shouldShowLiteralParameterNameHintsOnly(s.preferences) && !isHintableLiteral(arg) { | ||
| signatureParamPos++ | ||
| continue | ||
| } | ||
| if skipSpreadHint { | ||
| continue | ||
| } | ||
|
|
||
| identifierInfo := s.getParameterIdentifierInfoAtPosition(signature, signatureParamPos) | ||
| offsetFromEnd := -1 | ||
| if lastSpreadIndex >= 0 && i > lastSpreadIndex { | ||
| offsetFromEnd = len(args) - i | ||
| } | ||
| identifierInfo := s.getParameterIdentifierInfoAtPosition(signature, signatureParamPos, argumentCount, offsetFromEnd, hasUncertainTupleSpread && !ast.IsSpreadElement(arg)) | ||
| signatureParamPos = signatureParamPos + core.IfElse(spreadArgs > 0, spreadArgs, 1) | ||
| if identifierInfo == nil { | ||
| return | ||
| continue | ||
| } | ||
|
|
||
| parameter := identifierInfo.parameter | ||
|
|
@@ -831,10 +841,13 @@ type parameterInfo struct { | |
| isRestParameter bool | ||
| } | ||
|
|
||
| func (s *inlayHintState) getParameterIdentifierInfoAtPosition(signature *checker.Signature, pos int) *parameterInfo { | ||
| func (s *inlayHintState) getParameterIdentifierInfoAtPosition(signature *checker.Signature, pos int, argumentCount int, offsetFromEnd int, uncertainTupleSpread bool) *parameterInfo { | ||
| parameters := signature.Parameters() | ||
| paramCount := len(parameters) - core.IfElse(signature.HasRestParameter(), 1, 0) | ||
| if pos < paramCount { | ||
| if uncertainTupleSpread { | ||
| return nil | ||
| } | ||
| param := parameters[pos] | ||
| paramId := getParameterDeclarationIdentifier(param) | ||
| if paramId == nil { | ||
|
|
@@ -859,14 +872,36 @@ func (s *inlayHintState) getParameterIdentifierInfoAtPosition(signature *checker | |
|
|
||
| restType := s.checker.GetTypeOfSymbol(restParameter) | ||
| if restType.IsTupleType() { | ||
| associatedNames := make([]*ast.Node, 0, len(restType.Target().AsTupleType().ElementInfos())) | ||
| for _, elementInfo := range restType.Target().AsTupleType().ElementInfos() { | ||
| labeledElement := elementInfo.LabeledDeclaration() | ||
| associatedNames = append(associatedNames, labeledElement) | ||
| } | ||
| tupleType := restType.Target().AsTupleType() | ||
| elementInfos := tupleType.ElementInfos() | ||
| index := pos - paramCount | ||
| if index < len(associatedNames) { | ||
| associatedName := associatedNames[index] | ||
| restArgumentCount := argumentCount - paramCount | ||
| firstVariableIndex := tupleType.FixedLength() | ||
| trailingCount := checker.GetEndElementCount(tupleType, checker.ElementFlagsFixed) | ||
| // Optional trailing elements may be omitted, so only required ones can be aligned from the end. | ||
| requiredTrailingCount := checker.GetEndElementCount(tupleType, checker.ElementFlagsRequired) | ||
| if uncertainTupleSpread && (offsetFromEnd <= 0 || offsetFromEnd > requiredTrailingCount) { | ||
| return nil | ||
| } | ||
| variableCount := len(elementInfos) - firstVariableIndex - trailingCount | ||
| if trailingCount > 0 && variableCount > 0 { | ||
| switch { | ||
| case offsetFromEnd > 0 && offsetFromEnd <= requiredTrailingCount: | ||
| index = len(elementInfos) - offsetFromEnd | ||
| case offsetFromEnd > trailingCount: | ||
| return nil | ||
| case argumentCount >= 0 && restArgumentCount >= firstVariableIndex+trailingCount: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have mostly been able to track this stuff, but I'm starting to find it hard to keep track of in this branch. Maybe you could add some comments at each branch to show concrete situations that we're trying to handle? |
||
| trailingStart := restArgumentCount - trailingCount | ||
| switch { | ||
| case index >= trailingStart: | ||
| index = len(elementInfos) - (restArgumentCount - index) | ||
| case index >= firstVariableIndex && (variableCount > 1 || index > firstVariableIndex): | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
| if index < len(elementInfos) { | ||
| associatedName := elementInfos[index].LabeledDeclaration() | ||
| if associatedName != nil { | ||
| debug.Assert(ast.IsIdentifier(associatedName.Name())) | ||
| var isRestTupleElement bool | ||
|
|
@@ -886,6 +921,9 @@ func (s *inlayHintState) getParameterIdentifierInfoAtPosition(signature *checker | |
| return nil | ||
| } | ||
|
|
||
| if uncertainTupleSpread { | ||
| return nil | ||
| } | ||
| if pos == paramCount { | ||
| return ¶meterInfo{ | ||
| parameter: restId, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...lines/reference/fourslash/inlayHints/inlayHintsOptionalParameterAfterTupleSpread.baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // === Inlay Hints === | ||
| g(...x, "end"); | ||
| ^ | ||
| { | ||
| "position": { | ||
| "line": 2, | ||
| "character": 2 | ||
| }, | ||
| "label": [ | ||
| { | ||
| "value": "a", | ||
| "location": { | ||
| "uri": "file:///inlayHintsOptionalParameterAfterTupleSpread.ts", | ||
| "range": { | ||
| "start": { | ||
| "line": 0, | ||
| "character": 19 | ||
| }, | ||
| "end": { | ||
| "line": 0, | ||
| "character": 20 | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "value": ":" | ||
| } | ||
| ], | ||
| "kind": 2, | ||
| "paddingRight": true | ||
| } |
2 changes: 2 additions & 0 deletions
2
...elines/reference/fourslash/inlayHints/inlayHintsOptionalTrailingAfterTupleSpread.baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // === Inlay Hints === | ||
| === No inlay hints === |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
trailingFixedCount