diff --git a/internal/handler/completion_main_test.go b/internal/handler/completion_main_test.go index 210fe2a0..bd7d1544 100644 --- a/internal/handler/completion_main_test.go +++ b/internal/handler/completion_main_test.go @@ -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", @@ -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{ @@ -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{ diff --git a/internal/language_features/template_context.go b/internal/language_features/template_context.go index 13236378..9e7677c7 100644 --- a/internal/language_features/template_context.go +++ b/internal/language_features/template_context.go @@ -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 + } diff --git a/internal/lsp/ast.go b/internal/lsp/ast.go index 44330c38..9e85aff1 100644 --- a/internal/lsp/ast.go +++ b/internal/lsp/ast.go @@ -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) } diff --git a/internal/lsp/ast_test.go b/internal/lsp/ast_test.go new file mode 100644 index 00000000..3caa1a63 --- /dev/null +++ b/internal/lsp/ast_test.go @@ -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, + }) +}