Skip to content

Commit 4dacb0f

Browse files
Support assert statements (#146)
* 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
1 parent f055ead commit 4dacb0f

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

pkg/ast/processing/find_position.go

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
"github.com/google/go-jsonnet/ast"
7+
"github.com/google/go-jsonnet/toolutils"
78
"github.com/grafana/jsonnet-language-server/pkg/nodestack"
89
)
910

@@ -34,14 +35,8 @@ func FindNodeByPosition(node ast.Node, location ast.Location) (*nodestack.NodeSt
3435
} else if curr.Loc().End.IsSet() {
3536
continue
3637
}
38+
3739
switch curr := curr.(type) {
38-
case *ast.Local:
39-
for _, bind := range curr.Binds {
40-
stack.Push(bind.Body)
41-
}
42-
if curr.Body != nil {
43-
stack.Push(curr.Body)
44-
}
4540
case *ast.DesugaredObject:
4641
for _, field := range curr.Fields {
4742
body := field.Body
@@ -57,43 +52,13 @@ func FindNodeByPosition(node ast.Node, location ast.Location) (*nodestack.NodeSt
5752
for _, local := range curr.Locals {
5853
stack.Push(local.Body)
5954
}
60-
case *ast.Binary:
61-
stack.Push(curr.Left)
62-
stack.Push(curr.Right)
63-
case *ast.Array:
64-
for _, element := range curr.Elements {
65-
stack.Push(element.Expr)
55+
for _, assert := range curr.Asserts {
56+
stack.Push(assert)
6657
}
67-
case *ast.Apply:
68-
for _, posArg := range curr.Arguments.Positional {
69-
stack.Push(posArg.Expr)
70-
}
71-
for _, namedArg := range curr.Arguments.Named {
72-
stack.Push(namedArg.Arg)
73-
}
74-
stack.Push(curr.Target)
75-
case *ast.Conditional:
76-
stack.Push(curr.Cond)
77-
stack.Push(curr.BranchTrue)
78-
stack.Push(curr.BranchFalse)
79-
case *ast.Error:
80-
stack.Push(curr.Expr)
81-
case *ast.Function:
82-
for _, param := range curr.Parameters {
83-
if param.DefaultArg != nil {
84-
stack.Push(param.DefaultArg)
85-
}
58+
default:
59+
for _, c := range toolutils.Children(curr) {
60+
stack.Push(c)
8661
}
87-
stack.Push(curr.Body)
88-
case *ast.Index:
89-
stack.Push(curr.Target)
90-
stack.Push(curr.Index)
91-
case *ast.InSuper:
92-
stack.Push(curr.Index)
93-
case *ast.SuperIndex:
94-
stack.Push(curr.Index)
95-
case *ast.Unary:
96-
stack.Push(curr.Expr)
9762
}
9863
}
9964
return searchStack.ReorderDesugaredObjects(), nil

pkg/server/definition_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,36 @@ var definitionTestCases = []definitionTestCase{
943943
},
944944
}},
945945
},
946+
{
947+
name: "goto assert local var",
948+
filename: "testdata/goto-assert-var.jsonnet",
949+
position: protocol.Position{Line: 3, Character: 11},
950+
results: []definitionResult{{
951+
targetRange: protocol.Range{
952+
Start: protocol.Position{Line: 1, Character: 8},
953+
End: protocol.Position{Line: 1, Character: 19},
954+
},
955+
targetSelectionRange: protocol.Range{
956+
Start: protocol.Position{Line: 1, Character: 8},
957+
End: protocol.Position{Line: 1, Character: 12},
958+
},
959+
}},
960+
},
961+
{
962+
name: "goto assert self var",
963+
filename: "testdata/goto-assert-var.jsonnet",
964+
position: protocol.Position{Line: 3, Character: 23},
965+
results: []definitionResult{{
966+
targetRange: protocol.Range{
967+
Start: protocol.Position{Line: 2, Character: 2},
968+
End: protocol.Position{Line: 2, Character: 16},
969+
},
970+
targetSelectionRange: protocol.Range{
971+
Start: protocol.Position{Line: 2, Character: 2},
972+
End: protocol.Position{Line: 2, Character: 6},
973+
},
974+
}},
975+
},
946976
}
947977

948978
func TestDefinition(t *testing.T) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
local var1 = true,
3+
var2: 'string',
4+
assert var1 : self.var2,
5+
}

0 commit comments

Comments
 (0)