diff --git a/pkg/ast/processing/find_position.go b/pkg/ast/processing/find_position.go index 92682d6..aedd2af 100644 --- a/pkg/ast/processing/find_position.go +++ b/pkg/ast/processing/find_position.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/google/go-jsonnet/ast" + "github.com/google/go-jsonnet/toolutils" "github.com/grafana/jsonnet-language-server/pkg/nodestack" ) @@ -34,14 +35,8 @@ func FindNodeByPosition(node ast.Node, location ast.Location) (*nodestack.NodeSt } else if curr.Loc().End.IsSet() { continue } + switch curr := curr.(type) { - case *ast.Local: - for _, bind := range curr.Binds { - stack.Push(bind.Body) - } - if curr.Body != nil { - stack.Push(curr.Body) - } case *ast.DesugaredObject: for _, field := range curr.Fields { body := field.Body @@ -57,43 +52,13 @@ func FindNodeByPosition(node ast.Node, location ast.Location) (*nodestack.NodeSt for _, local := range curr.Locals { stack.Push(local.Body) } - case *ast.Binary: - stack.Push(curr.Left) - stack.Push(curr.Right) - case *ast.Array: - for _, element := range curr.Elements { - stack.Push(element.Expr) + for _, assert := range curr.Asserts { + stack.Push(assert) } - case *ast.Apply: - for _, posArg := range curr.Arguments.Positional { - stack.Push(posArg.Expr) - } - for _, namedArg := range curr.Arguments.Named { - stack.Push(namedArg.Arg) - } - stack.Push(curr.Target) - case *ast.Conditional: - stack.Push(curr.Cond) - stack.Push(curr.BranchTrue) - stack.Push(curr.BranchFalse) - case *ast.Error: - stack.Push(curr.Expr) - case *ast.Function: - for _, param := range curr.Parameters { - if param.DefaultArg != nil { - stack.Push(param.DefaultArg) - } + default: + for _, c := range toolutils.Children(curr) { + stack.Push(c) } - stack.Push(curr.Body) - case *ast.Index: - stack.Push(curr.Target) - stack.Push(curr.Index) - case *ast.InSuper: - stack.Push(curr.Index) - case *ast.SuperIndex: - stack.Push(curr.Index) - case *ast.Unary: - stack.Push(curr.Expr) } } return searchStack.ReorderDesugaredObjects(), nil diff --git a/pkg/server/definition_test.go b/pkg/server/definition_test.go index cc7db68..5789aa3 100644 --- a/pkg/server/definition_test.go +++ b/pkg/server/definition_test.go @@ -943,6 +943,36 @@ var definitionTestCases = []definitionTestCase{ }, }}, }, + { + name: "goto assert local var", + filename: "testdata/goto-assert-var.jsonnet", + position: protocol.Position{Line: 3, Character: 11}, + results: []definitionResult{{ + targetRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 8}, + End: protocol.Position{Line: 1, Character: 19}, + }, + targetSelectionRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 8}, + End: protocol.Position{Line: 1, Character: 12}, + }, + }}, + }, + { + name: "goto assert self var", + filename: "testdata/goto-assert-var.jsonnet", + position: protocol.Position{Line: 3, Character: 23}, + results: []definitionResult{{ + targetRange: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 16}, + }, + targetSelectionRange: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 6}, + }, + }}, + }, } func TestDefinition(t *testing.T) { diff --git a/pkg/server/testdata/goto-assert-var.jsonnet b/pkg/server/testdata/goto-assert-var.jsonnet new file mode 100644 index 0000000..1f5f857 --- /dev/null +++ b/pkg/server/testdata/goto-assert-var.jsonnet @@ -0,0 +1,5 @@ +{ + local var1 = true, + var2: 'string', + assert var1 : self.var2, +}