Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
for _, foundField := range foundFields {
switch fieldNode := foundField.Body.(type) {
case *ast.Var:
bind := FindBindByIdViaStack(stack, fieldNode.Id)
// If the field is a var, we need to find the value of the var
// To do so, we get the stack where the var is used and search that stack for the var's definition
varFileNode, _, _ := vm.ImportAST("", fieldNode.LocRange.FileName)
varStack, err := FindNodeByPosition(varFileNode, fieldNode.Loc().Begin)
if err != nil {
return nil, fmt.Errorf("got the following error when finding the bind for %s: %w", fieldNode.Id, err)
}
bind := FindBindByIdViaStack(varStack, fieldNode.Id)
if bind == nil {
return nil, fmt.Errorf("could not find bind for %s", fieldNode.Id)
}
Expand Down
50 changes: 47 additions & 3 deletions pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func TestDefinition(t *testing.T) {
{
name: "goto with overrides: clobber string",
filename: "testdata/goto-overrides.jsonnet",
position: protocol.Position{Line: 38, Character: 30},
position: protocol.Position{Line: 39, Character: 30},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 24, Character: 4},
Expand All @@ -434,7 +434,7 @@ func TestDefinition(t *testing.T) {
{
name: "goto with overrides: clobber nested string",
filename: "testdata/goto-overrides.jsonnet",
position: protocol.Position{Line: 39, Character: 44},
position: protocol.Position{Line: 40, Character: 44},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 26, Character: 6},
Expand All @@ -449,7 +449,7 @@ func TestDefinition(t *testing.T) {
{
name: "goto with overrides: clobber map",
filename: "testdata/goto-overrides.jsonnet",
position: protocol.Position{Line: 40, Character: 28},
position: protocol.Position{Line: 41, Character: 28},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 28, Character: 4},
Expand Down Expand Up @@ -496,6 +496,28 @@ func TestDefinition(t *testing.T) {
End: protocol.Position{Line: 2, Character: 3},
},
},
{
targetFilename: "testdata/goto-overrides-base.jsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 16, Character: 2},
End: protocol.Position{Line: 16, Character: 24},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 16, Character: 2},
End: protocol.Position{Line: 16, Character: 3},
},
},
{
targetFilename: "testdata/goto-overrides-base.jsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 7, Character: 3},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 3},
},
},
},
},
{
Expand Down Expand Up @@ -533,6 +555,28 @@ func TestDefinition(t *testing.T) {
End: protocol.Position{Line: 4, Character: 11},
},
},
{
targetFilename: "testdata/goto-overrides-base.jsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 12, Character: 4},
End: protocol.Position{Line: 14, Character: 5},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 12, Character: 4},
End: protocol.Position{Line: 12, Character: 11},
},
},
{
targetFilename: "testdata/goto-overrides-base.jsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 3, Character: 4},
End: protocol.Position{Line: 5, Character: 5},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 3, Character: 4},
End: protocol.Position{Line: 3, Character: 11},
},
},
},
},
{
Expand Down
18 changes: 18 additions & 0 deletions pkg/server/testdata/goto-overrides-base.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
a: {
hello: 'this will be clobbered',
nested1: {
hello: 'this will be clobbered',
},
nested2: {},
},

}
+ {
local extensionFromLocal = {
nested1+: {
from_local: 'hey!',
},
},
a+: extensionFromLocal,
}
27 changes: 14 additions & 13 deletions pkg/server/testdata/goto-overrides.jsonnet
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
// 1. Initial definition
a: {
(import 'goto-overrides-base.jsonnet') // 1. Initial definition from base file
{ // 2. Override nested string
a+: {
hello: 'world',
nested1: {
nested1+: {
hello: 'world',
},
nested2: {
Expand All @@ -11,7 +11,7 @@
},
}
+ {
// 2. Override maps but keep string keys
// 3. Override maps but keep string keys
a+: {
hello2: 'world2',
nested1+: {
Expand All @@ -20,7 +20,7 @@
},
}
+ {
// 3. Clobber some attributes
// 4. Clobber some attributes
a+: {
hello2: 'clobbered', // Clobber a string
nested1+: {
Expand All @@ -30,13 +30,14 @@
},
}
+ {
map_overrides: self.a, // This should refer to all three definitions (initial + 2 overrides)
nested_map_overrides: self.a.nested1, // This should refer to all three definitions (initial + 2 overrides)
map_overrides: self.a, // This should refer to all definitions
nested_map_overrides: self.a.nested1, // This should refer to all definitions

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

clobbered_string: self.a.hello2, // This should refer to the override only (map 3)
clobbered_nested_string: self.a.nested1.hello, // This should refer to the override only (map 3)
clobbered_map: self.a.nested2, // This should refer to the override only (map 3)
clobbered_string: self.a.hello2, // This should refer to the override only (map 4)
clobbered_nested_string: self.a.nested1.hello, // This should refer to the override only (map 4)
clobbered_map: self.a.nested2, // This should refer to the override only (map 4)
}