From 27e73fb8eb8e7b9be0e7d829e1d790fbc37a963f Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 28 Apr 2024 17:42:30 +0200 Subject: [PATCH] remove old code --- internal/handler/completion.go | 5 +- internal/handler/completion_values_test.go | 19 --- .../language_features/template_context.go | 1 + internal/lsp/ast.go | 49 ++----- internal/lsp/ast_field_identifier_test.go | 127 ------------------ internal/lsp/ast_test.go | 4 +- internal/protocol/completion.go | 1 + 7 files changed, 19 insertions(+), 187 deletions(-) delete mode 100644 internal/lsp/ast_field_identifier_test.go diff --git a/internal/handler/completion.go b/internal/handler/completion.go index 923494a7..79140c38 100644 --- a/internal/handler/completion.go +++ b/internal/handler/completion.go @@ -44,7 +44,7 @@ func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionPara Row: params.Position.Line, Column: params.Position.Character, } - relevantChildNode = lsplocal.FindRelevantChildNode(currentNode, pointToLoopUp) + relevantChildNode = lsplocal.FindRelevantChildNodeCompletion(currentNode, pointToLoopUp) ) genericDocumentUseCase = genericDocumentUseCase.WithNode(relevantChildNode) @@ -165,9 +165,6 @@ func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string case gotemplate.NodeTypeDot: logger.Debug("TraverseIdentifierPathUp for dot node") word = lsplocal.TraverseIdentifierPathUp(relevantChildNode, doc) - case gotemplate.NodeTypeDotSymbol, gotemplate.NodeTypeFieldIdentifier: - logger.Debug("GetFieldIdentifierPath") - word = lsplocal.GetFieldIdentifierPath(relevantChildNode, doc) case gotemplate.NodeTypeText, gotemplate.NodeTypeTemplate: return word, true } diff --git a/internal/handler/completion_values_test.go b/internal/handler/completion_values_test.go index 9c454cdf..179780d5 100644 --- a/internal/handler/completion_values_test.go +++ b/internal/handler/completion_values_test.go @@ -4,10 +4,8 @@ import ( "testing" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" "github.com/mrjosh/helm-ls/pkg/chart" "github.com/stretchr/testify/assert" - "go.lsp.dev/protocol" "gopkg.in/yaml.v3" ) @@ -87,23 +85,6 @@ func TestWrongValues(t *testing.T) { } } -func TestCompletionAstParsing(t *testing.T) { - documentText := `{{ .Values.global. }}` - expectedWord := ".Values.global." - doc := &lsplocal.Document{ - Content: documentText, - Ast: lsplocal.ParseAst(nil, documentText), - } - position := protocol.Position{ - Line: 0, - Character: 18, - } - word, _ := completionAstParsing(doc, position) - if expectedWord != word { - t.Errorf("Expected word '%s', but got '%s'", expectedWord, word) - } -} - func TestGetValuesCompletions(t *testing.T) { handler := &langHandler{ linterName: "helm-lint", diff --git a/internal/language_features/template_context.go b/internal/language_features/template_context.go index 9e7677c7..e07fc12f 100644 --- a/internal/language_features/template_context.go +++ b/internal/language_features/template_context.go @@ -173,6 +173,7 @@ func (f *TemplateContextFeature) Completion() (result *lsp.CompletionList, err e return &lsp.CompletionList{Items: completions, IsIncomplete: false}, nil case "Chart", "Release", "Files", "Capabilities", "Template": + // TODO: make this more fine, by checking the lenght result, ok := helmdocs.BuiltInOjectVals[templateContext[0]] if !ok { result := helmdocs.BuiltInObjects diff --git a/internal/lsp/ast.go b/internal/lsp/ast.go index 9e85aff1..22091827 100644 --- a/internal/lsp/ast.go +++ b/internal/lsp/ast.go @@ -2,7 +2,6 @@ package lsp import ( "context" - "fmt" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" sitter "github.com/smacker/go-tree-sitter" @@ -32,6 +31,19 @@ func FindDirectChildNodeByStart(currentNode *sitter.Node, pointToLookUp sitter.P } func FindRelevantChildNode(currentNode *sitter.Node, pointToLookUp sitter.Point) *sitter.Node { + for i := 0; i < int(currentNode.ChildCount()); i++ { + child := currentNode.Child(i) + if child == nil { + continue + } + if isPointLargerOrEq(pointToLookUp, child.StartPoint()) && isPointLargerOrEq(child.EndPoint(), pointToLookUp) { + return FindRelevantChildNode(child, pointToLookUp) + } + } + return currentNode +} + +func FindRelevantChildNodeCompletion(currentNode *sitter.Node, pointToLookUp sitter.Point) *sitter.Node { childCount := int(currentNode.ChildCount()) for i := childCount - 1; i >= 0; i-- { child := currentNode.Child(i) @@ -39,7 +51,7 @@ func FindRelevantChildNode(currentNode *sitter.Node, pointToLookUp sitter.Point) continue } if isPointLargerOrEq(pointToLookUp, child.StartPoint()) && isPointLargerOrEq(child.EndPoint(), pointToLookUp) { - return FindRelevantChildNode(child, pointToLookUp) + return FindRelevantChildNodeCompletion(child, pointToLookUp) } } return currentNode @@ -52,39 +64,6 @@ func isPointLargerOrEq(a sitter.Point, b sitter.Point) bool { return a.Row > b.Row } -func GetFieldIdentifierPath(node *sitter.Node, doc *Document) (path string) { - path = buildFieldIdentifierPath(node, doc) - logger.Debug(fmt.Sprintf("buildFieldIdentifierPath: %s for node %s with parent %s", path, node, node.Parent())) - - return path -} - -func buildFieldIdentifierPath(node *sitter.Node, doc *Document) string { - prepend := node.PrevNamedSibling() - - currentPath := node.Content([]byte(doc.Content)) - if prepend != nil { - nodeContent := node.Content([]byte(doc.Content)) - if nodeContent == "." { - nodeContent = "" - } - currentPath = prepend.Content([]byte(doc.Content)) + "." + nodeContent - logger.Println("Adding currentpath", currentPath) - } else if node.Parent() != nil && node.Parent().Type() == gotemplate.NodeTypeError { - return buildFieldIdentifierPath(node.Parent(), doc) - } - - if currentPath[0:1] == "$" { - return currentPath - } - - if currentPath[0:1] != "." { - currentPath = "." + currentPath - } - - return TraverseIdentifierPathUp(node, doc) + currentPath -} - func TraverseIdentifierPathUp(node *sitter.Node, doc *Document) string { parent := node.Parent() diff --git a/internal/lsp/ast_field_identifier_test.go b/internal/lsp/ast_field_identifier_test.go deleted file mode 100644 index bb06b23d..00000000 --- a/internal/lsp/ast_field_identifier_test.go +++ /dev/null @@ -1,127 +0,0 @@ -package lsp - -import ( - "testing" - - sitter "github.com/smacker/go-tree-sitter" - "github.com/stretchr/testify/assert" - lsp "go.lsp.dev/protocol" -) - -func TestGetFieldIdentifierPathSimple(t *testing.T) { - template := `{{ .Values.test }}` - - ast := ParseAst(nil, template) - // (template [0, 0] - [1, 0] - // (selector_expression [0, 3] - [0, 15] - // operand: (field [0, 3] - [0, 10] - // name: (identifier [0, 4] - [0, 10])) - // field: (field_identifier [0, 11] - [0, 15])) - - test_start := sitter.Point{Row: 0, Column: 12} - testNode := ast.RootNode().NamedDescendantForPointRange(test_start, test_start) - - if testNode.Content([]byte(template)) != "test" { - t.Errorf("Nodes were not correctly selected") - } - - doc := Document{ - Content: template, - Ast: ast, - } - - result := GetFieldIdentifierPath(testNode, &doc) - assert.Equal(t, ".Values.test", result) -} - -func TestGetFieldIdentifierPathWith(t *testing.T) { - template := `{{ with .Values }}{{ .test }} {{ end }}` - - ast := ParseAst(nil, template) - // (template [0, 0] - [1, 0] - // (with_action [0, 0] - [0, 39] - // condition: (field [0, 8] - [0, 15] - // name: (identifier [0, 9] - [0, 15])) - // consequence: (field [0, 21] - [0, 26] - // name: (identifier [0, 22] - [0, 26])))) - - test_start := sitter.Point{Row: 0, Column: 22} - testNode := ast.RootNode().NamedDescendantForPointRange(test_start, test_start) - - if testNode.Content([]byte(template)) != "test" { - t.Errorf("Nodes were not correctly selected") - } - - doc := Document{ - Content: template, - Ast: ast, - } - - result := GetFieldIdentifierPath(testNode, &doc) - assert.Equal(t, ".Values.test", result) -} - -func TestGetFieldIdentifierPathFunction(t *testing.T) { - template := `{{ and .Values.test1 .Values.test2 }}` - - ast := ParseAst(nil, template) - // (template [0, 0] - [1, 0] - // (function_call [0, 3] - [0, 35] - // function: (identifier [0, 3] - [0, 6]) - // arguments: (argument_list [0, 7] - [0, 35] - // (selector_expression [0, 7] - [0, 20] - // operand: (field [0, 7] - [0, 14] - // name: (identifier [0, 8] - [0, 14])) - // field: (field_identifier [0, 15] - [0, 20])) - // (selector_expression [0, 21] - [0, 34] - // operand: (field [0, 21] - [0, 28] - // name: (identifier [0, 22] - [0, 28])) - // field: (field_identifier [0, 29] - [0, 34]))))) - // - test1_start := sitter.Point{Row: 0, Column: 16} - test2_start := sitter.Point{Row: 0, Column: 33} - test1Node := ast.RootNode().NamedDescendantForPointRange(test1_start, test1_start) - test2Node := ast.RootNode().NamedDescendantForPointRange(test2_start, test2_start) - - test1NodeContent := test1Node.Content([]byte(template)) - test2NodeContent := test2Node.Content([]byte(template)) - - assert.Equal(t, "test1", test1NodeContent, "Nodes were not correctly selected") - assert.Equal(t, "test2", test2NodeContent, "Nodes were not correctly selected") - - doc := Document{ - Content: template, - Ast: ast, - } - - assert.Equal(t, ".Values.test1", GetFieldIdentifierPath(test1Node, &doc)) - assert.Equal(t, ".Values.test2", GetFieldIdentifierPath(test2Node, &doc)) -} - -func TestGetFieldIdentifierPathFunctionForCompletion(t *testing.T) { - template := `{{ and .Values.image .Values. }}` - // | -> complete at dot - - ast := ParseAst(nil, template) - - var ( - position = lsp.Position{Line: 0, Character: 29} - currentNode = NodeAtPosition(ast, position) - pointToLoopUp = sitter.Point{ - Row: position.Line, - Column: position.Character, - } - relevantChildNode = FindRelevantChildNode(currentNode, pointToLoopUp) - ) - - childNodeContent := relevantChildNode.Content([]byte(template)) - - assert.Equal(t, ".", childNodeContent, "Nodes were not correctly selected ") - - doc := Document{ - Content: template, - Ast: ast, - } - - assert.Equal(t, ".Values.", GetFieldIdentifierPath(relevantChildNode, &doc)) -} diff --git a/internal/lsp/ast_test.go b/internal/lsp/ast_test.go index 3caa1a63..775220f6 100644 --- a/internal/lsp/ast_test.go +++ b/internal/lsp/ast_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestFindRelevantChildNode(t *testing.T) { +func TestFindRelevantChildNodeCompletio(t *testing.T) { template := `{{ .Values. }} {{ .Values.re }} @@ -21,7 +21,7 @@ func TestFindRelevantChildNode(t *testing.T) { logger.Println("RootNode:", ast.RootNode().String()) - node := FindRelevantChildNode(ast.RootNode(), sitter.Point{ + node := FindRelevantChildNodeCompletion(ast.RootNode(), sitter.Point{ Row: 0, Column: 11, }) diff --git a/internal/protocol/completion.go b/internal/protocol/completion.go index 2e6859b5..bc47e790 100644 --- a/internal/protocol/completion.go +++ b/internal/protocol/completion.go @@ -27,6 +27,7 @@ func (c *CompletionResult) ToLSP() (result lsp.CompletionItem) { Detail: c.Documentation.Detail, InsertText: c.Documentation.Name, InsertTextFormat: lsp.InsertTextFormatSnippet, + Kind: lsp.CompletionItemKindConstant, } }