Skip to content

Commit

Permalink
Completion from dollar sign: Basic case (#79)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
julienduchesne authored Oct 20, 2022
1 parent 81ab873 commit f6fe858
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/server/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"sort"
"strings"

"github.com/google/go-jsonnet"
Expand Down Expand Up @@ -59,6 +60,7 @@ func getCompletionLine(fileContent string, position protocol.Position) string {
func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm *jsonnet.VM) []protocol.CompletionItem {
lineWords := strings.Split(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

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

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

sort.Slice(items, func(i, j int) bool {
return items[i].Label < items[j].Label
})

return items
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,52 @@ func TestCompletion(t *testing.T) {
},
},
},
{
name: "autocomplete dollar sign",
filename: "testdata/goto-dollar-simple.jsonnet",
replaceString: "test: $.attribute,",
replaceByString: "test: $.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "attribute",
Kind: protocol.FieldCompletion,
Detail: "$.attribute",
InsertText: "attribute",
},
{
Label: "attribute2",
Kind: protocol.FieldCompletion,
Detail: "$.attribute2",
InsertText: "attribute2",
},
},
},
},
{
name: "autocomplete dollar sign, end with comma",
filename: "testdata/goto-dollar-simple.jsonnet",
replaceString: "test: $.attribute,",
replaceByString: "test: $.,",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "attribute",
Kind: protocol.FieldCompletion,
Detail: "$.attribute",
InsertText: "attribute",
},
{
Label: "attribute2",
Kind: protocol.FieldCompletion,
Detail: "$.attribute2",
InsertText: "attribute2",
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit f6fe858

Please sign in to comment.