Skip to content

Commit

Permalink
refactor(yamlls): use protocol client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 11, 2024
1 parent 2f1443a commit 25b3336
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.lsp.dev/jsonrpc2 v0.10.0
go.lsp.dev/protocol v0.12.0
go.lsp.dev/uri v0.3.0
go.uber.org/zap v1.24.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apiextensions-apiserver v0.29.2
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
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.Conn == nil {
if yamllsConnector.server == nil {
return &lsp.CompletionList{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/diagnostics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestYamllsDiagnosticsIntegration(t *testing.T) {
config.YamllsSettings = yamllsSettings
yamllsConnector := NewConnector(config, client, documents)

if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
t.Fatal("Could not connect to yaml-language-server")
}

Expand Down
18 changes: 9 additions & 9 deletions internal/adapter/yamlls/documentSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.Document) {
if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
return
}
for _, doc := range docs {
Expand All @@ -25,24 +25,24 @@ 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.Conn == nil {
if yamllsConnector.server == nil {
return
}
params.TextDocument.Text = lsplocal.TrimTemplate(ast, params.TextDocument.Text)

err := (*yamllsConnector.Conn).Notify(context.Background(), lsp.MethodTextDocumentDidOpen, params)
err := yamllsConnector.server.DidOpen(context.Background(), &params)
if err != nil {
logger.Error("Error calling yamlls for didOpen", err)
}
}

func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params lsp.DidSaveTextDocumentParams) {
if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
return
}
params.Text = lsplocal.TrimTemplate(doc.Ast, doc.Content)

err := (*yamllsConnector.Conn).Notify(context.Background(), lsp.MethodTextDocumentDidSave, params)
err := yamllsConnector.server.DidSave(context.Background(), &params)
if err != nil {
logger.Error("Error calling yamlls for didSave", err)
}
Expand All @@ -55,7 +55,7 @@ func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.Document, params
}

func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) {
if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
return
}
trimmedText := lsplocal.TrimTemplate(doc.Ast, doc.Content)
Expand All @@ -77,14 +77,14 @@ func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.Document, param
}

logger.Debug("Sending DocumentDidChange", params)
err := (*yamllsConnector.Conn).Notify(context.Background(), lsp.MethodTextDocumentDidChange, params)
err := yamllsConnector.server.DidChange(context.Background(), &params)
if err != nil {
logger.Println("Error calling yamlls for didChange", err)
}
}

func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Document, params lsp.DidChangeTextDocumentParams) {
if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
return
}

Expand All @@ -98,7 +98,7 @@ func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.Documen
}

