Skip to content

Commit 42fa4ab

Browse files
committed
Completion within parentheses
* Enables completion when typing inside parentheses (e.g. in-line arguments of a function call) * Also fixes some edge-case bugs causing the server to crash (e.g. initiating auto-complete on an empty line)
1 parent 3e5d544 commit 42fa4ab

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

pkg/server/completion.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ func (s *Server) Completion(_ context.Context, params *protocol.CompletionParams
4444

4545
vm := s.getVM(doc.Item.URI.SpanURI().Filename())
4646

47+
// Inside parentheses, search for completions as if the content was on a separate line
48+
// e.g., this enables completion in function arguments
49+
if strings.LastIndex(line, "(") > strings.LastIndex(line, ")") {
50+
argsPart := line[strings.LastIndex(line, "(")+1:]
51+
// Only consider the last argument for completion
52+
arguments := strings.Split(argsPart, ",")
53+
lastArg := arguments[len(arguments)-1]
54+
lastArg = strings.TrimSpace(lastArg)
55+
lastArg = strings.TrimLeft(lastArg, "{[") // Ignore leading array or object tokens
56+
line = lastArg
57+
}
58+
4759
items := s.completionFromStack(line, searchStack, vm, params.Position)
4860
return &protocol.CompletionList{IsIncomplete: false, Items: items}, nil
4961
}
@@ -62,6 +74,7 @@ func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm
6274
lineWords := splitWords(line)
6375
lastWord := lineWords[len(lineWords)-1]
6476
lastWord = strings.TrimRight(lastWord, ",;") // Ignore trailing commas and semicolons, they can present when someone is modifying an existing line
77+
lastWord = strings.TrimSpace(lastWord)
6578

6679
indexes := strings.Split(lastWord, ".")
6780

@@ -289,5 +302,9 @@ func splitWords(input string) []string {
289302
words = append(words, "")
290303
}
291304

305+
if len(words) == 0 {
306+
words = append(words, "")
307+
}
308+
292309
return words
293310
}

pkg/server/completion_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,44 @@ func TestCompletion(t *testing.T) {
692692
},
693693
},
694694
},
695+
{
696+
name: "completion in function arguments",
697+
filename: "testdata/goto-functions.libsonnet",
698+
replaceString: "test: myfunc(arg1, arg2)",
699+
replaceByString: "test: myfunc(arg1, self.",
700+
expected: protocol.CompletionList{
701+
IsIncomplete: false,
702+
Items: []protocol.CompletionItem{
703+
{
704+
Label: "a",
705+
Kind: protocol.FieldCompletion,
706+
Detail: "self.a",
707+
InsertText: "a",
708+
LabelDetails: protocol.CompletionItemLabelDetails{
709+
Description: "variable",
710+
},
711+
},
712+
{
713+
Label: "b",
714+
Kind: protocol.FieldCompletion,
715+
Detail: "self.b",
716+
InsertText: "b",
717+
LabelDetails: protocol.CompletionItemLabelDetails{
718+
Description: "variable",
719+
},
720+
},
721+
{
722+
Label: "c",
723+
Kind: protocol.FieldCompletion,
724+
Detail: "self.c",
725+
InsertText: "c",
726+
LabelDetails: protocol.CompletionItemLabelDetails{
727+
Description: "string",
728+
},
729+
},
730+
},
731+
},
732+
},
695733
}
696734
for _, tc := range testCases {
697735
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)