Skip to content

Conversation

@gabritto
Copy link
Member

@gabritto gabritto commented Dec 9, 2025

Fixes some problems caused by #2258:

  1. If we're after a comma, we may also be inside an argument list and should go through the code for that when the parent is not an array literal.
  2. Unlike the comment Copilot added, we can't just pass -1 as the spread indices, it'll result in a wrong contextual type if there are spread elements present. I think -1 length is ok to use, since it'll just opt us out of counting from the end of the array, which I think we should avoid since the array literal may be incomplete.

Copilot AI review requested due to automatic review settings December 9, 2025 23:19
@gabritto gabritto enabled auto-merge December 9, 2025 23:21
Copy link
Contributor

Copilot AI left a 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 issues with contextual type determination for array completions introduced in #2258. The changes address two key problems: (1) ensuring comma-separated contexts fall through to argument list handling when not in array literals, and (2) properly tracking spread element indices when computing contextual types for array elements.

  • Refactored the getContextualType function to restructure control flow, removing the fallthrough from KindColonToken and moving KindCommaToken handling to properly support both array literals and argument lists
  • Replaced the oversimplified contextual type computation with GetContextualTypeForArrayLiteralAtPosition, which now tracks spread element positions
  • Added test coverage for argument completions with both function calls and array literals with spread elements

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/ls/completions.go Restructured switch cases to properly handle comma tokens in both array literals and argument lists, removed problematic fallthrough behavior
internal/checker/services.go Implemented GetContextualTypeForArrayLiteralAtPosition to replace simplified logic, adding spread element tracking and position-based element index calculation
internal/fourslash/tests/argumentCompletions_test.go Added test cases verifying contextual type completions for function arguments and array literals with spread elements

@gabritto gabritto disabled auto-merge December 9, 2025 23:23
@gabritto gabritto enabled auto-merge December 9, 2025 23:28
contextualType := typeChecker.GetContextualType(previousToken, checker.ContextFlagsCompletions)
if contextualType != nil {
return contextualType
case ast.KindCommaToken:
Copy link
Member

@DanielRosenwasser DanielRosenwasser Dec 9, 2025

Choose a reason for hiding this comment

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

I might still co-locate the [ and ] cases, but this is fine.

I was thinking about whether there are cases where we should also guard on ], but I couldn't imagine where you'd want or have a contextual type immediately after a ].

@gabritto gabritto added this pull request to the merge queue Dec 9, 2025
Merged via the queue into main with commit cd451c4 Dec 9, 2025
22 checks passed
@gabritto gabritto deleted the gabritto/fix_array_ctx branch December 9, 2025 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants