Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: process values.yaml files #111

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
11 changes: 6 additions & 5 deletions internal/adapter/yamlls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"strings"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast"
sitter "github.com/smacker/go-tree-sitter"
"go.lsp.dev/protocol"
lsp "go.lsp.dev/protocol"
)

func (c Connector) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) (err error) {
doc, ok := c.documents.Get(params.URI)
doc, ok := c.documents.GetTemplateDoc(params.URI)
if !ok {
logger.Println("Error handling diagnostic. Could not get document: " + params.URI.Filename())
return fmt.Errorf("Could not get document: %s", params.URI.Filename())
Expand All @@ -32,16 +33,16 @@ func (c Connector) PublishDiagnostics(ctx context.Context, params *protocol.Publ
return nil
}

func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content string) (filtered []lsp.Diagnostic) {
func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content []byte) (filtered []lsp.Diagnostic) {
filtered = []lsp.Diagnostic{}

for _, diagnostic := range diagnostics {
node := lsplocal.NodeAtPosition(ast, diagnostic.Range.Start)
childNode := lsplocal.FindRelevantChildNode(ast.RootNode(), lsplocal.GetSitterPointForLspPos(diagnostic.Range.Start))
node := templateast.NodeAtPosition(ast, diagnostic.Range.Start)
childNode := templateast.FindRelevantChildNode(ast.RootNode(), templateast.GetSitterPointForLspPos(diagnostic.Range.Start))

if node.Type() == "text" && childNode.Type() == "text" {
logger.Debug("Diagnostic", diagnostic)
logger.Debug("Node", node.Content([]byte(content)))
logger.Debug("Node", node.Content(content))

if diagnisticIsRelevant(diagnostic, childNode) {
diagnostic.Message = "Yamlls: " + diagnostic.Message
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/diagnostics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
"time"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/lsp/document"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
Expand Down Expand Up @@ -44,7 +44,7 @@ func readTestFiles(dir string, channel chan<- string, doneChan chan<- int) {
doneChan <- count
}

func sendTestFilesToYamlls(documents *lsplocal.DocumentStore, yamllsConnector *Connector,
func sendTestFilesToYamlls(documents *document.DocumentStore, yamllsConnector *Connector,
doneReadingFilesChan <-chan int,
doneSendingFilesChan chan<- int,
filesChan <-chan string,
Expand Down
22 changes: 14 additions & 8 deletions internal/adapter/yamlls/document_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/lsp/document"
"github.com/mrjosh/helm-ls/internal/util"
sitter "github.com/smacker/go-tree-sitter"
lsp "go.lsp.dev/protocol"
)

func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Document) {
func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*document.TemplateDocument) {
if yamllsConnector.server == nil {
return
}
Expand All @@ -19,15 +20,16 @@ func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Doc
continue
}

doc.IsYaml = lsplocal.IsYamlDocument(doc.URI, yamllsConnector.config)
doc.IsYaml = yamllsConnector.IsYamllsEnabled(doc.URI)

if !yamllsConnector.isRelevantFile(doc.URI) {
continue
}

yamllsConnector.DocumentDidOpen(doc.Ast, lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: doc.URI,
Text: doc.Content,
Text: string(doc.Content),
},
})
}
Expand All @@ -39,15 +41,15 @@ func (yamllsConnector Connector) DocumentDidOpen(ast *sitter.Tree, params lsp.Di
if !yamllsConnector.shouldRun(params.TextDocument.URI) {
return
}
params.TextDocument.Text = lsplocal.TrimTemplate(ast, params.TextDocument.Text)
params.TextDocument.Text = lsplocal.TrimTemplate(ast, []byte(params.TextDocument.Text))

err := yamllsConnector.server.DidOpen(context.Background(), &params)
if err != nil {
logger.Error("Error calling yamlls for didOpen", err)
}
}

func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params lsp.DidSaveTextDocumentParams) {
func (yamllsConnector Connector) DocumentDidSave(doc *document.TemplateDocument, params lsp.DidSaveTextDocumentParams) {
if !yamllsConnector.shouldRun(doc.URI) {
return
}
Expand All @@ -66,7 +68,7 @@ func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params
})
}

func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) {
func (yamllsConnector Connector) DocumentDidChange(doc *document.TemplateDocument, params lsp.DidChangeTextDocumentParams) {
if !yamllsConnector.shouldRun(doc.URI) {
return
}
Expand Down Expand Up @@ -95,12 +97,12 @@ func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, param
}
}

func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) {
func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *document.TemplateDocument, params lsp.DidChangeTextDocumentParams) {
if !yamllsConnector.shouldRun(doc.URI) {
return
}

logger.Debug("Sending DocumentDidChange with full sync, current content:", doc.Content)
logger.Debug("Sending DocumentDidChange with full sync, current content:", string(doc.Content))
trimmedText := lsplocal.TrimTemplate(doc.Ast.Copy(), doc.Content)

params.ContentChanges = []lsp.TextDocumentContentChangeEvent{
Expand All @@ -115,3 +117,7 @@ func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Documen
logger.Println("Error calling yamlls for didChange", err)
}
}

func (yamllsConnector Connector) IsYamllsEnabled(uri lsp.URI) bool {
return yamllsConnector.EnabledForFilesGlobObject.Match(uri.Filename())
}
2 changes: 2 additions & 0 deletions internal/adapter/yamlls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (yamllsConnector Connector) getHoverFromCompletion(ctx context.Context, par
}
}

logger.Debug("Got completion for hover from yamlls", completionList)

return protocol.BuildHoverResponse(documentation, resultRange), nil
}

Expand Down
10 changes: 5 additions & 5 deletions internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/lsp/document"
"github.com/mrjosh/helm-ls/internal/util"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
Expand Down Expand Up @@ -47,9 +47,9 @@ func (proc readWriteCloseMock) Close() error {
return nil
}

func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *lsplocal.DocumentStore, chan lsp.PublishDiagnosticsParams) {
func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *document.DocumentStore, chan lsp.PublishDiagnosticsParams) {
dir := t.TempDir()
documents := lsplocal.NewDocumentStore()
documents := document.NewDocumentStore()
diagnosticsChan := make(chan lsp.PublishDiagnosticsParams)
con := jsonrpc2.NewConn(jsonrpc2.NewStream(readWriteCloseMock{diagnosticsChan}))
zapLogger, _ := zap.NewProduction()
Expand All @@ -66,7 +66,7 @@ func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connect
return yamllsConnector, documents, diagnosticsChan
}

func openFile(t *testing.T, documents *lsplocal.DocumentStore, path string, yamllsConnector *Connector) {
func openFile(t *testing.T, documents *document.DocumentStore, path string, yamllsConnector *Connector) {
fileURI := uri.File(path)

content, err := os.ReadFile(path)
Expand All @@ -81,6 +81,6 @@ func openFile(t *testing.T, documents *lsplocal.DocumentStore, path string, yaml
Text: string(content),
},
}
doc, err := documents.DidOpen(&d, util.DefaultConfig)
doc, err := documents.DidOpenTemplateDocument(&d, util.DefaultConfig)
yamllsConnector.DocumentDidOpen(doc.Ast, d)
}
16 changes: 9 additions & 7 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"os"
"os/exec"

"github.com/gobwas/glob"
"github.com/mrjosh/helm-ls/internal/log"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/lsp/document"
"github.com/mrjosh/helm-ls/internal/util"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
Expand All @@ -18,13 +19,14 @@
var logger = log.GetLogger()

type Connector struct {
config util.YamllsConfiguration
server protocol.Server
documents *lsplocal.DocumentStore
client protocol.Client
config util.YamllsConfiguration
server protocol.Server
documents *document.DocumentStore
client protocol.Client
EnabledForFilesGlobObject glob.Glob
}

func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector {
func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *document.DocumentStore) *Connector {
yamllsCmd := exec.Command(yamllsConfiguration.Path, "--stdio")

stdin, err := yamllsCmd.StdinPipe()
Expand Down Expand Up @@ -65,7 +67,7 @@
}

go func() {
io.Copy(os.Stderr, strderr)

Check failure on line 70 in internal/adapter/yamlls/yamlls.go

View workflow job for this annotation

GitHub Actions / lint (1.22.7, ubuntu-latest)

Error return value of `io.Copy` is not checked (errcheck)
}()

yamllsConnector := Connector{
Expand All @@ -82,7 +84,7 @@
}

func (yamllsConnector *Connector) isRelevantFile(uri lsp.URI) bool {
doc, ok := yamllsConnector.documents.Get(uri)
doc, ok := yamllsConnector.documents.GetTemplateDoc(uri)
if !ok {
logger.Error("Could not find document", uri)
return true
Expand Down
8 changes: 4 additions & 4 deletions internal/adapter/yamlls/yamlls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/lsp/document"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/uri"
Expand All @@ -17,7 +17,7 @@ func TestIsRelevantFile(t *testing.T) {
},
}

connector.documents = &lsplocal.DocumentStore{}
connector.documents = document.NewDocumentStore()
yamlFile := "../../../testdata/example/templates/deployment.yaml"
nonYamlFile := "../../../testdata/example/templates/_helpers.tpl"

Expand All @@ -27,8 +27,8 @@ func TestIsRelevantFile(t *testing.T) {
nonYamlFileContent, err := os.ReadFile(nonYamlFile)
assert.NoError(t, err)

connector.documents.Store(yamlFile, yamlFileContent, util.DefaultConfig)
connector.documents.Store(nonYamlFile, nonYamlFileContent, util.DefaultConfig)
connector.documents.StoreTemplateDocument(yamlFile, yamlFileContent, util.DefaultConfig)
connector.documents.StoreTemplateDocument(nonYamlFile, nonYamlFileContent, util.DefaultConfig)

assert.True(t, connector.isRelevantFile(uri.File(yamlFile)))
assert.False(t, connector.isRelevantFile(uri.File(nonYamlFile)))
Expand Down
35 changes: 3 additions & 32 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,15 @@ package handler
import (
"context"

languagefeatures "github.com/mrjosh/helm-ls/internal/language_features"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/protocol"
lsp "go.lsp.dev/protocol"

helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm"
)

func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionParams) (result *lsp.CompletionList, err error) {
func (h *ServerHandler) Completion(ctx context.Context, params *lsp.CompletionParams) (result *lsp.CompletionList, err error) {
logger.Debug("Running completion with params", params)

genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NestedNodeAtPositionForCompletion)
handler, err := h.selectLangHandler(ctx, params.TextDocument.URI)
if err != nil {
return nil, err
}

usecases := []languagefeatures.CompletionUseCase{
languagefeatures.NewTemplateContextFeature(genericDocumentUseCase),
languagefeatures.NewFunctionCallFeature(genericDocumentUseCase),
languagefeatures.NewTextFeature(ctx, genericDocumentUseCase, h.yamllsConnector, &params.TextDocumentPositionParams),
languagefeatures.NewIncludesCallFeature(genericDocumentUseCase),
}

for _, usecase := range usecases {
if usecase.AppropriateForNode() {
return usecase.Completion()
}
}

// If no usecase matched, we assume we are at {{ }}
// and provide the basic BuiltInObjects and functions
items := []helmdocs.HelmDocumentation{}
for _, v := range helmdocs.BuiltInObjects {
v.Name = "." + v.Name
items = append(items, v)
}

return protocol.CompletionResults{}.
WithDocs(items, lsp.CompletionItemKindConstant).
WithDocs(helmdocs.AllFuncs, lsp.CompletionItemKindFunction).ToList(), nil
return handler.Completion(ctx, params)
}
4 changes: 2 additions & 2 deletions internal/handler/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
lsp "go.lsp.dev/protocol"
)

func (h *langHandler) DidChangeConfiguration(ctx context.Context, params *lsp.DidChangeConfigurationParams) (err error) {
func (h *ServerHandler) DidChangeConfiguration(ctx context.Context, params *lsp.DidChangeConfigurationParams) (err error) {
// go h.retrieveWorkspaceConfiguration(ctx)
logger.Println("Changing workspace config is not implemented")
return nil
}

func (h *langHandler) retrieveWorkspaceConfiguration(ctx context.Context) {
func (h *ServerHandler) retrieveWorkspaceConfiguration(ctx context.Context) {
logger.Debug("Calling workspace/configuration")
configurationParams := lsp.ConfigurationParams{
Items: []lsp.ConfigurationItem{{Section: "helm-ls"}},
Expand Down
13 changes: 8 additions & 5 deletions internal/handler/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import (
"go.lsp.dev/uri"
)

var configurationParams = lsp.ConfigurationParams{Items: []lsp.ConfigurationItem{{Section: "helm-ls"}}}
var (
addChartCallback = func(chart *charts.Chart) {}
configurationParams = lsp.ConfigurationParams{Items: []lsp.ConfigurationItem{{Section: "helm-ls"}}}
)

func TestConfigurationWorks(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
handler := &ServerHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
Expand All @@ -41,7 +44,7 @@ func TestConfigurationWorks(t *testing.T) {

func TestConfigurationWorksForEmptyConfig(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
handler := &ServerHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
Expand All @@ -60,7 +63,7 @@ func TestConfigurationWorksForEmptyConfig(t *testing.T) {

func TestConfigurationWorksForError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
handler := &ServerHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
Expand All @@ -82,7 +85,7 @@ func TestConfigurationWorksForError(t *testing.T) {

func TestConfigurationWorksForJsonError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
handler := &ServerHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
Expand Down
25 changes: 5 additions & 20 deletions internal/handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,15 @@ package handler
import (
"context"

languagefeatures "github.com/mrjosh/helm-ls/internal/language_features"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
lsp "go.lsp.dev/protocol"
)

func (h *langHandler) Definition(_ context.Context, params *lsp.DefinitionParams) (result []lsp.Location, err error) {
genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NodeAtPosition)
func (h *ServerHandler) Definition(ctx context.Context, params *lsp.DefinitionParams) (result []lsp.Location, err error) {
logger.Debug("Running Definition with params", params)

handler, err := h.selectLangHandler(ctx, params.TextDocument.URI)
if err != nil {
return nil, err
}

usecases := []languagefeatures.DefinitionUseCase{
languagefeatures.NewBuiltInObjectsFeature(genericDocumentUseCase), // has to be before template context
languagefeatures.NewVariablesFeature(genericDocumentUseCase),
languagefeatures.NewTemplateContextFeature(genericDocumentUseCase),
languagefeatures.NewIncludesCallFeature(genericDocumentUseCase),
}

for _, usecase := range usecases {
if usecase.AppropriateForNode() {
result, err := usecase.Definition()
return result, err
}
}

return nil, nil
return handler.Definition(ctx, params)
}
Loading
Loading