Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find definitions through function applies #77

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}