From 0870d626f75c2150ba77d413a1a5b603fa3d3664 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Thu, 4 Jul 2024 18:51:24 +0200 Subject: [PATCH] add config for yamlls enable per filetyps --- README.md | 4 +++- internal/adapter/yamlls/completion.go | 2 +- internal/adapter/yamlls/diagnostics.go | 5 +++++ internal/adapter/yamlls/documentSync.go | 12 +++++++----- internal/adapter/yamlls/hover.go | 2 +- internal/adapter/yamlls/yamlls.go | 18 ++++++++++++++++++ internal/util/config.go | 16 +++++++++------- 7 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 456fc000..e4ac86f0 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,8 @@ You can configure helm-ls with lsp workspace configurations. ### yaml-language-server config -- **Enable yaml-language-server**: Toggle support of this feature. +- **Enabled yaml-language-server**: Toggle support of this feature. +- **EnabledForFileExtensions yaml-language-server**: For wich file extensions (or suffixes) yaml-language-server should be enabled. - **Path to yaml-language-server**: Specify the executable location. - **Diagnostics Settings**: @@ -181,6 +182,7 @@ settings = { }, yamlls = { enabled = true, + enabledForFileExtensions: {".yaml", ".yml"}, diagnosticsLimit = 50, showDiagnosticsDirectly = false, path = "yaml-language-server", diff --git a/internal/adapter/yamlls/completion.go b/internal/adapter/yamlls/completion.go index b3459259..a95cf497 100644 --- a/internal/adapter/yamlls/completion.go +++ b/internal/adapter/yamlls/completion.go @@ -7,7 +7,7 @@ import ( ) func (yamllsConnector Connector) CallCompletion(ctx context.Context, params *lsp.CompletionParams) (*lsp.CompletionList, error) { - if yamllsConnector.server == nil { + if !yamllsConnector.shouldRun(params.TextDocumentPositionParams.TextDocument.URI) { return &lsp.CompletionList{}, nil } diff --git a/internal/adapter/yamlls/diagnostics.go b/internal/adapter/yamlls/diagnostics.go index b140103d..f7fa48cb 100644 --- a/internal/adapter/yamlls/diagnostics.go +++ b/internal/adapter/yamlls/diagnostics.go @@ -32,23 +32,28 @@ func (c Connector) PublishDiagnostics(ctx context.Context, params *protocol.Publ func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content string) (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)) + if node.Type() == "text" && childNode.Type() == "text" { logger.Debug("Diagnostic", diagnostic) logger.Debug("Node", node.Content([]byte(content))) + if diagnisticIsRelevant(diagnostic, childNode) { diagnostic.Message = "Yamlls: " + diagnostic.Message filtered = append(filtered, diagnostic) } } } + return filtered } func diagnisticIsRelevant(diagnostic lsp.Diagnostic, node *sitter.Node) bool { logger.Debug("Checking if diagnostic is relevant", diagnostic.Message) + switch diagnostic.Message { case "Map keys must be unique": return !lsplocal.IsInElseBranch(node) diff --git a/internal/adapter/yamlls/documentSync.go b/internal/adapter/yamlls/documentSync.go index 9c10e054..31c40ec7 100644 --- a/internal/adapter/yamlls/documentSync.go +++ b/internal/adapter/yamlls/documentSync.go @@ -14,7 +14,7 @@ func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Doc return } for _, doc := range docs { - if !doc.IsOpen { + if !doc.IsOpen || !yamllsConnector.isRelevantFile(doc.URI) { continue } yamllsConnector.DocumentDidOpen(doc.Ast, lsp.DidOpenTextDocumentParams{ @@ -28,7 +28,8 @@ func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Doc func (yamllsConnector Connector) DocumentDidOpen(ast *sitter.Tree, params lsp.DidOpenTextDocumentParams) { logger.Debug("YamllsConnector DocumentDidOpen", params.TextDocument.URI) - if yamllsConnector.server == nil { + + if !yamllsConnector.shouldRun(params.TextDocument.URI) { return } params.TextDocument.Text = lsplocal.TrimTemplate(ast, params.TextDocument.Text) @@ -40,9 +41,10 @@ func (yamllsConnector Connector) DocumentDidOpen(ast *sitter.Tree, params lsp.Di } func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params lsp.DidSaveTextDocumentParams) { - if yamllsConnector.server == nil { + if !yamllsConnector.shouldRun(doc.URI) { return } + params.Text = lsplocal.TrimTemplate(doc.Ast, doc.Content) err := yamllsConnector.server.DidSave(context.Background(), ¶ms) @@ -58,7 +60,7 @@ func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params } func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) { - if yamllsConnector.server == nil { + if !yamllsConnector.shouldRun(doc.URI) { return } trimmedText := lsplocal.TrimTemplate(doc.Ast, doc.Content) @@ -87,7 +89,7 @@ func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, param } func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) { - if yamllsConnector.server == nil { + if !yamllsConnector.shouldRun(doc.URI) { return } diff --git a/internal/adapter/yamlls/hover.go b/internal/adapter/yamlls/hover.go index b6f85df2..df19e200 100644 --- a/internal/adapter/yamlls/hover.go +++ b/internal/adapter/yamlls/hover.go @@ -13,7 +13,7 @@ import ( // Yamlls can not handle hover if the schema validation returns error, // thats why we fall back to calling completion func (yamllsConnector Connector) CallHover(ctx context.Context, params lsp.HoverParams, word string) (*lsp.Hover, error) { - if yamllsConnector.server == nil { + if !yamllsConnector.shouldRun(params.TextDocumentPositionParams.TextDocument.URI) { return &lsp.Hover{}, nil } diff --git a/internal/adapter/yamlls/yamlls.go b/internal/adapter/yamlls/yamlls.go index 9349f0b2..dd9442de 100644 --- a/internal/adapter/yamlls/yamlls.go +++ b/internal/adapter/yamlls/yamlls.go @@ -5,12 +5,14 @@ import ( "io" "os" "os/exec" + "strings" "github.com/mrjosh/helm-ls/internal/log" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" "github.com/mrjosh/helm-ls/internal/util" "go.lsp.dev/jsonrpc2" "go.lsp.dev/protocol" + lsp "go.lsp.dev/protocol" "go.uber.org/zap" ) @@ -75,3 +77,19 @@ func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfigurat yamllsConnector.server = server return &yamllsConnector } + +func (yamllsConnector *Connector) isRelevantFile(uri lsp.URI) bool { + for _, fileExtension := range yamllsConnector.config.EnabledForFileExtensions { + if strings.HasSuffix(uri.Filename(), fileExtension) { + return true + } + } + return false +} + +func (yamllsConnector *Connector) shouldRun(uri lsp.DocumentURI) bool { + if yamllsConnector.server == nil { + return false + } + return yamllsConnector.isRelevantFile(uri) +} diff --git a/internal/util/config.go b/internal/util/config.go index 5c65ff98..b8a782a4 100644 --- a/internal/util/config.go +++ b/internal/util/config.go @@ -13,8 +13,9 @@ type ValuesFilesConfig struct { } type YamllsConfiguration struct { - Enabled bool `json:"enabled,omitempty"` - Path string `json:"path,omitempty"` + Enabled bool `json:"enabled,omitempty"` + EnabledForFileExtensions []string `json:"enabledForFileExtensions,omitempty"` + Path string `json:"path,omitempty"` // max diagnostics from yamlls that are shown for a single file DiagnosticsLimit int `json:"diagnosticsLimit,omitempty"` // if set to false diagnostics will only be shown after saving the file @@ -32,11 +33,12 @@ var DefaultConfig = HelmlsConfiguration{ AdditionalValuesFilesGlobPattern: "values*.yaml", }, YamllsConfiguration: YamllsConfiguration{ - Enabled: true, - Path: "yaml-language-server", - DiagnosticsLimit: 50, - ShowDiagnosticsDirectly: false, - YamllsSettings: DefaultYamllsSettings, + Enabled: true, + EnabledForFileExtensions: []string{".yaml", ".yml"}, + Path: "yaml-language-server", + DiagnosticsLimit: 50, + ShowDiagnosticsDirectly: false, + YamllsSettings: DefaultYamllsSettings, }, }