Skip to content

Add label completions #888

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

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,7 @@ type (
ObjectTypeDeclaration = Node // ClassLikeDeclaration | InterfaceDeclaration | TypeLiteralNode
JsxOpeningLikeElement = Node // JsxOpeningElement | JsxSelfClosingElement
NamedImportsOrExports = Node // NamedImports | NamedExports
BreakOrContinueStatement = Node // BreakStatement | ContinueStatement
)

// Aliases for node singletons
Expand Down
70 changes: 69 additions & 1 deletion internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ func (l *LanguageService) getCompletionsAtPosition(

// !!! string literal completions

// !!! label completions
if previousToken != nil && ast.IsBreakOrContinueStatement(previousToken.Parent) &&
(previousToken.Kind == ast.KindBreakKeyword ||
previousToken.Kind == ast.KindContinueKeyword ||
previousToken.Kind == ast.KindIdentifier) {
return l.getLabelCompletionsAtPosition(previousToken.Parent, clientOptions, file, position)
}

checker, done := program.GetTypeCheckerForFile(ctx, file)
defer done()
Expand Down Expand Up @@ -4103,3 +4108,66 @@ func (l *LanguageService) createLSPCompletionItem(
Data: nil, // !!! auto-imports
}
}

func (l *LanguageService) getLabelCompletionsAtPosition(
node *ast.BreakOrContinueStatement,
clientOptions *lsproto.CompletionClientCapabilities,
file *ast.SourceFile,
position int,
) *lsproto.CompletionList {
items := l.getLabelStatementCompletions(node, clientOptions, file, position)
if len(items) == 0 {
return nil
}
defaultCommitCharacters := getDefaultCommitCharacters(false /*isNewIdentifierLocation*/)
itemDefaults := setCommitCharacters(clientOptions, items, &defaultCommitCharacters)
return &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: items,
}
}

func (l *LanguageService) getLabelStatementCompletions(
node *ast.BreakOrContinueStatement,
clientOptions *lsproto.CompletionClientCapabilities,
file *ast.SourceFile,
position int,
) []*lsproto.CompletionItem {
var uniques core.Set[string]
var items []*lsproto.CompletionItem
current := node
for current != nil {
if ast.IsFunctionLike(current) {
break
}
if ast.IsLabeledStatement(current) {
name := current.Label().Text()
if !uniques.Has(name) {
uniques.Add(name)
items = append(items, l.createLSPCompletionItem(
name,
"", /*insertText*/
"", /*filterText*/
SortTextLocationPriority,
ScriptElementKindLabel,
core.Set[ScriptElementKindModifier]{}, /*kindModifiers*/
nil, /*replacementSpan*/
nil, /*optionalReplacementSpan*/
nil, /*commitCharacters*/
nil, /*labelDetails*/
file,
position,
clientOptions,
false, /*isMemberCompletion*/
false, /*isSnippet*/
false, /*hasAction*/
false, /*preselect*/
"", /*source*/
))
}
}
current = current.Parent
}
return items
}
151 changes: 151 additions & 0 deletions internal/ls/completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestCompletions(t *testing.T) {
variableKind := ptrTo(lsproto.CompletionItemKindVariable)
classKind := ptrTo(lsproto.CompletionItemKindClass)
keywordKind := ptrTo(lsproto.CompletionItemKindKeyword)
propertyKind := ptrTo(lsproto.CompletionItemKindProperty)

stringMembers := []*lsproto.CompletionItem{
{Label: "charAt", Kind: methodKind, SortText: sortTextLocationPriority, InsertTextFormat: insertTextFormatPlainText},
Expand Down Expand Up @@ -1528,6 +1529,156 @@ function fn3() {
},
},
},
{
name: "completionListWithLabel",
files: map[string]string{
defaultMainFileName: `label: while (true) {
break /*1*/
continue /*2*/
testlabel: while (true) {
break /*3*/
continue /*4*/
break tes/*5*/
continue tes/*6*/
}
break /*7*/
break; /*8*/
}`,
},
expectedResult: map[string]*testCaseResult{
"1": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"2": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"7": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"3": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "testlabel",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"4": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "testlabel",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"5": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "testlabel",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"6": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{
{
Label: "testlabel",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
{
Label: "label",
Kind: propertyKind,
SortText: sortTextLocationPriority,
InsertTextFormat: insertTextFormatPlainText,
},
},
},
},
"8": {
list: &lsproto.CompletionList{
IsIncomplete: false,
ItemDefaults: itemDefaults,
Items: []*lsproto.CompletionItem{},
},
isIncludes: true,
excludes: []string{"label"},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
Expand Down