Skip to content

Commit

Permalink
add config for yamlls enable per filetyps
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 4, 2024
1 parent 6cab70b commit 0870d62
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
Expand All @@ -181,6 +182,7 @@ settings = {
},
yamlls = {
enabled = true,
enabledForFileExtensions: {".yaml", ".yml"},
diagnosticsLimit = 50,
showDiagnosticsDirectly = false,
path = "yaml-language-server",
Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions internal/adapter/yamlls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions internal/adapter/yamlls/documentSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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(), &params)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
16 changes: 9 additions & 7 deletions internal/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
},
}

Expand Down

0 comments on commit 0870d62

Please sign in to comment.