Skip to content

Commit

Permalink
fix(symbol_table): unfinished selector expression in ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jun 30, 2024
1 parent 7fba236 commit 93a4b99
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
6 changes: 5 additions & 1 deletion internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func NewTemplateContext(string string) TemplateContext {
if string == "." {
return TemplateContext{}
}
return strings.Split(string, ".")
splitted := strings.Split(string, ".")
if len(splitted) > 0 && splitted[0] == "" {
return splitted[1:]
}
return splitted
}

func ensureNoLeadingDot(context string) string {
Expand Down
9 changes: 5 additions & 4 deletions internal/lsp/symbol_table_template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) {
v.symbolTable.AddTemplateContext(append(v.currentContext, content), GetRangeForNode(node.ChildByFieldName("name")))
case gotemplate.NodeTypeUnfinishedSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable {
v.StashContext()
content := getContextForSelectorExpression(operandNode, v.content)
if !content.IsVariable() {
content = append(v.currentContext, content...)
}
v.symbolTable.AddTemplateContext(append(append(v.currentContext, getContextForSelectorExpression(operandNode, v.content)...), ""),
v.symbolTable.AddTemplateContext(append(content, ""),
GetRangeForNode(node.Child(int(node.ChildCount())-1)))
case gotemplate.NodeTypeSelectorExpression:
operandNode := node.ChildByFieldName("operand")
Expand All @@ -79,7 +80,7 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) {

func (v *TemplateContextVisitor) Exit(node *sitter.Node) {
switch node.Type() {
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
case gotemplate.NodeTypeSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable {
v.PopContext()
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/symbol_table_template_context_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestResolveVariablesInTemplateContext(t *testing.T) {
expectedCtx TemplateContext
expectedErr error
}{
{"{{ $values := .Values }} {{ $values.te^st }}", TemplateContext{"$values", "test"}, TemplateContext{"", "Values", "test"}, nil},
{"{{- range $type, $config := .Values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"", "Values", "deployments[]", "test"}, nil},
{" {{ $values := .Values }} {{- range $type, $config := $values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"", "Values", "deployments[]", "test"}, nil},
{"{{ $values := .Values }} {{ $values.te^st }}", TemplateContext{"$values", "test"}, TemplateContext{"Values", "test"}, nil},
{"{{- range $type, $config := .Values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"Values", "deployments[]", "test"}, nil},
{" {{ $values := .Values }} {{- range $type, $config := $values.deployments }} {{ $config.te^st }}", TemplateContext{"$config", "test"}, TemplateContext{"Values", "deployments[]", "test"}, nil},
}

for _, tt := range tests {
Expand Down
11 changes: 10 additions & 1 deletion internal/lsp/symbol_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ func TestSymbolTableForValuesSingleTests(t *testing.T) {
},
foundContextsLen: 5,
},
{
template: `{{- range $type, $config := .Values.deployments }} {{ $config.test.nested. }} {{ end }} `,
path: []string{"$config", "test", "nested", ""},
startPoint: sitter.Point{
Row: 0,
Column: 73,
},
foundContextsLen: 5,
},
}

for _, v := range testCases {
Expand All @@ -322,7 +331,7 @@ func TestSymbolTableForValuesSingleTests(t *testing.T) {
for _, v := range values {
points = append(points, v.StartPoint)
}
assert.Contains(t, points, v.startPoint)
assert.Contains(t, points, v.startPoint, "Ast was %s", ast.RootNode())
assert.Len(t, symbolTable.contexts, v.foundContextsLen)
})
}
Expand Down
File renamed without changes.

0 comments on commit 93a4b99

Please sign in to comment.