From 43ad456ec5cd77be039d1894650370896e6554e1 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 21 Jul 2024 17:26:38 +0200 Subject: [PATCH] feat(document-symbols): add yamlls document-symbols --- go.mod | 2 +- .../{documentSync.go => document_sync.go} | 0 internal/adapter/yamlls/initization.go | 7 +++ internal/adapter/yamlls/symbol.go | 14 ++++++ .../adapter/yamlls/symbol_integration_test.go | 50 +++++++++++++++++++ internal/adapter/yamlls/yamlls.go | 6 ++- internal/handler/handler.go | 6 --- internal/handler/initialization.go | 12 ++--- internal/handler/symbol.go | 12 +++++ 9 files changed, 94 insertions(+), 15 deletions(-) rename internal/adapter/yamlls/{documentSync.go => document_sync.go} (100%) create mode 100644 internal/adapter/yamlls/symbol.go create mode 100644 internal/adapter/yamlls/symbol_integration_test.go create mode 100644 internal/handler/symbol.go diff --git a/go.mod b/go.mod index ae19c435..31a5a8d3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/mrjosh/helm-ls go 1.21 require ( + github.com/gobwas/glob v0.2.3 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/smacker/go-tree-sitter v0.0.0-20240214120134-1f283e24f560 @@ -55,7 +56,6 @@ require ( github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect - github.com/gobwas/glob v0.2.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/btree v1.0.1 // indirect diff --git a/internal/adapter/yamlls/documentSync.go b/internal/adapter/yamlls/document_sync.go similarity index 100% rename from internal/adapter/yamlls/documentSync.go rename to internal/adapter/yamlls/document_sync.go diff --git a/internal/adapter/yamlls/initization.go b/internal/adapter/yamlls/initization.go index 396b6ae5..ebf3ddb9 100644 --- a/internal/adapter/yamlls/initization.go +++ b/internal/adapter/yamlls/initization.go @@ -19,6 +19,13 @@ func (yamllsConnector Connector) CallInitialize(ctx context.Context, workspaceUR ClientInfo: &lsp.ClientInfo{ Name: "helm-ls", }, + Capabilities: lsp.ClientCapabilities{ + TextDocument: &lsp.TextDocumentClientCapabilities{ + DocumentSymbol: &lsp.DocumentSymbolClientCapabilities{ + HierarchicalDocumentSymbolSupport: true, + }, + }, + }, } _, err := yamllsConnector.server.Initialize(ctx, ¶ms) diff --git a/internal/adapter/yamlls/symbol.go b/internal/adapter/yamlls/symbol.go new file mode 100644 index 00000000..3a3a0df1 --- /dev/null +++ b/internal/adapter/yamlls/symbol.go @@ -0,0 +1,14 @@ +package yamlls + +import ( + "context" + + lsp "go.lsp.dev/protocol" +) + +func (yamllsConnector Connector) CallDocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) (result []interface{}, err error) { + if yamllsConnector.server == nil { + return []interface{}{}, nil + } + return yamllsConnector.server.DocumentSymbol(ctx, params) +} diff --git a/internal/adapter/yamlls/symbol_integration_test.go b/internal/adapter/yamlls/symbol_integration_test.go new file mode 100644 index 00000000..f5040dc9 --- /dev/null +++ b/internal/adapter/yamlls/symbol_integration_test.go @@ -0,0 +1,50 @@ +//go:build integration + +package yamlls + +import ( + "context" + "testing" + "time" + + "github.com/mrjosh/helm-ls/internal/util" + "github.com/stretchr/testify/assert" + lsp "go.lsp.dev/protocol" + "go.lsp.dev/uri" +) + +func TestYamllsDocumentSymoblIntegration(t *testing.T) { + config := util.DefaultConfig.YamllsConfiguration + + testCases := []struct { + file string + expectedLen int + }{ + { + file: "../../../testdata/example/templates/deployment.yaml", + expectedLen: 4, + }, + { + file: "../../../testdata/example/templates/ingress.yaml", + expectedLen: 6, + }, + } + for _, tt1 := range testCases { + tt := tt1 + t.Run(tt.file, func(t *testing.T) { + t.Parallel() + yamllsConnector, documents, _ := getYamlLsConnector(t, config) + openFile(t, documents, tt.file, yamllsConnector) + + assert.EventuallyWithT(t, func(c *assert.CollectT) { + result, err := yamllsConnector.CallDocumentSymbol(context.Background(), &lsp.DocumentSymbolParams{ + TextDocument: lsp.TextDocumentIdentifier{ + URI: uri.File(tt.file), + }, + }) + assert.NoError(c, err) + assert.Len(c, result, tt.expectedLen) + }, time.Second*10, time.Second*2) + }) + } +} diff --git a/internal/adapter/yamlls/yamlls.go b/internal/adapter/yamlls/yamlls.go index 2d07a068..e711b8c3 100644 --- a/internal/adapter/yamlls/yamlls.go +++ b/internal/adapter/yamlls/yamlls.go @@ -68,7 +68,11 @@ func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfigurat io.Copy(os.Stderr, strderr) }() - yamllsConnector := Connector{documents: documents, config: yamllsConfiguration, client: client} + yamllsConnector := Connector{ + config: yamllsConfiguration, + documents: documents, + client: client, + } zapLogger, _ := zap.NewProduction() _, _, server := protocol.NewClient(ctx, yamllsConnector, jsonrpc2.NewStream(readWriteCloser), zapLogger) diff --git a/internal/handler/handler.go b/internal/handler/handler.go index ac86b4f6..6ad1a426 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -130,12 +130,6 @@ func (h *langHandler) DocumentLinkResolve(ctx context.Context, params *lsp.Docum return nil, nil } -// DocumentSymbol implements protocol.Server. -func (h *langHandler) DocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) (result []interface{}, err error) { - logger.Error("Document symbol unimplemented") - return nil, nil -} - // ExecuteCommand implements protocol.Server. func (h *langHandler) ExecuteCommand(ctx context.Context, params *lsp.ExecuteCommandParams) (result interface{}, err error) { logger.Error("Execute command unimplemented") diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index 1828e676..64e8e84d 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -36,23 +36,21 @@ func (h *langHandler) Initialize(ctx context.Context, params *lsp.InitializePara TextDocumentSync: lsp.TextDocumentSyncOptions{ Change: lsp.TextDocumentSyncKindIncremental, OpenClose: true, - Save: &lsp.SaveOptions{ - IncludeText: true, - }, }, CompletionProvider: &lsp.CompletionOptions{ TriggerCharacters: []string{".", "$."}, ResolveProvider: false, }, - HoverProvider: true, - DefinitionProvider: true, - ReferencesProvider: true, + HoverProvider: true, + DefinitionProvider: true, + ReferencesProvider: true, + DocumentSymbolProvider: true, }, }, nil } func (h *langHandler) Initialized(ctx context.Context, _ *lsp.InitializedParams) (err error) { - go h.retrieveWorkspaceConfiguration(ctx) + h.retrieveWorkspaceConfiguration(ctx) return nil } diff --git a/internal/handler/symbol.go b/internal/handler/symbol.go new file mode 100644 index 00000000..29886bdc --- /dev/null +++ b/internal/handler/symbol.go @@ -0,0 +1,12 @@ +package handler + +import ( + "context" + + lsp "go.lsp.dev/protocol" +) + +// DocumentSymbol implements protocol.Server. +func (h *langHandler) DocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) (result []interface{}, err error) { + return h.yamllsConnector.CallDocumentSymbol(ctx, params) +}