Skip to content

Commit

Permalink
feat(yamlls): use glob pattern to enable yamlls for files
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 9, 2024
1 parent 08b2b69 commit 9510047
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ You can configure helm-ls with lsp workspace configurations.
### yaml-language-server config
- **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.
- **EnabledForFilesGlob**: For wich files matchig the glob pattern yaml-language-server should be enabled.
- **Path to yaml-language-server**: Specify the executable location.
- **Diagnostics Settings**:
Expand All @@ -182,7 +182,7 @@ settings = {
},
yamlls = {
enabled = true,
enabledForFileExtensions = {".yaml", ".yml"},
enabledForFilesGlob = "*.{yaml,yml}",
diagnosticsLimit = 50,
showDiagnosticsDirectly = false,
path = "yaml-language-server",
Expand Down
9 changes: 8 additions & 1 deletion internal/adapter/yamlls/documentSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Doc
if yamllsConnector.server == nil {
return
}

for _, doc := range docs {
if !doc.IsOpen || !yamllsConnector.isRelevantFile(doc.URI) {
if !doc.IsOpen {
continue
}

doc.IsYaml = lsplocal.IsYamlDocument(doc.URI, yamllsConnector.config)
if !yamllsConnector.isRelevantFile(doc.URI) {
continue
}

yamllsConnector.DocumentDidOpen(doc.Ast, lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: doc.URI,
Expand Down
11 changes: 5 additions & 6 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"
"os/exec"
"strings"

"github.com/mrjosh/helm-ls/internal/log"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
Expand Down Expand Up @@ -79,12 +78,12 @@ func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfigurat
}

func (yamllsConnector *Connector) isRelevantFile(uri lsp.URI) bool {
for _, fileExtension := range yamllsConnector.config.EnabledForFileExtensions {
if strings.HasSuffix(uri.Filename(), fileExtension) {
return true
}
doc, ok := yamllsConnector.documents.Get(uri)
if !ok {
logger.Error("Could not find document", uri)
return true
}
return false
return doc.IsYaml
}

func (yamllsConnector *Connector) shouldRun(uri lsp.DocumentURI) bool {
Expand Down
18 changes: 12 additions & 6 deletions internal/adapter/yamlls/yamlls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yamlls
import (
"testing"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/uri"
Expand All @@ -11,19 +12,24 @@ import (
func TestIsRelevantFile(t *testing.T) {
connector := Connector{
config: util.YamllsConfiguration{
Enabled: true,
EnabledForFileExtensions: []string{".yaml", ".yml"},
Enabled: true,
},
}
assert.True(t, connector.isRelevantFile(uri.File("test.yaml")))
assert.False(t, connector.isRelevantFile(uri.File("_helpers.tpl")))

connector.documents = &lsplocal.DocumentStore{}
yamlFile := uri.File("../../../testdata/example/templates/deployment.yaml")
nonYamlFile := uri.File("../../../testdata/example/templates/_helpers.tpl")
connector.documents.Store(yamlFile, util.DefaultConfig)
connector.documents.Store(nonYamlFile, util.DefaultConfig)

assert.True(t, connector.isRelevantFile(yamlFile))
assert.False(t, connector.isRelevantFile(nonYamlFile))
}

func TestShouldRun(t *testing.T) {
connector := Connector{
config: util.YamllsConfiguration{
Enabled: true,
EnabledForFileExtensions: []string{".yaml", ".yml"},
Enabled: true,
},
}
assert.False(t, connector.shouldRun(uri.File("test.yaml")))
Expand Down
7 changes: 7 additions & 0 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"

"github.com/gobwas/glob"
"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
"github.com/mrjosh/helm-ls/internal/charts"
"github.com/mrjosh/helm-ls/internal/util"
Expand Down Expand Up @@ -56,6 +57,12 @@ func (h *langHandler) Initialized(ctx context.Context, _ *lsp.InitializedParams)
}

func (h *langHandler) initializationWithConfig(ctx context.Context) {
globObject, err := glob.Compile(h.helmlsConfig.YamllsConfiguration.EnabledForFilesGlob)
if err != nil {
logger.Error("Error compiling glob for yamlls EnabledForFilesGlob", err)
globObject = util.DefaultConfig.YamllsConfiguration.EnabledForFilesGlobObject
}
h.helmlsConfig.YamllsConfiguration.EnabledForFilesGlobObject = globObject
configureLogLevel(h.helmlsConfig)
h.chartStore.SetValuesFilesConfig(h.helmlsConfig.ValuesFilesConfig)
configureYamlls(ctx, h)
Expand Down
1 change: 1 addition & 0 deletions internal/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Document struct {
DiagnosticsCache DiagnosticsCache
IsOpen bool
SymbolTable *SymbolTable
IsYaml bool
}

// ApplyChanges updates the content of the document from LSP textDocument/didChange events.
Expand Down
12 changes: 12 additions & 0 deletions internal/lsp/document_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ func (s *DocumentStore) DidOpen(params *lsp.DidOpenTextDocumentParams, helmlsCon
DiagnosticsCache: NewDiagnosticsCache(helmlsConfig),
IsOpen: true,
SymbolTable: NewSymbolTable(ast, []byte(params.TextDocument.Text)),
IsYaml: IsYamlDocument(uri, helmlsConfig.YamllsConfiguration),
}
logger.Debug("Storing doc ", path)
s.documents.Store(path, doc)
return doc, nil
}

func (s *DocumentStore) Store(uri uri.URI, helmlsConfig util.HelmlsConfiguration) error {
_, ok := s.documents.Load(uri.Filename())
if ok {
return nil
}

content, err := os.ReadFile(uri.Filename())
if err != nil {
logger.Error("Could not open file ", uri.Filename(), " ", err)
Expand All @@ -67,6 +73,7 @@ func (s *DocumentStore) Store(uri uri.URI, helmlsConfig util.HelmlsConfiguration
DiagnosticsCache: NewDiagnosticsCache(helmlsConfig),
IsOpen: false,
SymbolTable: NewSymbolTable(ast, content),
IsYaml: IsYamlDocument(uri, helmlsConfig.YamllsConfiguration),
},
)
return nil
Expand All @@ -75,8 +82,13 @@ func (s *DocumentStore) Store(uri uri.URI, helmlsConfig util.HelmlsConfiguration
func (s *DocumentStore) Get(docuri uri.URI) (*Document, bool) {
path := docuri.Filename()
d, ok := s.documents.Load(path)

if !ok {
return nil, false
}
return d.(*Document), ok
}

func IsYamlDocument(uri lsp.URI, yamllsConfiguration util.YamllsConfiguration) bool {
return yamllsConfiguration.EnabledForFilesGlobObject.Match(uri.Filename())
}
17 changes: 17 additions & 0 deletions internal/lsp/document_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lsp

import (
"testing"

"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/uri"
)

func TestIsYamlDocument(t *testing.T) {
assert := assert.New(t)
assert.True(IsYamlDocument(uri.File("test.yaml"), util.DefaultConfig.YamllsConfiguration))
assert.False(IsYamlDocument(uri.File("test.tpl"), util.DefaultConfig.YamllsConfiguration))
assert.True(IsYamlDocument(uri.File("../../testdata/example/templates/hpa.yaml"), util.DefaultConfig.YamllsConfiguration))
assert.False(IsYamlDocument(uri.File("../../testdata/example/templates/_helpers.tpl"), util.DefaultConfig.YamllsConfiguration))
}
22 changes: 13 additions & 9 deletions internal/util/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package util

import "github.com/gobwas/glob"

type HelmlsConfiguration struct {
YamllsConfiguration YamllsConfiguration `json:"yamlls,omitempty"`
ValuesFilesConfig ValuesFilesConfig `json:"valuesFiles,omitempty"`
Expand All @@ -13,9 +15,10 @@ type ValuesFilesConfig struct {
}

type YamllsConfiguration struct {
Enabled bool `json:"enabled,omitempty"`
EnabledForFileExtensions []string `json:"enabledForFileExtensions,omitempty"`
Path string `json:"path,omitempty"`
Enabled bool `json:"enabled,omitempty"`
EnabledForFilesGlob string `json:"enabledForFilesGlob,omitempty"`
EnabledForFilesGlobObject glob.Glob
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 @@ -33,12 +36,13 @@ var DefaultConfig = HelmlsConfiguration{
AdditionalValuesFilesGlobPattern: "values*.yaml",
},
YamllsConfiguration: YamllsConfiguration{
Enabled: true,
EnabledForFileExtensions: []string{".yaml", ".yml"},
Path: "yaml-language-server",
DiagnosticsLimit: 50,
ShowDiagnosticsDirectly: false,
YamllsSettings: DefaultYamllsSettings,
Enabled: true,
EnabledForFilesGlob: "*.{yaml,yml}",
EnabledForFilesGlobObject: glob.MustCompile("*.{yaml,yml}"),
Path: "yaml-language-server",
DiagnosticsLimit: 50,
ShowDiagnosticsDirectly: false,
YamllsSettings: DefaultYamllsSettings,
},
}

Expand Down

0 comments on commit 9510047

Please sign in to comment.