Skip to content

Commit

Permalink
refactor: yamlls setup in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Oct 6, 2024
1 parent e94373e commit 228d3a5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 47 deletions.
7 changes: 6 additions & 1 deletion internal/adapter/yamlls/document_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}
10 changes: 6 additions & 4 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
}

Expand Down
26 changes: 2 additions & 24 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down
9 changes: 6 additions & 3 deletions internal/handler/lang_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions internal/handler/template_handler/configure.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
13 changes: 10 additions & 3 deletions internal/handler/template_handler/template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ 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{},
}
}

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
}

0 comments on commit 228d3a5

Please sign in to comment.