diff --git a/internal/lsp/symbol_table.go b/internal/lsp/symbol_table.go index 47630c4..9689b53 100644 --- a/internal/lsp/symbol_table.go +++ b/internal/lsp/symbol_table.go @@ -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 { diff --git a/internal/lsp/symbol_table_template_context.go b/internal/lsp/symbol_table_template_context.go index b80d845..0becc8c 100644 --- a/internal/lsp/symbol_table_template_context.go +++ b/internal/lsp/symbol_table_template_context.go @@ -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") @@ -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() diff --git a/internal/lsp/symbol_table_template_context_variables_test.go b/internal/lsp/symbol_table_template_context_variables_test.go index cb2a214..b5e6c58 100644 --- a/internal/lsp/symbol_table_template_context_variables_test.go +++ b/internal/lsp/symbol_table_template_context_variables_test.go @@ -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 { diff --git a/internal/lsp/symbol_table_test.go b/internal/lsp/symbol_table_test.go index 462acce..96d30fa 100644 --- a/internal/lsp/symbol_table_test.go +++ b/internal/lsp/symbol_table_test.go @@ -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 { @@ -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) }) } diff --git a/internal/lsp/symbol_table_variable_test.go b/internal/lsp/symbol_table_variables_test.go similarity index 100% rename from internal/lsp/symbol_table_variable_test.go rename to internal/lsp/symbol_table_variables_test.go