Skip to content

Commit 85e63dc

Browse files
Support multiple definitions: Locals (#30)
* Support definitions across multiples files Yet another case for #6 This supports two cases: - Definitions across multiple files - The case where a local is used in the second file. We were currently using the initial stack to find the bind for that var. Across files, that doesn't work, we have to work with the stack where the var is being used * Better tests
1 parent 77a93c3 commit 85e63dc

File tree

4 files changed

+87
-17
lines changed

4 files changed

+87
-17
lines changed

pkg/processing/find_field.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
122122
for _, foundField := range foundFields {
123123
switch fieldNode := foundField.Body.(type) {
124124
case *ast.Var:
125-
bind := FindBindByIdViaStack(stack, fieldNode.Id)
125+
// If the field is a var, we need to find the value of the var
126+
// To do so, we get the stack where the var is used and search that stack for the var's definition
127+
varFileNode, _, _ := vm.ImportAST("", fieldNode.LocRange.FileName)
128+
varStack, err := FindNodeByPosition(varFileNode, fieldNode.Loc().Begin)
129+
if err != nil {
130+
return nil, fmt.Errorf("got the following error when finding the bind for %s: %w", fieldNode.Id, err)
131+
}
132+
bind := FindBindByIdViaStack(varStack, fieldNode.Id)
126133
if bind == nil {
127134
return nil, fmt.Errorf("could not find bind for %s", fieldNode.Id)
128135
}

pkg/server/definition_test.go

+47-3
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: 38, Character: 30},
422+
position: protocol.Position{Line: 39, 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: 39, Character: 44},
437+
position: protocol.Position{Line: 40, 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: 40, Character: 28},
452+
position: protocol.Position{Line: 41, Character: 28},
453453
results: []definitionResult{{
454454
targetRange: protocol.Range{
455455
Start: protocol.Position{Line: 28, Character: 4},
@@ -496,6 +496,28 @@ func TestDefinition(t *testing.T) {
496496
End: protocol.Position{Line: 2, Character: 3},
497497
},
498498
},
499+
{
500+
targetFilename: "testdata/goto-overrides-base.jsonnet",
501+
targetRange: protocol.Range{
502+
Start: protocol.Position{Line: 16, Character: 2},
503+
End: protocol.Position{Line: 16, Character: 24},
504+
},
505+
targetSelectionRange: protocol.Range{
506+
Start: protocol.Position{Line: 16, Character: 2},
507+
End: protocol.Position{Line: 16, Character: 3},
508+
},
509+
},
510+
{
511+
targetFilename: "testdata/goto-overrides-base.jsonnet",
512+
targetRange: protocol.Range{
513+
Start: protocol.Position{Line: 1, Character: 2},
514+
End: protocol.Position{Line: 7, Character: 3},
515+
},
516+
targetSelectionRange: protocol.Range{
517+
Start: protocol.Position{Line: 1, Character: 2},
518+
End: protocol.Position{Line: 1, Character: 3},
519+
},
520+
},
499521
},
500522
},
501523
{
@@ -533,6 +555,28 @@ func TestDefinition(t *testing.T) {
533555
End: protocol.Position{Line: 4, Character: 11},
534556
},
535557
},
558+
{
559+
targetFilename: "testdata/goto-overrides-base.jsonnet",
560+
targetRange: protocol.Range{
561+
Start: protocol.Position{Line: 12, Character: 4},
562+
End: protocol.Position{Line: 14, Character: 5},
563+
},
564+
targetSelectionRange: protocol.Range{
565+
Start: protocol.Position{Line: 12, Character: 4},
566+
End: protocol.Position{Line: 12, Character: 11},
567+
},
568+
},
569+
{
570+
targetFilename: "testdata/goto-overrides-base.jsonnet",
571+
targetRange: protocol.Range{
572+
Start: protocol.Position{Line: 3, Character: 4},
573+
End: protocol.Position{Line: 5, Character: 5},
574+
},
575+
targetSelectionRange: protocol.Range{
576+
Start: protocol.Position{Line: 3, Character: 4},
577+
End: protocol.Position{Line: 3, Character: 11},
578+
},
579+
},
536580
},
537581
},
538582
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
a: {
3+
hello: 'this will be clobbered',
4+
nested1: {
5+
hello: 'this will be clobbered',
6+
},
7+
nested2: {},
8+
},
9+
10+
}
11+
+ {
12+
local extensionFromLocal = {
13+
nested1+: {
14+
from_local: 'hey!',
15+
},
16+
},
17+
a+: extensionFromLocal,
18+
}
+14-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{
2-
// 1. Initial definition
3-
a: {
1+
(import 'goto-overrides-base.jsonnet') // 1. Initial definition from base file
2+
{ // 2. Override nested string
3+
a+: {
44
hello: 'world',
5-
nested1: {
5+
nested1+: {
66
hello: 'world',
77
},
88
nested2: {
@@ -11,7 +11,7 @@
1111
},
1212
}
1313
+ {
14-
// 2. Override maps but keep string keys
14+
// 3. Override maps but keep string keys
1515
a+: {
1616
hello2: 'world2',
1717
nested1+: {
@@ -20,7 +20,7 @@
2020
},
2121
}
2222
+ {
23-
// 3. Clobber some attributes
23+
// 4. Clobber some attributes
2424
a+: {
2525
hello2: 'clobbered', // Clobber a string
2626
nested1+: {
@@ -30,13 +30,14 @@
3030
},
3131
}
3232
+ {
33-
map_overrides: self.a, // This should refer to all three definitions (initial + 2 overrides)
34-
nested_map_overrides: self.a.nested1, // This should refer to all three definitions (initial + 2 overrides)
33+
map_overrides: self.a, // This should refer to all definitions
34+
nested_map_overrides: self.a.nested1, // This should refer to all definitions
3535

36-
carried_string: self.a.hello, // This should refer to the initial definition (map 1)
37-
carried_nested_string: self.a.nested1.hello2, // This should refer to the initial definition (map 2)
36+
carried_string: self.a.hello, // This should refer to the initial definition (map 2)
37+
carried_nested_string: self.a.nested1.hello2, // This should refer to the initial definition (map 3)
38+
carried_nested_string_from_local: self.a.nested1.from_local, // This should refer to the definition specified in a local in the base file
3839

39-
clobbered_string: self.a.hello2, // This should refer to the override only (map 3)
40-
clobbered_nested_string: self.a.nested1.hello, // This should refer to the override only (map 3)
41-
clobbered_map: self.a.nested2, // This should refer to the override only (map 3)
40+
clobbered_string: self.a.hello2, // This should refer to the override only (map 4)
41+
clobbered_nested_string: self.a.nested1.hello, // This should refer to the override only (map 4)
42+
clobbered_map: self.a.nested2, // This should refer to the override only (map 4)
4243
}

0 commit comments

Comments
 (0)