Skip to content
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
17 changes: 17 additions & 0 deletions pkg/server/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func (s *Server) Completion(_ context.Context, params *protocol.CompletionParams

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

// Inside parentheses, search for completions as if the content was on a separate line
// e.g., this enables completion in function arguments
if strings.LastIndex(line, "(") > strings.LastIndex(line, ")") {
argsPart := line[strings.LastIndex(line, "(")+1:]
// Only consider the last argument for completion
arguments := strings.Split(argsPart, ",")
lastArg := arguments[len(arguments)-1]
lastArg = strings.TrimSpace(lastArg)
lastArg = strings.TrimLeft(lastArg, "{[") // Ignore leading array or object tokens
line = lastArg
}

items := s.completionFromStack(line, searchStack, vm, params.Position)
return &protocol.CompletionList{IsIncomplete: false, Items: items}, nil
}
Expand All @@ -62,6 +74,7 @@ func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm
lineWords := splitWords(line)
lastWord := lineWords[len(lineWords)-1]
lastWord = strings.TrimRight(lastWord, ",;") // Ignore trailing commas and semicolons, they can present when someone is modifying an existing line
lastWord = strings.TrimSpace(lastWord)

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

Expand Down Expand Up @@ -289,5 +302,9 @@ func splitWords(input string) []string {
words = append(words, "")
}

if len(words) == 0 {
words = append(words, "")
}

return words
}
47 changes: 47 additions & 0 deletions pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,53 @@ func TestCompletion(t *testing.T) {
},
},
},
{
name: "completion in function arguments",
filename: "testdata/functions.libsonnet",
replaceString: "test: myfunc(arg1, arg2)",
replaceByString: "test: myfunc(arg1, self.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "a",
Kind: protocol.FieldCompletion,
Detail: "self.a",
InsertText: "a",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "variable",
},
},
{
Label: "b",
Kind: protocol.FieldCompletion,
Detail: "self.b",
InsertText: "b",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "variable",
},
},
{
Label: "c",
Kind: protocol.FieldCompletion,
Detail: "self.c",
InsertText: "c",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "string",
},
},
{
Label: "test",
Kind: protocol.FieldCompletion,
Detail: "self.test",
InsertText: "test",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "*ast.Apply",
},
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading