Skip to content

Commit

Permalink
Find definitions through function applies
Browse files Browse the repository at this point in the history
Started in #63 and #70
I think this now covers all (or most) cases
Most of this PR is just tests, since the missing code was just missing a few cases in switch cases
  • Loading branch information
julienduchesne committed Oct 12, 2022
1 parent 4b86f9d commit 111d05f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/ast/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
case *ast.Import:
filename := bodyNode.File.Value
foundDesugaredObjects = findTopLevelObjectsInFile(vm, filename, "")
case *ast.Index:
case *ast.Index, *ast.Apply:
tempStack := nodestack.NewNodeStack(bodyNode)
indexList = append(tempStack.BuildIndexList(), indexList...)
return FindRangesFromIndexList(stack, indexList, vm)
Expand Down
2 changes: 2 additions & 0 deletions pkg/ast/processing/top_level_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func findTopLevelObjects(stack *nodestack.NodeStack, vm *jsonnet.VM) []*ast.Desu
continue
}
stack.Push(varReference)
case *ast.Function:
stack.Push(curr.Body)
}
}
return objects
Expand Down
4 changes: 1 addition & 3 deletions pkg/nodestack/nodestack.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func (s *NodeStack) BuildIndexList() []string {
curr := s.Pop()
switch curr := curr.(type) {
case *ast.Apply:
if target, ok := curr.Target.(*ast.Var); ok {
indexList = append(indexList, string(target.Id))
}
s.Push(curr.Target)
case *ast.SuperIndex:
s.Push(curr.Index)
indexList = append(indexList, "super")
Expand Down
103 changes: 96 additions & 7 deletions pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,102 @@ var definitionTestCases = []definitionTestCase{
},
}},
},
{
name: "goto attribute of root-function library",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 5, Character: 70},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 22},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 11},
},
}},
},
{
name: "goto attribute of root-function library through local import",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 6, Character: 28},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 22},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 11},
},
}},
},
{
name: "goto attribute of root-function library through local resolved import",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 7, Character: 36},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 22},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 11},
},
}},
},
{
name: "goto nested attribute of root-function library",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 9, Character: 98},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 36},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 19},
},
}},
},
{
name: "goto nested attribute of root-function library through local import",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 10, Character: 55},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 36},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 19},
},
}},
},
{
name: "goto nested attribute of root-function library through local resolved import",
filename: "testdata/goto-root-function.jsonnet",
position: protocol.Position{Line: 11, Character: 64},
results: []definitionResult{{
targetFilename: "testdata/goto-root-function-lib.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 36},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 4, Character: 4},
End: protocol.Position{Line: 4, Character: 19},
},
}},
},
}

func TestDefinition(t *testing.T) {
Expand Down Expand Up @@ -891,13 +987,6 @@ func TestDefinitionFail(t *testing.T) {
position: protocol.Position{Line: 0, Character: 1},
expected: fmt.Errorf("cannot find definition"),
},

{
name: "goto range index fails",
filename: "testdata/goto-local-function.libsonnet",
position: protocol.Position{Line: 15, Character: 57},
expected: fmt.Errorf("unexpected node type when finding bind for 'ports': *ast.Apply"),
},
{
name: "goto super fails as no LHS object exists",
filename: "testdata/goto-local-function.libsonnet",
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/testdata/goto-root-function-lib.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function(attribute) {
attribute: attribute,

nestedFunc(nestedAttribute):: {
nestedAttribute: nestedAttribute,
},
}
13 changes: 13 additions & 0 deletions pkg/server/testdata/goto-root-function.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local lib = import 'goto-root-function-lib.libsonnet';
local libResolved = (import 'goto-root-function-lib.libsonnet')('test');

{

fromImport: (import 'goto-root-function-lib.libsonnet')('test').attribute,
fromLib: lib('test').attribute,
fromResolvedLib: libResolved.attribute,

nestedFromImport: (import 'goto-root-function-lib.libsonnet')('test').nestedFunc('test').nestedAttribute,
nestedFromLib: lib('test').nestedFunc('test').nestedAttribute,
nestedFromResolvedLib: libResolved.nestedFunc('test').nestedAttribute,
}

0 comments on commit 111d05f

Please sign in to comment.