Skip to content

Commit

Permalink
Make it work nested
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduchesne committed Aug 24, 2023
1 parent 0f53eef commit 3f49668
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
21 changes: 14 additions & 7 deletions pkg/ast/processing/top_level_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ 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
}

var container ast.Node
// If our target is a var, the container for the index is the var ref
if varTarget, targetIsVar := curr.Target.(*ast.Var); targetIsVar {
Expand All @@ -53,14 +58,16 @@ func FindTopLevelObjects(stack *nodestack.NodeStack, vm *jsonnet.VM) []*ast.Desu
container = stack.Peek()
}

var possibleObjects []*ast.DesugaredObject
if containerObj, containerIsObj := container.(*ast.DesugaredObject); containerIsObj {
indexValue, indexIsString := curr.Index.(*ast.LiteralString)
if !indexIsString {
continue
}
objs := findObjectFieldsInObject(containerObj, indexValue.Value, false)
if len(objs) > 0 {
stack.Push(objs[0].Body)
possibleObjects = []*ast.DesugaredObject{containerObj}
} else if containerImport, containerIsImport := container.(*ast.Import); containerIsImport {
possibleObjects = FindTopLevelObjectsInFile(vm, containerImport.File.Value, string(containerImport.Loc().File.DiagnosticFileName))
}

for _, obj := range possibleObjects {
for _, field := range findObjectFieldsInObject(obj, indexValue.Value, false) {
stack.Push(field.Body)
}
}
case *ast.Var:
Expand Down
40 changes: 40 additions & 0 deletions pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,46 @@ func TestCompletion(t *testing.T) {
Kind: protocol.FieldCompletion,
Detail: "hello.to.the",
InsertText: "the",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "object",
},
},
},
},
},
{
name: "autocomplete local at root 3, import chain",
filename: "testdata/local-at-root-3.jsonnet",
replaceString: "hello2.the",
replaceByString: "hello2.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "the",
Kind: protocol.FieldCompletion,
Detail: "hello2.the",
InsertText: "the",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "object",
},
},
},
},
},
{
name: "autocomplete local at root 4, import chain",
filename: "testdata/local-at-root-4.jsonnet",
replaceString: "hello3.world",
replaceByString: "hello3.",
expected: protocol.CompletionList{
IsIncomplete: false,
Items: []protocol.CompletionItem{
{
Label: "world",
Kind: protocol.FieldCompletion,
Detail: "hello3.world",
InsertText: "world",
LabelDetails: protocol.CompletionItemLabelDetails{
Description: "string",
},
Expand Down
4 changes: 1 addition & 3 deletions pkg/server/testdata/local-at-root-2.jsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local hello = import 'local-at-root.jsonnet';

{
a: hello.to,
}
hello.to
3 changes: 3 additions & 0 deletions pkg/server/testdata/local-at-root-3.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local hello2 = import 'local-at-root-2.jsonnet';

hello2.the
3 changes: 3 additions & 0 deletions pkg/server/testdata/local-at-root-4.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local hello3 = import 'local-at-root-3.jsonnet';

hello3.world
4 changes: 3 additions & 1 deletion pkg/server/testdata/local-at-root.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ local hello = {
},
hello: {
to: {
the: 'world',
the: {
world: 'hello',
},
},
},
};
Expand Down

0 comments on commit 3f49668

Please sign in to comment.