logger.Println("Sending DocumentDidChange with full sync", params)
err := (*yamllsConnector.Conn).Notify(context.Background(), lsp.MethodTextDocumentDidChange, params)
err := yamllsConnector.server.DidChange(context.Background(), &params)
if err != nil {
logger.Println("Error calling yamlls for didChange", err)
}
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 @@ -10,7 +10,7 @@ import (
// Calls the Hover method of yamlls to get a fitting hover response
// If hover returns nothing appropriate, calls yamlls for completions
func (yamllsConnector Connector) CallHover(ctx context.Context, params lsp.HoverParams, word string) (*lsp.Hover, error) {
if yamllsConnector.Conn == nil {
if yamllsConnector.server == nil {
return &lsp.Hover{}, nil
}

Expand Down
16 changes: 12 additions & 4 deletions internal/adapter/yamlls/initization.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"go.lsp.dev/uri"
)

func (yamllsConnector Connector) CallInitialize(workspaceURI uri.URI) (result *lsp.InitializeResult, err error) {
if yamllsConnector.Conn == nil {
return
func (yamllsConnector Connector) CallInitialize(workspaceURI uri.URI) error {
if yamllsConnector.server == nil {
return nil
}

params := lsp.InitializeParams{
Expand All @@ -21,5 +21,13 @@ func (yamllsConnector Connector) CallInitialize(workspaceURI uri.URI) (result *l
},
}

return yamllsConnector.server.Initialize(context.Background(), &params)
_, err := yamllsConnector.server.Initialize(context.Background(), &params)
if err != nil {
return err
}
err = yamllsConnector.server.DidChangeConfiguration(context.Background(), &lsp.DidChangeConfigurationParams{})
if err != nil {
return err
}
return yamllsConnector.server.Initialized(context.Background(), &lsp.InitializedParams{})
}
7 changes: 2 additions & 5 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
var logger = log.GetLogger()

type Connector struct {
Conn *jsonrpc2.Conn
config util.YamllsConfiguration
server protocol.Server
documents *lsplocal.DocumentStore
client protocol.Client
}

func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector {
yamllsCmd := exec.Command("yamlls-debug.sh", "--stdio")
yamllsCmd := exec.Command(yamllsConfiguration.Path, "--stdio")

stdin, err := yamllsCmd.StdinPipe()
if err != nil {
Expand Down Expand Up @@ -59,10 +58,8 @@ func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol.

ctx := context.Background()
zapLogger, _ := zap.NewProduction()
ctx, conn, server := protocol.NewClient(ctx, yamllsConnector, jsonrpc2.NewStream(readWriteCloser), zapLogger)
ctx, _, server := protocol.NewClient(ctx, yamllsConnector, jsonrpc2.NewStream(readWriteCloser), zapLogger)

Check failure on line 61 in internal/adapter/yamlls/yamlls.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

ineffectual assignment to ctx (ineffassign)

// conn.Go(context.Background(), yamllsConnector.yamllsHandler(clientConn, documents))
yamllsConnector.Conn = &conn
yamllsConnector.server = server
return &yamllsConnector
}
12 changes: 5 additions & 7 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func (h *langHandler) Initialize(ctx context.Context, params *lsp.InitializePara
workspaceURI = uri.File(".")
}

logger.Debug("Initializing yamllsConnector")
h.yamllsConnector.CallInitialize(workspaceURI)

logger.Debug("Initializing chartStore")
h.chartStore = charts.NewChartStore(workspaceURI, h.NewChartWithWatchedFiles)

Expand All @@ -52,13 +49,12 @@ func (h *langHandler) Initialize(ctx context.Context, params *lsp.InitializePara
}, nil
}

func (h *langHandler) Initialized(ctx context.Context, params *lsp.InitializedParams) (err error) {
func (h *langHandler) Initialized(ctx context.Context, _ *lsp.InitializedParams) (err error) {
go h.RetrieveWorkspaceConfiguration(ctx)
return nil
}

func (h *langHandler) initializationWithConfig() {
logger.Println("initializationWithConfig")
configureLogLevel(h.helmlsConfig)
h.chartStore.SetValuesFilesConfig(h.helmlsConfig.ValuesFilesConfig)
configureYamlls(h)
Expand All @@ -68,8 +64,10 @@ func configureYamlls(h *langHandler) {
config := h.helmlsConfig
if config.YamllsConfiguration.Enabled {
h.yamllsConnector = yamlls.NewConnector(config.YamllsConfiguration, h.client, h.documents)
h.yamllsConnector.CallInitialize(h.chartStore.RootURI)
h.yamllsConnector.DidChangeConfiguration()
err := h.yamllsConnector.CallInitialize(h.chartStore.RootURI)
if err != nil {
logger.Error("Error initializing yamlls", err)
}
h.yamllsConnector.InitiallySyncOpenDocuments(h.documents.GetAllDocs())
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var DefaultConfig = HelmlsConfiguration{

type YamllsSchemaStoreSettings struct {
Enable bool `json:"enable"`
Url string `json:"url"`
URL string `json:"url"`
}

type YamllsSettings struct {
Expand All @@ -58,6 +58,6 @@ var DefaultYamllsSettings = YamllsSettings{
Hover: true,
YamllsSchemaStoreSettings: YamllsSchemaStoreSettings{
Enable: true,
Url: "https://www.schemastore.org/api/json/catalog.json",
URL: "https://www.schemastore.org/api/json/catalog.json",
},
}

0 comments on commit 25b3336

Please sign in to comment.