Skip to content

Commit

Permalink
start with completion test
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 15, 2024
1 parent 5b5c02b commit cfcfa30
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
5 changes: 3 additions & 2 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string
word string
)

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

switch relevantChildNode.Type() {
case gotemplate.NodeTypeIdentifier:
Expand All @@ -144,6 +144,7 @@ func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string
case gotemplate.NodeTypeText, gotemplate.NodeTypeTemplate:
return word, true
}
logger.Println("word", word)
return word, false
}

Expand Down
76 changes: 76 additions & 0 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package handler

import (
"context"
"os"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
"github.com/mrjosh/helm-ls/internal/charts"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
)

func TestCompletionMain(t *testing.T) {
testCases := []struct {
desc string
position lsp.Position
expectedInsertText string
expectedError error
}{
{
desc: "Test completion on .Values.re",
position: lsp.Position{
Line: 0,
Character: 13,
},
expectedInsertText: "not $x\n\nnegate the boolean value of $x",
expectedError: nil,
},
}
for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
documents := lsplocal.NewDocumentStore()

path := "../../testdata/example/templates/completion-test.yaml"
fileURI := uri.File(path)

content, err := os.ReadFile(path)
if err != nil {
t.Fatal("Could not read test file", err)
}
d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
LanguageID: "",
Version: 0,
Text: string(content),
},
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
result, err := h.Completion(context.Background(), &lsp.CompletionParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: fileURI,
},
Position: tt.position,
},
})
assert.Equal(t, tt.expectedError, err)

insertTexts := []string{}
for _, item := range result.Items {
insertTexts = append(insertTexts, item.InsertText)
}
assert.Contains(t, insertTexts, tt.expectedInsertText)
})
}
}
8 changes: 4 additions & 4 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func (f *TemplateContextFeature) Hover() (string, error) {
case "Values":
return f.valuesHover(templateContext.Tail())
case "Chart":
docs, error := f.builtInOjectDocsLookup(templateContext.Tail().Format(), helmdocs.BuiltInOjectVals[templateContext[0]])
docs, err := f.builtInOjectDocsLookup(templateContext.Tail().Format(), helmdocs.BuiltInOjectVals[templateContext[0]])
value := f.getMetadataField(&f.Chart.ChartMetadata.Metadata, docs.Name)
return fmt.Sprintf("%s\n\n%s\n", docs.Doc, value), error
return fmt.Sprintf("%s\n\n%s\n", docs.Doc, value), err
case "Release", "Files", "Capabilities", "Template":
docs, error := f.builtInOjectDocsLookup(templateContext.Tail().Format(), helmdocs.BuiltInOjectVals[templateContext[0]])
return docs.Doc, error
docs, err := f.builtInOjectDocsLookup(templateContext.Tail().Format(), helmdocs.BuiltInOjectVals[templateContext[0]])
return docs.Doc, err
}

return templateContext.Format(), err
Expand Down
3 changes: 2 additions & 1 deletion internal/language_features/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func NewVariablesFeature(genericDocumentUseCase *GenericDocumentUseCase) *Variab
}

func (f *VariablesFeature) AppropriateForNode() bool {
return f.NodeType == gotemplate.NodeTypeIdentifier && f.ParentNodeType == gotemplate.NodeTypeVariable
return f.NodeType == gotemplate.NodeTypeIdentifier &&
f.ParentNodeType == gotemplate.NodeTypeVariable
}

func (f *VariablesFeature) Definition() (result []lsp.Location, err error) {
Expand Down
7 changes: 7 additions & 0 deletions testdata/example/templates/completion-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ .Values.imagePullSecrets }}
{{ .Values }}

{{ toY }}



0 comments on commit cfcfa30

Please sign in to comment.