Skip to content

Commit e164149

Browse files
Support multiple definitions from imports (#31)
In a multiple definitions context, having an import somewhere along the line would cut the chain since it overwrote the `foundDesugaredObjects` slice This builds upon #30 Issue: #6
1 parent 85e63dc commit e164149

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

pkg/processing/find_field.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
149149
case *ast.Import:
150150
filename := fieldNode.File.Value
151151
rootNode, _, _ := vm.ImportAST(string(fieldNode.Loc().File.DiagnosticFileName), filename)
152-
foundDesugaredObjects = findTopLevelObjects(nodestack.NewNodeStack(rootNode), vm)
152+
foundDesugaredObjects = append(foundDesugaredObjects, findTopLevelObjects(nodestack.NewNodeStack(rootNode), vm)...)
153153
}
154154
}
155155
}

pkg/server/definition_test.go

+58-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: 39, Character: 30},
422+
position: protocol.Position{Line: 40, 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: 40, Character: 44},
437+
position: protocol.Position{Line: 41, 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: 41, Character: 28},
452+
position: protocol.Position{Line: 42, Character: 28},
453453
results: []definitionResult{{
454454
targetRange: protocol.Range{
455455
Start: protocol.Position{Line: 28, Character: 4},
@@ -496,6 +496,17 @@ 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: 19, Character: 2},
503+
End: protocol.Position{Line: 19, Character: 48},
504+
},
505+
targetSelectionRange: protocol.Range{
506+
Start: protocol.Position{Line: 19, Character: 2},
507+
End: protocol.Position{Line: 19, Character: 3},
508+
},
509+
},
499510
{
500511
targetFilename: "testdata/goto-overrides-base.jsonnet",
501512
targetRange: protocol.Range{
@@ -555,6 +566,17 @@ func TestDefinition(t *testing.T) {
555566
End: protocol.Position{Line: 4, Character: 11},
556567
},
557568
},
569+
{
570+
targetFilename: "testdata/goto-overrides-imported.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+
},
558580
{
559581
targetFilename: "testdata/goto-overrides-base.jsonnet",
560582
targetRange: protocol.Range{
@@ -609,6 +631,38 @@ func TestDefinition(t *testing.T) {
609631
},
610632
}},
611633
},
634+
{
635+
name: "goto with overrides: string carried from local",
636+
filename: "testdata/goto-overrides.jsonnet",
637+
position: protocol.Position{Line: 37, Character: 57},
638+
results: []definitionResult{{
639+
targetFilename: "testdata/goto-overrides-base.jsonnet",
640+
targetRange: protocol.Range{
641+
Start: protocol.Position{Line: 13, Character: 6},
642+
End: protocol.Position{Line: 13, Character: 24},
643+
},
644+
targetSelectionRange: protocol.Range{
645+
Start: protocol.Position{Line: 13, Character: 6},
646+
End: protocol.Position{Line: 13, Character: 16},
647+
},
648+
}},
649+
},
650+
{
651+
name: "goto with overrides: string carried from import",
652+
filename: "testdata/goto-overrides.jsonnet",
653+
position: protocol.Position{Line: 38, Character: 57},
654+
results: []definitionResult{{
655+
targetFilename: "testdata/goto-overrides-imported.jsonnet",
656+
targetRange: protocol.Range{
657+
Start: protocol.Position{Line: 2, Character: 4},
658+
End: protocol.Position{Line: 2, Character: 23},
659+
},
660+
targetSelectionRange: protocol.Range{
661+
Start: protocol.Position{Line: 2, Character: 4},
662+
End: protocol.Position{Line: 2, Character: 15},
663+
},
664+
}},
665+
},
612666
}
613667
for _, tc := range testCases {
614668
t.Run(tc.name, func(t *testing.T) {
@@ -625,7 +679,7 @@ func TestDefinition(t *testing.T) {
625679
server.getVM = testGetVM
626680
serverOpenTestFile(t, server, string(tc.filename))
627681
response, err := server.definitionLink(context.Background(), params, false)
628-
assert.NoError(t, err)
682+
require.NoError(t, err)
629683

630684
var expected []protocol.DefinitionLink
631685
for _, r := range tc.results {

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

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
},
1717
a+: extensionFromLocal,
1818
}
19+
+ {
20+
a+: (import 'goto-overrides-imported.jsonnet'),
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
nested1+: {
3+
from_import: 'hey!',
4+
},
5+
}

pkg/server/testdata/goto-overrides.jsonnet

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
carried_string: self.a.hello, // This should refer to the initial definition (map 2)
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
39+
carried_nested_string_from_import: self.a.nested1.from_import, // This should refer to the definition specified in an import in the base file
3940

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

0 commit comments

Comments
 (0)