Skip to content

Commit f6fe858

Browse files
Completion from dollar sign: Basic case (#79)
Closes #4 Also: - Sort the list of suggestions by label - Ignore lines ending with , or ;. This happens when a user is replacing an index in an existing line (refactoring, etc)
1 parent 81ab873 commit f6fe858

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pkg/server/completion.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"context"
5+
"sort"
56
"strings"
67

78
"github.com/google/go-jsonnet"
@@ -59,6 +60,7 @@ func getCompletionLine(fileContent string, position protocol.Position) string {
5960
func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm *jsonnet.VM) []protocol.CompletionItem {
6061
lineWords := strings.Split(line, " ")
6162
lastWord := lineWords[len(lineWords)-1]
63+
lastWord = strings.TrimRight(lastWord, ",;") // Ignore trailing commas and semicolons, they can present when someone is modifying an existing line
6264

6365
indexes := strings.Split(lastWord, ".")
6466
firstIndex, indexes := indexes[0], indexes[1:]
@@ -103,6 +105,8 @@ func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm
103105
ref, _ := processing.FindVarReference(targetVar, vm)
104106

105107
switch ref := ref.(type) {
108+
case *ast.Self: // This case catches `$` references (it's set as a self reference on the root object)
109+
objectsToSearch = processing.FindTopLevelObjects(nodestack.NewNodeStack(stack.From), vm)
106110
case *ast.DesugaredObject:
107111
objectsToSearch = []*ast.DesugaredObject{ref}
108112
case *ast.Import:
@@ -188,6 +192,10 @@ func createCompletionItemsFromObjects(objects []*ast.DesugaredObject, firstIndex
188192
}
189193
}
190194

195+
sort.Slice(items, func(i, j int) bool {
196+
return items[i].Label < items[j].Label
197+
})
198+
191199
return items
192200
}
193201

pkg/server/completion_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,52 @@ func TestCompletion(t *testing.T) {
285285
},
286286
},
287287
},
288+
{
289+
name: "autocomplete dollar sign",
290+
filename: "testdata/goto-dollar-simple.jsonnet",
291+
replaceString: "test: $.attribute,",
292+
replaceByString: "test: $.",
293+
expected: protocol.CompletionList{
294+
IsIncomplete: false,
295+
Items: []protocol.CompletionItem{
296+
{
297+
Label: "attribute",
298+
Kind: protocol.FieldCompletion,
299+
Detail: "$.attribute",
300+
InsertText: "attribute",
301+
},
302+
{
303+
Label: "attribute2",
304+
Kind: protocol.FieldCompletion,
305+
Detail: "$.attribute2",
306+
InsertText: "attribute2",
307+
},
308+
},
309+
},
310+
},
311+
{
312+
name: "autocomplete dollar sign, end with comma",
313+
filename: "testdata/goto-dollar-simple.jsonnet",
314+
replaceString: "test: $.attribute,",
315+
replaceByString: "test: $.,",
316+
expected: protocol.CompletionList{
317+
IsIncomplete: false,
318+
Items: []protocol.CompletionItem{
319+
{
320+
Label: "attribute",
321+
Kind: protocol.FieldCompletion,
322+
Detail: "$.attribute",
323+
InsertText: "attribute",
324+
},
325+
{
326+
Label: "attribute2",
327+
Kind: protocol.FieldCompletion,
328+
Detail: "$.attribute2",
329+
InsertText: "attribute2",
330+
},
331+
},
332+
},
333+
},
288334
}
289335
for _, tc := range testCases {
290336
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)