Skip to content

Commit 93a0c28

Browse files
Copilotjakebailey
andcommitted
Fix source node inference sort ordering in string literal completions
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 4dfa0b5 commit 93a0c28

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

internal/fourslash/tests/gen/typeErrorAfterStringCompletionsInNestedCall_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ createMachine<GreetingEvent>({
4949
},
5050
Items: &fourslash.CompletionsExpectedItems{
5151
Exact: []fourslash.CompletionsExpectedItem{
52-
"ALOHA",
5352
"ALOHAx",
53+
"ALOHA",
5454
"LUNCH_TIME",
5555
"MORNING",
5656
},

internal/ls/string_completions.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import (
2828
)
2929

3030
type completionsFromTypes struct {
31-
types []*checker.StringLiteralType
32-
isNewIdentifier bool
31+
types []*checker.StringLiteralType
32+
isNewIdentifier bool
33+
numberOfInferences int // number of types at the beginning of 'types' from source node inference
3334
}
3435

3536
type completionsFromProperties struct {
@@ -146,14 +147,19 @@ func (l *LanguageService) convertStringLiteralCompletions(
146147
} else {
147148
quoteChar = printer.QuoteCharDoubleQuote
148149
}
149-
items := core.Map(completion.types, func(t *checker.StringLiteralType) *lsproto.CompletionItem {
150+
items := make([]*lsproto.CompletionItem, len(completion.types))
151+
for i, t := range completion.types {
150152
name := printer.EscapeString(t.AsLiteralType().Value().(string), quoteChar)
151-
return l.createLSPCompletionItem(
153+
sortText := SortTextLocationPriority
154+
if i < completion.numberOfInferences {
155+
sortText = SortTextLocalDeclarationPriority
156+
}
157+
items[i] = l.createLSPCompletionItem(
152158
ctx,
153159
name,
154160
"", /*insertText*/
155161
"", /*filterText*/
156-
SortTextLocationPriority,
162+
sortText,
157163
lsutil.ScriptElementKindString,
158164
collections.Set[lsutil.ScriptElementKindModifier]{},
159165
l.getReplacementRangeForContextToken(file, contextToken, position),
@@ -169,7 +175,7 @@ func (l *LanguageService) convertStringLiteralCompletions(
169175
nil, /*autoImportEntryData*/
170176
nil, /*detail*/
171177
)
172-
})
178+
}
173179
defaultCommitCharacters := getDefaultCommitCharacters(completion.isNewIdentifier)
174180
itemDefaults := l.setItemDefaults(
175181
ctx,
@@ -283,11 +289,17 @@ func (l *LanguageService) getStringLiteralCompletionEntries(
283289
}
284290
if ast.FindAncestor(parent.Parent, ast.IsCallLikeExpression) != nil {
285291
uniques := &collections.Set[string]{}
286-
stringLiteralTypes := append(
287-
getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsNone), uniques, typeChecker),
288-
getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsCompletions), uniques, typeChecker)...,
289-
)
290-
return toStringLiteralCompletionsFromTypes(stringLiteralTypes)
292+
noneTypes := getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsNone), uniques, typeChecker)
293+
completionsTypes := getStringLiteralTypes(typeChecker.GetContextualType(node, checker.ContextFlagsCompletions), uniques, typeChecker)
294+
stringLiteralTypes := append(noneTypes, completionsTypes...)
295+
result := toCompletionsFromTypes(stringLiteralTypes)
296+
if result != nil {
297+
result.numberOfInferences = len(noneTypes)
298+
return &stringLiteralCompletions{
299+
fromTypes: result,
300+
}
301+
}
302+
return nil
291303
}
292304
return &stringLiteralCompletions{
293305
fromTypes: fromContextualType(checker.ContextFlagsNone, node, typeChecker),
@@ -436,16 +448,6 @@ func toCompletionsFromTypes(types []*checker.StringLiteralType) *completionsFrom
436448
}
437449
}
438450

439-
func toStringLiteralCompletionsFromTypes(types []*checker.StringLiteralType) *stringLiteralCompletions {
440-
result := toCompletionsFromTypes(types)
441-
if result == nil {
442-
return nil
443-
}
444-
return &stringLiteralCompletions{
445-
fromTypes: result,
446-
}
447-
}
448-
449451
func fromUnionableLiteralType(
450452
grandparent *ast.Node,
451453
parent *ast.Node,

0 commit comments

Comments
 (0)