Skip to content

Commit a02ab61

Browse files
Support multiple definitons: Binary operators (#32)
Whenever a binary operator was in the chain, it would make all fields to not be found further down the line This supports find fields in constructs like this: ``` { my_field+: (import 'test') + (import 'test2') } ``` Note: the tests are getting pretty intense. I'm planning a refactor once I've got the cases I want working. I'll probably set up some benchmarking as well, see if I can find some low-hanging fruits for performance improvements This builds upon #30 and #31 Issue: #6
1 parent e164149 commit a02ab61

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

pkg/processing/find_field.go

+12
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,20 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
119119
return ranges, nil
120120
}
121121

122+
// Unpack binary nodes. A field could be either in the left or right side of the binary
123+
var fieldNodes []ast.Node
122124
for _, foundField := range foundFields {
123125
switch fieldNode := foundField.Body.(type) {
126+
case *ast.Binary:
127+
fieldNodes = append(fieldNodes, fieldNode.Right)
128+
fieldNodes = append(fieldNodes, fieldNode.Left)
129+
default:
130+
fieldNodes = append(fieldNodes, fieldNode)
131+
}
132+
}
133+
134+
for _, fieldNode := range fieldNodes {
135+
switch fieldNode := fieldNode.(type) {
124136
case *ast.Var:
125137
// If the field is a var, we need to find the value of the var
126138
// To do so, we get the stack where the var is used and search that stack for the var's definition

pkg/server/definition_test.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func TestDefinition(t *testing.T) {
419419
{
420420
name: "goto with overrides: clobber string",
421421
filename: "testdata/goto-overrides.jsonnet",
422-
position: protocol.Position{Line: 40, Character: 30},
422+
position: protocol.Position{Line: 41, Character: 30},
423423
results: []definitionResult{{
424424
targetRange: protocol.Range{
425425
Start: protocol.Position{Line: 24, Character: 4},
@@ -434,7 +434,7 @@ func TestDefinition(t *testing.T) {
434434
{
435435
name: "goto with overrides: clobber nested string",
436436
filename: "testdata/goto-overrides.jsonnet",
437-
position: protocol.Position{Line: 41, Character: 44},
437+
position: protocol.Position{Line: 42, Character: 44},
438438
results: []definitionResult{{
439439
targetRange: protocol.Range{
440440
Start: protocol.Position{Line: 26, Character: 6},
@@ -449,7 +449,7 @@ func TestDefinition(t *testing.T) {
449449
{
450450
name: "goto with overrides: clobber map",
451451
filename: "testdata/goto-overrides.jsonnet",
452-
position: protocol.Position{Line: 42, Character: 28},
452+
position: protocol.Position{Line: 43, Character: 28},
453453
results: []definitionResult{{
454454
targetRange: protocol.Range{
455455
Start: protocol.Position{Line: 28, Character: 4},
@@ -500,7 +500,7 @@ func TestDefinition(t *testing.T) {
500500
targetFilename: "testdata/goto-overrides-base.jsonnet",
501501
targetRange: protocol.Range{
502502
Start: protocol.Position{Line: 19, Character: 2},
503-
End: protocol.Position{Line: 19, Character: 48},
503+
End: protocol.Position{Line: 19, Character: 94},
504504
},
505505
targetSelectionRange: protocol.Range{
506506
Start: protocol.Position{Line: 19, Character: 2},
@@ -566,6 +566,17 @@ func TestDefinition(t *testing.T) {
566566
End: protocol.Position{Line: 4, Character: 11},
567567
},
568568
},
569+
{
570+
targetFilename: "testdata/goto-overrides-imported2.jsonnet",
571+
targetRange: protocol.Range{
572+
Start: protocol.Position{Line: 1, Character: 2},
573+
End: protocol.Position{Line: 3, Character: 3},
574+
},
575+
targetSelectionRange: protocol.Range{
576+
Start: protocol.Position{Line: 1, Character: 2},
577+
End: protocol.Position{Line: 1, Character: 9},
578+
},
579+
},
569580
{
570581
targetFilename: "testdata/goto-overrides-imported.jsonnet",
571582
targetRange: protocol.Range{
@@ -663,6 +674,22 @@ func TestDefinition(t *testing.T) {
663674
},
664675
}},
665676
},
677+
{
678+
name: "goto with overrides: string carried from second import",
679+
filename: "testdata/goto-overrides.jsonnet",
680+
position: protocol.Position{Line: 39, Character: 67},
681+
results: []definitionResult{{
682+
targetFilename: "testdata/goto-overrides-imported2.jsonnet",
683+
targetRange: protocol.Range{
684+
Start: protocol.Position{Line: 2, Character: 4},
685+
End: protocol.Position{Line: 2, Character: 30},
686+
},
687+
targetSelectionRange: protocol.Range{
688+
Start: protocol.Position{Line: 2, Character: 4},
689+
End: protocol.Position{Line: 2, Character: 22},
690+
},
691+
}},
692+
},
666693
}
667694
for _, tc := range testCases {
668695
t.Run(tc.name, func(t *testing.T) {

pkg/server/testdata/goto-overrides-base.jsonnet

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
a+: extensionFromLocal,
1818
}
1919
+ {
20-
a+: (import 'goto-overrides-imported.jsonnet'),
20+
a+: (import 'goto-overrides-imported.jsonnet') + (import 'goto-overrides-imported2.jsonnet'),
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
nested1+: {
3+
from_second_import: 'hey!',
4+
},
5+
}

pkg/server/testdata/goto-overrides.jsonnet

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
carried_nested_string: self.a.nested1.hello2, // This should refer to the initial definition (map 3)
3838
carried_nested_string_from_local: self.a.nested1.from_local, // This should refer to the definition specified in a local in the base file
3939
carried_nested_string_from_import: self.a.nested1.from_import, // This should refer to the definition specified in an import in the base file
40+
carried_nested_string_from_second_import: self.a.nested1.from_second_import, // This should refer to the definition specified in an import in the base file
4041

4142
clobbered_string: self.a.hello2, // This should refer to the override only (map 4)
4243
clobbered_nested_string: self.a.nested1.hello, // This should refer to the override only (map 4)

0 commit comments

Comments
 (0)