Skip to content

Commit

Permalink
Support assert statements (#146)
Browse files Browse the repository at this point in the history
* Support `assert` statements
Closes #128

* Simplify `FindNodeByPosition`
The whole function is just about finding the children of nodes
go-jsonnet provides a helper for that
  • Loading branch information
julienduchesne authored Aug 7, 2024
1 parent f055ead commit 4dacb0f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
49 changes: 7 additions & 42 deletions pkg/ast/processing/find_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/testdata/goto-assert-var.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
local var1 = true,
var2: 'string',
assert var1 : self.var2,
}

0 comments on commit 4dacb0f

Please sign in to comment.