Skip to content

Commit

Permalink
Fix: Go-to definition on recursive binary operators (#126)
Browse files Browse the repository at this point in the history
* Fix: Go-to definition on recursive binary operators
Included the grafonnet example where the bug was identified as a testcase
From now on, we can use grafonnet to build test cases

* Fix formatting + linting
  • Loading branch information
julienduchesne authored Sep 7, 2023
1 parent 4117c7b commit 02d502b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
11 changes: 9 additions & 2 deletions pkg/ast/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
return ranges, nil
}

func flattenBinary(node ast.Node) []ast.Node {
binary, nodeIsBinary := node.(*ast.Binary)
if !nodeIsBinary {
return []ast.Node{node}
}
return append(flattenBinary(binary.Right), flattenBinary(binary.Left)...)
}

// unpackFieldNodes extracts nodes from fields
// - Binary nodes. A field could be either in the left or right side of the binary
// - Self nodes. We want the object self refers to, not the self node itself
Expand All @@ -182,8 +190,7 @@ func unpackFieldNodes(vm *jsonnet.VM, fields []*ast.DesugaredObjectField) ([]ast
}
}
case *ast.Binary:
fieldNodes = append(fieldNodes, fieldNode.Right)
fieldNodes = append(fieldNodes, fieldNode.Left)
fieldNodes = append(fieldNodes, flattenBinary(fieldNode)...)
default:
fieldNodes = append(fieldNodes, fieldNode)
}
Expand Down
19 changes: 18 additions & 1 deletion pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
_ "embed"
"fmt"
"path/filepath"
"testing"

"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
Expand Down Expand Up @@ -926,6 +927,22 @@ var definitionTestCases = []definitionTestCase{
},
}},
},
{
name: "grafonnet: eval variable selection options",
filename: "./testdata/grafonnet/eval-variable-options.jsonnet",
position: protocol.Position{Line: 5, Character: 54},
results: []definitionResult{{
targetFilename: "testdata/grafonnet/vendor/github.com/grafana/grafonnet/gen/grafonnet-v10.0.0/custom/dashboard/variable.libsonnet",
targetRange: protocol.Range{
Start: protocol.Position{Line: 101, Character: 12},
End: protocol.Position{Line: 103, Character: 13},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 101, Character: 12},
End: protocol.Position{Line: 101, Character: 21},
},
}},
},
}

func TestDefinition(t *testing.T) {
Expand All @@ -941,7 +958,7 @@ func TestDefinition(t *testing.T) {
}

server := NewServer("any", "test version", nil, Configuration{
JPaths: []string{"testdata"},
JPaths: []string{"testdata", filepath.Join(filepath.Dir(tc.filename), "vendor")},
})
serverOpenTestFile(t, server, tc.filename)
response, err := server.definitionLink(params)
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/testdata/grafonnet/eval-variable-options.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-v10.0.0/main.libsonnet';

g.dashboard.new('title')
+ g.dashboard.withVariables([
g.dashboard.variable.custom.new('var', ['a'])
+ g.dashboard.variable.custom.selectionOptions.withMulti(),
])

0 comments on commit 02d502b

Please sign in to comment.