@@ -2977,27 +2977,7 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi
29772977 contextualArrayType := typeChecker .GetContextualType (parent , checker .ContextFlagsNone )
29782978 if contextualArrayType != nil {
29792979 // Get the type for the first element (index 0)
2980- return typeChecker .GetContextualTypeForArrayElement (contextualArrayType , 0 )
2981- }
2982- }
2983- return nil
2984- case ast .KindCommaToken :
2985- // When completing after `,` in an array literal (e.g., `[x, /*here*/]`),
2986- // we should provide contextual type for the element after the comma
2987- if ast .IsArrayLiteralExpression (parent ) {
2988- contextualArrayType := typeChecker .GetContextualType (parent , checker .ContextFlagsNone )
2989- if contextualArrayType != nil {
2990- // Count how many elements come before the cursor position
2991- arrayLiteral := parent .AsArrayLiteralExpression ()
2992- elementIndex := 0
2993- for _ , elem := range arrayLiteral .Elements .Nodes {
2994- if elem .Pos () < position {
2995- elementIndex ++
2996- } else {
2997- break
2998- }
2999- }
3000- return typeChecker .GetContextualTypeForArrayElement (contextualArrayType , elementIndex )
2980+ return typeChecker .GetContextualTypeForArrayLiteralAtPosition (contextualArrayType , parent , position )
30012981 }
30022982 }
30032983 return nil
@@ -3023,22 +3003,30 @@ func getContextualType(previousToken *ast.Node, position int, file *ast.SourceFi
30233003 if ast .IsConditionalExpression (parent ) {
30243004 return getContextualTypeForConditionalExpression (parent , position , file , typeChecker )
30253005 }
3026- // Fall through to default for other colon contexts (object literals, etc.)
3027- fallthrough
3028- default :
3029- argInfo := getArgumentInfoForCompletions (previousToken , position , file , typeChecker )
3030- if argInfo != nil {
3031- return typeChecker .GetContextualTypeForArgumentAtIndex (argInfo .invocation , argInfo .argumentIndex )
3032- } else if isEqualityOperatorKind (previousToken .Kind ) && ast .IsBinaryExpression (parent ) && isEqualityOperatorKind (parent .AsBinaryExpression ().OperatorToken .Kind ) {
3033- // completion at `x ===/**/`
3034- return typeChecker .GetTypeAtLocation (parent .AsBinaryExpression ().Left )
3035- } else {
3036- contextualType := typeChecker .GetContextualType (previousToken , checker .ContextFlagsCompletions )
3037- if contextualType != nil {
3038- return contextualType
3006+ case ast .KindCommaToken :
3007+ // When completing after `,` in an array literal (e.g., `[x, /*here*/]`),
3008+ // we should provide contextual type for the element after the comma.
3009+ if ast .IsArrayLiteralExpression (parent ) {
3010+ contextualArrayType := typeChecker .GetContextualType (parent , checker .ContextFlagsNone )
3011+ if contextualArrayType != nil {
3012+ return typeChecker .GetContextualTypeForArrayLiteralAtPosition (contextualArrayType , parent , position )
30393013 }
3040- return typeChecker .GetContextualType (previousToken , checker .ContextFlagsNone )
3014+ return nil
3015+ }
3016+ }
3017+ // Default case: see if we're in an argument position.
3018+ argInfo := getArgumentInfoForCompletions (previousToken , position , file , typeChecker )
3019+ if argInfo != nil {
3020+ return typeChecker .GetContextualTypeForArgumentAtIndex (argInfo .invocation , argInfo .argumentIndex )
3021+ } else if isEqualityOperatorKind (previousToken .Kind ) && ast .IsBinaryExpression (parent ) && isEqualityOperatorKind (parent .AsBinaryExpression ().OperatorToken .Kind ) {
3022+ // completion at `x ===/**/`
3023+ return typeChecker .GetTypeAtLocation (parent .AsBinaryExpression ().Left )
3024+ } else {
3025+ contextualType := typeChecker .GetContextualType (previousToken , checker .ContextFlagsCompletions )
3026+ if contextualType != nil {
3027+ return contextualType
30413028 }
3029+ return typeChecker .GetContextualType (previousToken , checker .ContextFlagsNone )
30423030 }
30433031}
30443032
0 commit comments