Skip to content

Commit

Permalink
Completion: Fix completion through an import pointing to a local bind
Browse files Browse the repository at this point in the history
Closes #113

This also fixes the go-to-definition functionality for this case, since it's the same code
  • Loading branch information
julienduchesne committed Aug 22, 2023
1 parent c910c25 commit 174d84f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
16 changes: 13 additions & 3 deletions pkg/ast/processing/top_level_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@ func FindTopLevelObjects(stack *nodestack.NodeStack, vm *jsonnet.VM) []*ast.Desu
rootNode, _, _ := vm.ImportAST(string(curr.Loc().File.DiagnosticFileName), filename)
stack.Push(rootNode)
case *ast.Index:
indexValue, indexIsString := curr.Index.(*ast.LiteralString)
if !indexIsString {
continue
}

container := stack.Peek()
if containerObj, containerIsObj := container.(*ast.DesugaredObject); containerIsObj {
indexValue, indexIsString := curr.Index.(*ast.LiteralString)
if !indexIsString {
if varTarget, targetIsVar := curr.Target.(*ast.Var); container == nil && targetIsVar {
ref, err := FindVarReference(varTarget, vm)
if err != nil {
log.WithError(err).Errorf("Error finding var reference, ignoring this node")
continue
}
container = ref
}

if containerObj, containerIsObj := container.(*ast.DesugaredObject); containerIsObj {
objs := findObjectFieldsInObject(containerObj, indexValue.Value, false)
if len(objs) > 0 {
stack.Push(objs[0].Body)
Expand Down
62 changes: 40 additions & 22 deletions pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,28 +454,6 @@ func TestCompletion(t *testing.T) {
},
},
},
// TODO: This one doesn't work yet
// Issue: https://github.com/grafana/jsonnet-language-server/issues/113
// {
// name: "autocomplete local at root 2",
// filename: "testdata/local-at-root-2.jsonnet",
// replaceString: "hello.to",
// replaceByString: "hello.",
// expected: protocol.CompletionList{
// IsIncomplete: false,
// Items: []protocol.CompletionItem{
// {
// Label: "to",
// Kind: protocol.FieldCompletion,
// Detail: "hello.to",
// InsertText: "to",
// LabelDetails: protocol.CompletionItemLabelDetails{
// Description: "object",
// },
// },
// },
// },
// },
{
// This checks that we don't match on `hello.hello.*` if we autocomplete on `hello.hel.`
name: "autocomplete local at root, no partial match if full match exists",
Expand Down Expand Up @@ -508,6 +486,46 @@ func TestCompletion(t *testing.T) {
Items: nil,
},
},
{
name: "autocomplete local at root 2",
filename: "testdata/local-at-root-2.jsonnet",
replaceString: "hello.to",
replaceByString: "hello.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "to",
Kind: protocol.FieldCompletion,
Detail: "hello.to",
InsertText: "to",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "object",
},
},
},
},
},
{
name: "autocomplete local at root 2, nested",
filename: "testdata/local-at-root-2.jsonnet",
replaceString: "hello.to",
replaceByString: "hello.to.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "the",
Kind: protocol.FieldCompletion,
Detail: "hello.to.the",
InsertText: "the",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "string",
},
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 174d84f

Please sign in to comment.