From 93a4b99e9d1545ff04135dfed5476a6771ab50dc Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 30 Jun 2024 18:47:55 +0200 Subject: [PATCH] fix(symbol_table): unfinished selector expression in ranges --- internal/lsp/symbol_table.go | 6 +++++- internal/lsp/symbol_table_template_context.go | 9 +++++---- .../symbol_table_template_context_variables_test.go | 6 +++--- internal/lsp/symbol_table_test.go | 11 ++++++++++- ...ariable_test.go => symbol_table_variables_test.go} | 0 5 files changed, 23 insertions(+), 9 deletions(-) rename internal/lsp/{symbol_table_variable_test.go => symbol_table_variables_test.go} (100%) 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