Skip to content

Commit

Permalink
references for templateContext
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 7, 2024
1 parent a53b5ce commit cbf9523
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
5 changes: 5 additions & 0 deletions internal/handler/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func (h *langHandler) Hover(ctx context.Context, params *lsp.HoverParams) (resul
response, err := h.yamllsConnector.CallHover(ctx, *params, word)
return response, err
}
// if (pt == gotemplate.NodeTypeField && ct == gotemplate.NodeTypeIdentifier) || ct == gotemplate.NodeTypeFieldIdentifier || ct == gotemplate.NodeTypeField {
// valuesFeature := languagefeatures.NewValuesFeature(genericDocumentUseCase)
// response, err := valuesFeature.Hover()
// return util.BuildHoverResponse(response, wordRange), err
// }
if pt == gotemplate.NodeTypeFunctionCall && ct == gotemplate.NodeTypeIdentifier {
word = currentNode.Content([]byte(doc.Content))
}
Expand Down
92 changes: 82 additions & 10 deletions internal/handler/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"context"
"os"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
Expand Down Expand Up @@ -127,16 +128,16 @@ func TestRefercesTemplateContext(t *testing.T) {
{{ .Values.test.nested }}
{{ .Values.test }}
`
expected := []lsp.Location{
expectedValues := []lsp.Location{
{
URI: uri.File("/tmp/testfile.yaml"),
Range: protocol.Range{
Start: protocol.Position{
Line: 0x1, Character: 0x3,
Line: 0x1, Character: 4,
},
End: protocol.Position{
Line: 0x1,
Character: 0x13,
Character: 0xa,
},
},
},
Expand All @@ -145,24 +146,24 @@ func TestRefercesTemplateContext(t *testing.T) {
Range: protocol.Range{
Start: protocol.Position{
Line: 0x2,
Character: 0x3,
Character: 0x4,
},
End: protocol.Position{
Line: 0x2,
Character: 0x13,
Character: 0xa,
},
},
},
protocol.Location{
URI: uri.File("/tmp/testfile.yaml"),
Range: protocol.Range{
Start: protocol.Position{
Line: 0x0,
Character: 0x0,
Line: 0x3,
Character: 0x4,
},
End: protocol.Position{
Line: 0x0,
Character: 0x1c,
Line: 0x3,
Character: 0xa,
},
},
},
Expand All @@ -179,7 +180,7 @@ func TestRefercesTemplateContext(t *testing.T) {
Line: 1,
Character: 8,
},
expected: expected,
expected: expectedValues,
},
}

Expand Down Expand Up @@ -219,3 +220,74 @@ func TestRefercesTemplateContext(t *testing.T) {
})
}
}

func TestRefercesTemplateContextWithTestFile(t *testing.T) {
testCases := []struct {
desc string
position lsp.Position
expectedStartPoints []lsp.Position
expectedError error
}{
{
desc: "Test references on .Values",
position: lsp.Position{
Line: 25,
Character: 18,
},
expectedStartPoints: []lsp.Position{{Line: 25, Character: 16}, {Line: 31, Character: 20}},
},
{
desc: "Test references on .Values.imagePullSecrets",
position: lsp.Position{
Line: 25,
Character: 31,
},
expectedStartPoints: []lsp.Position{{Line: 25, Character: 23}},
},
}

for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
documents := lsplocal.NewDocumentStore()

path := "../../testdata/example/templates/deployment.yaml"
fileURI := uri.File(path)

content, err := os.ReadFile(path)
if err != nil {
t.Fatal("Could not read test file", err)
}

d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
LanguageID: "",
Version: 0,
Text: string(content),
},
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
result, err := h.References(context.Background(), &lsp.ReferenceParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: fileURI,
},
Position: tt.position,
},
})
assert.Equal(t, tt.expectedError, err)
startPoints := []lsp.Position{}
for _, location := range result {
startPoints = append(startPoints, location.Range.Start)
}
for _, expected := range tt.expectedStartPoints {
assert.Contains(t, startPoints, expected)
}
})
}
}
6 changes: 6 additions & 0 deletions internal/language_features/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package languagefeatures

import (
"fmt"
"strings"

lsp "go.lsp.dev/protocol"

Expand Down Expand Up @@ -48,3 +49,8 @@ func (f *ValuesFeature) getReferenceLocations(templateContext lsplocal.TemplateC

return locations
}

func (f *ValuesFeature) Hover() (string, error) {
templateContext, err := f.getTemplateContext()
return strings.Join(templateContext, " "), err
}
4 changes: 3 additions & 1 deletion internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func NewSymbolTable(ast *sitter.Tree, content []byte) *SymbolTable {

func (s *SymbolTable) AddValue(templateContext []string, pointRange sitter.Range) {
s.contexts[strings.Join(templateContext, ".")] = append(s.contexts[strings.Join(templateContext, ".")], pointRange)
s.contextsReversed[pointRange] = templateContext
sliceCopy := make(TemplateContext, len(templateContext))
copy(sliceCopy, templateContext)
s.contextsReversed[pointRange] = sliceCopy
}

func (s *SymbolTable) GetValues(path []string) []sitter.Range {
Expand Down
37 changes: 36 additions & 1 deletion internal/lsp/symbol_table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lsp

import (
"os"
"testing"

sitter "github.com/smacker/go-tree-sitter"
Expand All @@ -24,7 +25,6 @@ func TestSymbolTableForIncludeDefinitions(t *testing.T) {

assert.Len(t, symbolTable.includeDefinitions, 2)

// TODO: remove the double quotes
assert.Equal(t, symbolTable.includeDefinitions["bar"], []sitter.Range{{
StartPoint: sitter.Point{
Row: 5,
Expand Down Expand Up @@ -167,3 +167,38 @@ func TestSymbolTableForValues(t *testing.T) {
assert.Contains(t, points, v.startPoint)
}
}

func TestSymbolTableForValuesTestFile(t *testing.T) {
path := "../../testdata/example/templates/deployment.yaml"

content, err := os.ReadFile(path)
if err != nil {
t.Fatal("Could not read test file", err)
}
ast := ParseAst(nil, string(content))

symbolTable := NewSymbolTable(ast, []byte(content))
type expectedValue struct {
path []string
startPoint sitter.Point
}

expected := []expectedValue{
{
path: []string{"Values", "ingress"},
startPoint: sitter.Point{
Row: 5,
Column: 3,
},
},
}

for _, v := range expected {
values := symbolTable.GetValues(v.path)
points := []sitter.Point{}
for _, v := range values {
points = append(points, v.StartPoint)
}
assert.Contains(t, points, v.startPoint)
}
}
2 changes: 1 addition & 1 deletion internal/lsp/symbol_table_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v *ValuesVisitor) Enter(node *sitter.Node) {
v.symbolTable.AddValue(append(v.currentContext, content), GetRangeForNode(node))
case gotemplate.NodeTypeField:
content := node.ChildByFieldName("name").Content(v.content)
v.symbolTable.AddValue(append(v.currentContext, content), GetRangeForNode(node))
v.symbolTable.AddValue(append(v.currentContext, content), GetRangeForNode(node.ChildByFieldName("name")))
case gotemplate.NodeTypeSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
Expand Down

0 comments on commit cbf9523

Please sign in to comment.