Skip to content

Commit

Permalink
fix completion node selection
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 28, 2024
1 parent e2449d4 commit 4fd11e5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 29 deletions.
36 changes: 18 additions & 18 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestCompletionMain(t *testing.T) {
expectedError error
}{
{
desc: "Test completion on {{ . }}",
desc: "Test completion on .Chart.N",
position: lsp.Position{
Line: 6,
Character: 4,
Line: 5,
Character: 11,
},
expectedInsertText: "Chart",
expectedInsertText: "Name",
notExpectedInsertTexts: []string{
helmdocs.HelmFuncs[0].Name,
"replicaCount",
Expand All @@ -49,6 +49,20 @@ func TestCompletionMain(t *testing.T) {
},
expectedError: nil,
},
{
desc: "Test completion on {{ . }}",
position: lsp.Position{
Line: 6,
Character: 4,
},
expectedInsertText: "Chart",
notExpectedInsertTexts: []string{
helmdocs.HelmFuncs[0].Name,
"replicaCount",
"toYaml",
},
expectedError: nil,
},
{
desc: "Test completion on .Values.re",
position: lsp.Position{
Expand Down Expand Up @@ -86,20 +100,6 @@ func TestCompletionMain(t *testing.T) {
},
expectedError: nil,
},
{
desc: "Test completion on .Chart.N",
position: lsp.Position{
Line: 5,
Character: 11,
},
expectedInsertText: "Name",
notExpectedInsertTexts: []string{
helmdocs.HelmFuncs[0].Name,
"replicaCount",
"toYaml",
},
expectedError: nil,
},
// {
// desc: "Test completion on {{ }}",
// position: lsp.Position{
Expand Down
33 changes: 23 additions & 10 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,32 @@ func (f *TemplateContextFeature) Completion() (result *lsp.CompletionList, err e
return protocol.NewCompletionResults(result).ToLSP(), nil
}

m := make(map[string]lsp.CompletionItem)
for _, queriedValuesFiles := range f.Chart.ResolveValueFiles(templateContext.Tail(), f.ChartStore) {
for _, valuesFile := range queriedValuesFiles.ValuesFiles.AllValuesFiles() {
for _, item := range util.GetValueCompletion(valuesFile.Values, queriedValuesFiles.Selector) {
m[item.InsertText] = item
switch templateContext[0] {
case "Values":
m := make(map[string]lsp.CompletionItem)
for _, queriedValuesFiles := range f.Chart.ResolveValueFiles(templateContext.Tail(), f.ChartStore) {
for _, valuesFile := range queriedValuesFiles.ValuesFiles.AllValuesFiles() {
for _, item := range util.GetValueCompletion(valuesFile.Values, queriedValuesFiles.Selector) {
m[item.InsertText] = item
}
}
}
}
completions := []lsp.CompletionItem{}
for _, item := range m {
completions = append(completions, item)
}

return &lsp.CompletionList{Items: completions, IsIncomplete: false}, nil
case "Chart", "Release", "Files", "Capabilities", "Template":
result, ok := helmdocs.BuiltInOjectVals[templateContext[0]]
if !ok {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}
return protocol.NewCompletionResults(result).ToLSP(), nil

completions := []lsp.CompletionItem{}
for _, item := range m {
completions = append(completions, item)
}

return &lsp.CompletionList{Items: completions, IsIncomplete: false}, nil
return nil, nil

}
6 changes: 5 additions & 1 deletion internal/lsp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ 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++ {
childCount := int(currentNode.ChildCount())
for i := childCount - 1; i >= 0; i-- {
child := currentNode.Child(i)
if child == nil {
continue
}
if isPointLargerOrEq(pointToLookUp, child.StartPoint()) && isPointLargerOrEq(child.EndPoint(), pointToLookUp) {
return FindRelevantChildNode(child, pointToLookUp)
}
Expand Down
37 changes: 37 additions & 0 deletions internal/lsp/ast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lsp

import (
"testing"

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

func TestFindRelevantChildNode(t *testing.T) {

template := `{{ .Values. }}
{{ .Values.re }}
{{ toY }}
{{ .Chart.N }}
{{ . }}
`
ast := ParseAst(nil, template)

logger.Println("RootNode:", ast.RootNode().String())

node := FindRelevantChildNode(ast.RootNode(), sitter.Point{
Row: 0,
Column: 11,
})

assert.Equal(t, node.StartPoint(), sitter.Point{
Row: 0,
Column: 11,
})
assert.Equal(t, node.EndPoint(), sitter.Point{
Row: 0,
Column: 11,
})
}

0 comments on commit 4fd11e5

Please sign in to comment.