From 496e81645ef614bdc1b108ad8f8f8471c324eb25 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sat, 29 Jun 2024 19:13:52 +0200 Subject: [PATCH] feat(variables): scope includes definition --- internal/handler/definition_test.go | 19 +++++++++++++++++++ ...iables_visitor.go => variables_visitor.go} | 4 ++-- ...itor_test.go => variables_visitor_test.go} | 8 ++++---- 3 files changed, 25 insertions(+), 6 deletions(-) rename internal/lsp/{symobol_table_variables_visitor.go => variables_visitor.go} (97%) rename internal/lsp/{symbol_table_variables_visitor_test.go => variables_visitor_test.go} (87%) diff --git a/internal/handler/definition_test.go b/internal/handler/definition_test.go index 388c7a3..6169cd9 100644 --- a/internal/handler/definition_test.go +++ b/internal/handler/definition_test.go @@ -29,6 +29,7 @@ var testFileContent = ` {{ range .Values.list }} {{ . }} # line 12 {{ end }} +{{ range $index, $element := pipeline }}{{ $index }}{{ $element }}{{ end }} # line 14 ` var ( @@ -132,6 +133,24 @@ func TestDefinitionRange(t *testing.T) { }, nil) } +// Input: +// {{ range $index, $element := pipeline }}{{ $index }}{{ $element }}{{ end }} # line 14 +// ---------------------| +// Expected: +// {{ range $index, $element := pipeline }}{{ $index }}{{ $element }}{{ end }} # line 14 +// -----------------| +func TestDefinitionRangeOnRedeclaration(t *testing.T) { + genericDefinitionTest(t, lsp.Position{Line: 14, Character: 23}, []lsp.Location{ + { + URI: testDocumentTemplateURI, + Range: lsp.Range{ + Start: lsp.Position{Line: 14, Character: 17}, + End: lsp.Position{Line: 14, Character: 37}, + }, + }, + }, nil) +} + // Input: // {{ .Values.foo }} # line 8 // ------------| diff --git a/internal/lsp/symobol_table_variables_visitor.go b/internal/lsp/variables_visitor.go similarity index 97% rename from internal/lsp/symobol_table_variables_visitor.go rename to internal/lsp/variables_visitor.go index 343a484..e40cf15 100644 --- a/internal/lsp/symobol_table_variables_visitor.go +++ b/internal/lsp/variables_visitor.go @@ -98,9 +98,9 @@ func (v *VariablesVisitor) addVariableDefinition(variableType VariableType, defi Value: variableValueNode.Content(v.content), VariableType: variableType, Scope: sitter.Range{ - StartPoint: definitionNode.EndPoint(), + StartPoint: definitionNode.StartPoint(), EndPoint: v.currentScope().EndPoint(), - StartByte: definitionNode.EndByte(), + StartByte: definitionNode.StartByte(), EndByte: v.currentScope().EndByte(), }, Range: sitter.Range{ diff --git a/internal/lsp/symbol_table_variables_visitor_test.go b/internal/lsp/variables_visitor_test.go similarity index 87% rename from internal/lsp/symbol_table_variables_visitor_test.go rename to internal/lsp/variables_visitor_test.go index 621a7e0..7845344 100644 --- a/internal/lsp/symbol_table_variables_visitor_test.go +++ b/internal/lsp/variables_visitor_test.go @@ -22,7 +22,7 @@ func TestSymbolTableForVariableDefinitions(t *testing.T) { { Value: ".", VariableType: VariableTypeAssigment, - Scope: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 14}, EndPoint: sitter.Point{Row: 0, Column: 19}, StartByte: 14, EndByte: 19}, + Scope: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 4}, EndPoint: sitter.Point{Row: 0, Column: 19}, StartByte: 4, EndByte: 19}, Range: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 4}, EndPoint: sitter.Point{Row: 0, Column: 14}, StartByte: 4, EndByte: 14}, }, }, @@ -39,7 +39,7 @@ func TestSymbolTableForVariableDefinitions(t *testing.T) { { Value: "$root.Values.deployments", VariableType: VariableTypeRangeKeyOrIndex, - Scope: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 60}, EndPoint: sitter.Point{Row: 3, Column: 15}, StartByte: 61, EndByte: 101}, + Scope: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 18}, EndPoint: sitter.Point{Row: 3, Column: 15}, StartByte: 19, EndByte: 101}, Range: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 18}, EndPoint: sitter.Point{Row: 1, Column: 60}, StartByte: 19, EndByte: 61}, }, }, @@ -47,7 +47,7 @@ func TestSymbolTableForVariableDefinitions(t *testing.T) { { Value: "$root.Values.deployments", VariableType: VariableTypeRangeValue, - Scope: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 60}, EndPoint: sitter.Point{Row: 3, Column: 15}, StartByte: 61, EndByte: 101}, + Scope: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 18}, EndPoint: sitter.Point{Row: 3, Column: 15}, StartByte: 19, EndByte: 101}, Range: sitter.Range{StartPoint: sitter.Point{Row: 1, Column: 25}, EndPoint: sitter.Point{Row: 1, Column: 60}, StartByte: 26, EndByte: 61}, }, }, @@ -60,7 +60,7 @@ func TestSymbolTableForVariableDefinitions(t *testing.T) { { Value: ".Values", VariableType: VariableTypeAssigment, - Scope: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 16}, EndPoint: sitter.Point{Row: 0, Column: 50}, StartByte: 16, EndByte: 50}, + Scope: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 3}, EndPoint: sitter.Point{Row: 0, Column: 50}, StartByte: 3, EndByte: 50}, Range: sitter.Range{StartPoint: sitter.Point{Row: 0, Column: 3}, EndPoint: sitter.Point{Row: 0, Column: 16}, StartByte: 3, EndByte: 16}, }, },