diff --git a/internal/adapter/yamlls/document_sync.go b/internal/adapter/yamlls/document_sync.go index 1db8194..2c47dc9 100644 --- a/internal/adapter/yamlls/document_sync.go +++ b/internal/adapter/yamlls/document_sync.go @@ -19,7 +19,8 @@ func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Tem continue } - doc.IsYaml = lsplocal.IsYamllsEnabled(doc.URI, yamllsConnector.config) + doc.IsYaml = yamllsConnector.IsYamllsEnabled(doc.URI) + if !yamllsConnector.isRelevantFile(doc.URI) { continue } @@ -115,3 +116,7 @@ func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Templat logger.Println("Error calling yamlls for didChange", err) } } + +func (yamllsConnector Connector) IsYamllsEnabled(uri lsp.URI) bool { + return yamllsConnector.EnabledForFilesGlobObject.Match(uri.Filename()) +} diff --git a/internal/adapter/yamlls/yamlls.go b/internal/adapter/yamlls/yamlls.go index 2d1d215..759d440 100644 --- a/internal/adapter/yamlls/yamlls.go +++ b/internal/adapter/yamlls/yamlls.go @@ -6,6 +6,7 @@ import ( "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/util" @@ -18,10 +19,11 @@ import ( 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 *lsplocal.DocumentStore + client protocol.Client + EnabledForFilesGlobObject glob.Glob } func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector { diff --git a/internal/handler/handler.go b/internal/handler/handler.go index eec024c..b4d7070 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -40,23 +40,21 @@ func StartHandler(stream io.ReadWriteCloser) { logger, ) server.connPool = conn - server.client = client + server.setClient(client) <-conn.Done() } func newHandler(connPool jsonrpc2.Conn, client protocol.Client) *ServerHandler { documents := lsplocal.NewDocumentStore() - initalYamllsConnector := &yamlls.Connector{} handler := &ServerHandler{ - client: client, - linterName: "helm-lint", - connPool: connPool, - documents: documents, - helmlsConfig: util.DefaultConfig, - yamllsConnector: initalYamllsConnector, + client: client, + linterName: "helm-lint", + connPool: connPool, + documents: documents, + helmlsConfig: util.DefaultConfig, langHandlers: map[lsplocal.DocumentType]LangHandler{ - lsplocal.TemplateDocumentType: templatehandler.NewTemplateHandler(documents, nil, initalYamllsConnector), + lsplocal.TemplateDocumentType: templatehandler.NewTemplateHandler(client, documents, nil), }, } logger.Printf("helm-lint-langserver: connections opened") @@ -72,11 +70,11 @@ func (h *ServerHandler) setChartStrore(chartStore *charts.ChartStore) { } } -func (h *ServerHandler) setYamllsConnector(yamllsConnector *yamlls.Connector) { - h.yamllsConnector = yamllsConnector +func (h *ServerHandler) setClient(client protocol.Client) { + h.client = client for _, handler := range h.langHandlers { - handler.SetYamllsConnector(yamllsConnector) + handler.SetClient(client) } } diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index d4f6fb4..9a494f4 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -4,8 +4,6 @@ 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" "github.com/sirupsen/logrus" @@ -59,29 +57,9 @@ func (h *ServerHandler) Initialized(ctx context.Context, _ *lsp.InitializedParam func (h *ServerHandler) initializationWithConfig(ctx context.Context) { configureLogLevel(h.helmlsConfig) h.chartStore.SetValuesFilesConfig(h.helmlsConfig.ValuesFilesConfig) - h.configureYamlls(ctx) -} - -func (h *ServerHandler) configureYamlsEnabledGlob() { - 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 -} - -func (h *ServerHandler) configureYamlls(ctx context.Context) { - config := h.helmlsConfig - if config.YamllsConfiguration.Enabled { - h.configureYamlsEnabledGlob() - h.setYamllsConnector(yamlls.NewConnector(ctx, config.YamllsConfiguration, h.client, h.documents)) - err := h.yamllsConnector.CallInitialize(ctx, h.chartStore.RootURI) - if err != nil { - logger.Error("Error initializing yamlls", err) - } - h.yamllsConnector.InitiallySyncOpenDocuments(h.documents.GetAllTemplateDocs()) + for _, handler := range h.langHandlers { + handler.Configure(ctx, h.helmlsConfig) } } diff --git a/internal/handler/lang_handler.go b/internal/handler/lang_handler.go index 0d6389f..a2ea60a 100644 --- a/internal/handler/lang_handler.go +++ b/internal/handler/lang_handler.go @@ -4,9 +4,9 @@ import ( "context" "fmt" - "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" "github.com/mrjosh/helm-ls/internal/util" + "go.lsp.dev/protocol" lsp "go.lsp.dev/protocol" "go.lsp.dev/uri" ) @@ -25,13 +25,16 @@ type LangHandler interface { // DidChange is called when a document is changed, it must not update the document store DidChange(ctx context.Context, params *lsp.DidChangeTextDocumentParams) (err error) + Configure(ctx context.Context, helmlsConfig util.HelmlsConfiguration) GetDiagnostics(uri lsp.DocumentURI) []lsp.PublishDiagnosticsParams + // SetChartStore is called once the chart store has been initialized SetChartStore(chartStore *charts.ChartStore) - SetYamllsConnector(yamllsConnector *yamlls.Connector) + // SetChartStore is called once the client has been initialized + SetClient(client protocol.Client) } -func (h *ServerHandler) selectLangHandler(ctx context.Context, uri uri.URI) (LangHandler, error) { +func (h *ServerHandler) selectLangHandler(_ context.Context, uri uri.URI) (LangHandler, error) { documentType, ok := h.documents.GetDocumentType(uri) if !ok { diff --git a/internal/handler/template_handler/configure.go b/internal/handler/template_handler/configure.go new file mode 100644 index 0000000..31e951e --- /dev/null +++ b/internal/handler/template_handler/configure.go @@ -0,0 +1,36 @@ +package templatehandler + +import ( + "context" + + "github.com/gobwas/glob" + "github.com/mrjosh/helm-ls/internal/adapter/yamlls" + "github.com/mrjosh/helm-ls/internal/util" +) + +func (h *TemplateHandler) Configure(ctx context.Context, helmlsConfig util.HelmlsConfiguration) { + h.configureYamlls(ctx, helmlsConfig) +} + +func (h *TemplateHandler) configureYamlsEnabledGlob(helmlsConfig util.HelmlsConfiguration) { + globObject, err := glob.Compile(helmlsConfig.YamllsConfiguration.EnabledForFilesGlob) + if err != nil { + logger.Error("Error compiling glob for yamlls EnabledForFilesGlob", err) + globObject = util.DefaultConfig.YamllsConfiguration.EnabledForFilesGlobObject + } + h.yamllsConnector.EnabledForFilesGlobObject = globObject +} + +func (h *TemplateHandler) configureYamlls(ctx context.Context, helmlsConfig util.HelmlsConfiguration) { + config := helmlsConfig + if config.YamllsConfiguration.Enabled { + h.configureYamlsEnabledGlob(helmlsConfig) + h.setYamllsConnector(yamlls.NewConnector(ctx, config.YamllsConfiguration, h.client, h.documents)) + err := h.yamllsConnector.CallInitialize(ctx, h.chartStore.RootURI) + if err != nil { + logger.Error("Error initializing yamlls", err) + } + + h.yamllsConnector.InitiallySyncOpenDocuments(h.documents.GetAllTemplateDocs()) + } +} diff --git a/internal/handler/template_handler/template_handler.go b/internal/handler/template_handler/template_handler.go index b8ac5ba..377ad5d 100644 --- a/internal/handler/template_handler/template_handler.go +++ b/internal/handler/template_handler/template_handler.go @@ -5,21 +5,24 @@ import ( "github.com/mrjosh/helm-ls/internal/charts" "github.com/mrjosh/helm-ls/internal/log" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "go.lsp.dev/protocol" ) var logger = log.GetLogger() type TemplateHandler struct { + client protocol.Client documents *lsplocal.DocumentStore chartStore *charts.ChartStore yamllsConnector *yamlls.Connector } -func NewTemplateHandler(documents *lsplocal.DocumentStore, chartStore *charts.ChartStore, yamllsConnector *yamlls.Connector) *TemplateHandler { +func NewTemplateHandler(client protocol.Client, documents *lsplocal.DocumentStore, chartStore *charts.ChartStore) *TemplateHandler { return &TemplateHandler{ + client: client, documents: documents, chartStore: chartStore, - yamllsConnector: yamllsConnector, + yamllsConnector: &yamlls.Connector{}, } } @@ -27,6 +30,10 @@ func (h *TemplateHandler) SetChartStore(chartStore *charts.ChartStore) { h.chartStore = chartStore } -func (h *TemplateHandler) SetYamllsConnector(yamllsConnector *yamlls.Connector) { +func (h *TemplateHandler) SetClient(client protocol.Client) { + h.client = client +} + +func (h *TemplateHandler) setYamllsConnector(yamllsConnector *yamlls.Connector) { h.yamllsConnector = yamllsConnector }