Skip to content

Commit

Permalink
fix(completion): complete after dot on selector expression
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jan 12, 2024
1 parent 72617a6 commit b6a39c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
6 changes: 3 additions & 3 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *langHandler) handleTextDocumentCompletion(ctx context.Context, reply js
variableSplitted = append(variableSplitted, s)
}

logger.Println(fmt.Sprintf("Word < %s >", word))
logger.Println(fmt.Sprintf("Word found for completions is < %s >", word))

if len(variableSplitted) == 0 {
return reply(ctx, basicItems, err)
Expand Down Expand Up @@ -115,13 +115,13 @@ func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string
)

logger.Debug("currentNode", currentNode)
logger.Debug("relevantChildNode", relevantChildNode.Type())
logger.Debug("relevantChildNode", relevantChildNode)

switch relevantChildNode.Type() {
case gotemplate.NodeTypeIdentifier:
word = relevantChildNode.Content([]byte(doc.Content))
case gotemplate.NodeTypeDot:
logger.Debug("TraverseIdentifierPathUp")
logger.Debug("TraverseIdentifierPathUp for dot node")
word = lsplocal.TraverseIdentifierPathUp(relevantChildNode, doc)
case gotemplate.NodeTypeDotSymbol:
logger.Debug("GetFieldIdentifierPath")
Expand Down
8 changes: 5 additions & 3 deletions internal/lsp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lsp

import (
"context"
"fmt"

"github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate"
sitter "github.com/smacker/go-tree-sitter"
Expand Down Expand Up @@ -49,7 +50,8 @@ func isPointLargerOrEq(a sitter.Point, b sitter.Point) bool {

func GetFieldIdentifierPath(node *sitter.Node, doc *Document) (path string) {
path = buildFieldIdentifierPath(node, doc)
logger.Debug("buildFieldIdentifierPath:", path)
logger.Debug(fmt.Sprintf("buildFieldIdentifierPath: %s for node %s with parent %s", path, node, node.Parent()))

return path
}

Expand All @@ -64,8 +66,8 @@ func buildFieldIdentifierPath(node *sitter.Node, doc *Document) string {
}
currentPath = prepend.Content([]byte(doc.Content)) + "." + nodeContent
logger.Println("Adding currentpath", currentPath)
} else {
logger.Println("Prepend is nil currentpath is ", currentPath)
} else if node.Parent() != nil && node.Parent().Type() == "ERROR" {
return buildFieldIdentifierPath(node.Parent(), doc)
}

if currentPath[0:1] == "$" {
Expand Down
41 changes: 36 additions & 5 deletions internal/lsp/ast_field_identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sitter "github.com/smacker/go-tree-sitter"
"github.com/stretchr/testify/assert"
lsp "go.lsp.dev/protocol"
)

func TestGetFieldIdentifierPathSimple(t *testing.T) {
Expand Down Expand Up @@ -78,19 +79,49 @@ func TestGetFieldIdentifierPathFunction(t *testing.T) {
// field: (field_identifier [0, 29] - [0, 34])))))
//
test1_start := sitter.Point{Row: 0, Column: 16}
test2_start := sitter.Point{Row: 0, Column: 23}
test2_start := sitter.Point{Row: 0, Column: 33}
test1Node := ast.RootNode().NamedDescendantForPointRange(test1_start, test1_start)
test2Node := ast.RootNode().NamedDescendantForPointRange(test2_start, test2_start)

if test1Node.Content([]byte(template)) != "test1" || test2Node.Content([]byte(template)) != "test2" {
t.Errorf("Nodes were not correctly selected")
}
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.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

var ast = ParseAst(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))
}

0 comments on commit b6a39c2

Please sign in to comment.