-
Notifications
You must be signed in to change notification settings - Fork 763
Fix completions crash and enable contextual type completions in array literals #2258
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
Conversation
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
|
This might be alright, but I don't know if this is the right layer to do this in. Maybe this is more of a language service thing to work off of? Is there some sort of missing element we can use instead of the Also, it does reveal an undesirable behavior in both the new and old codebases. // @filename: a.ts
let x: "foo" | "bar" = /*a*/;
// @filename: b.ts
let y: ["foo" | "bar", string] = [/*b*/];If you request completions at In the old codebase (and I assume with this fix?), you will not get these suggested at |
|
@copilot try to address. |
I'll address this by adding special handling for Working on this now.
Fixed in 90ca278 by handling
Both issues you mentioned are now resolved. |
… handling Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
|
Why does this need to differ so much from the original one in Strada? |
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.
Pull request overview
This PR fixes a crash in completions when requesting completions inside new Map([...]) constructors and similar array literal contexts. The crash occurred because non-element tokens (like [ and ,) were being passed to the checker's getContextualTypeForElementExpression, which expects actual array element nodes and uses IndexOfNode that returns -1 for non-elements, causing an index out of range panic.
Key changes:
- Added special handling for
OpenBracketTokenandCommaTokenin array literals to compute contextual types at the token level - Created
GetContextualTypeForArrayElementwrapper function to enable language service access to contextual type resolution for array elements - Added comprehensive tests validating both the crash fix and new string literal completion functionality
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/ls/completions.go | Added OpenBracketToken and CommaToken cases to handle array literal completions, preventing invalid tokens from reaching checker's contextual type resolution |
| internal/checker/services.go | Added exported GetContextualTypeForArrayElement function to enable language service to resolve contextual types for array elements at specific indices |
| internal/fourslash/tests/manual/completionsInMapConstructorNoCrash_test.go | Test verifying completions no longer crash in Map constructor array literals at problematic positions |
| internal/fourslash/tests/manual/completionsInArrayLiteralWithContextualType_test.go | Test verifying string literal completions work in tuple contexts without needing to type quotes first |
Because in JavaScript, indexing into In fact here is some crazy completions behavior you can trigger in both Strada and Corsa just from how let x: { "-1": "hello" } = [/**/]Here, you will get completions for the string literal
(@copilot please add a test case to confirm that this no longer happens) |
Co-authored-by: DanielRosenwasser <[email protected]>
Added test case in d2f9582 that verifies properties named |

Completions panic with "index out of range [-1]" when requested inside
new Map([...])constructor at certain positions:Additionally, string literal completions were not being suggested in tuple contexts without typing a quote character first.
This PR also fixes an edge case bug where properties named
"-1"would incorrectly be suggested as completions in array literals due to the-1index being used to look up property names.Changes
completions.go: Add special handling for
OpenBracketTokenandCommaTokenin array literalspreviousTokenis[, get contextual type for element at index 0previousTokenis,, count elements before cursor and get contextual type for next elementservices.go: Add
GetContextualTypeForArrayElementfunctiongetContextualTypeForElementExpressionfor language service useTests: Added
TestCompletionsInMapConstructorNoCrashandTestCompletionsInArrayLiteralWithContextualType-1property edge case is fixed (properties named"-1"are not suggested)Benefits
let y: ["foo" | "bar"] = [/*here*/]now suggests"foo"and"bar")"-1"are no longer incorrectly suggested in array literals (e.g.,let x: { "-1": "hello" } = [/**/]no longer suggests"hello")Original prompt
new Map(...). #2254✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.