diff --git a/cmds/lint.go b/cmds/lint.go index 6319aa0f..03ba3f05 100644 --- a/cmds/lint.go +++ b/cmds/lint.go @@ -26,10 +26,7 @@ func newLintCmd() *cobra.Command { return err } - msgs, err := locallsp.GetDiagnostics(rootPath, chart.ValuesFiles.MainValuesFile.Values) - if err != nil { - return err - } + msgs := locallsp.GetDiagnostics(rootPath, chart.ValuesFiles.MainValuesFile.Values) for _, msg := range msgs { fmt.Println(msg) diff --git a/examples/nvim/init.lua b/examples/nvim/init.lua index b7613095..73579ecd 100644 --- a/examples/nvim/init.lua +++ b/examples/nvim/init.lua @@ -39,3 +39,45 @@ lspconfig.helm_ls.setup { -- setup yamlls lspconfig.yamlls.setup {} + + +-- below are keymapping as recommended by nvim-lspconfig + +-- Global mappings. +-- See `:help vim.diagnostic.*` for documentation on any of the below functions +vim.keymap.set('n', 'e', vim.diagnostic.open_float) +vim.keymap.set('n', '[d', vim.diagnostic.goto_prev) +vim.keymap.set('n', ']d', vim.diagnostic.goto_next) +vim.keymap.set('n', 'q', vim.diagnostic.setloclist) + +-- Use LspAttach autocommand to only map the following keys +-- after the language server attaches to the current buffer +vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('UserLspConfig', {}), + callback = function(ev) + -- Enable completion triggered by + vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' + + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local opts = { buffer = ev.buf } + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) + vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) + vim.keymap.set('n', '', vim.lsp.buf.signature_help, opts) + vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) + vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) + vim.keymap.set('n', 'wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, opts) + vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, opts) + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) + vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) + vim.keymap.set('n', 'f', function() + vim.lsp.buf.format { async = true } + end, opts) + end, +}) + diff --git a/internal/adapter/yamlls/integration_test_utils.go b/internal/adapter/yamlls/integration_test_utils.go index c44892db..33aae38f 100644 --- a/internal/adapter/yamlls/integration_test_utils.go +++ b/internal/adapter/yamlls/integration_test_utils.go @@ -55,7 +55,7 @@ func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connect zapLogger, _ := zap.NewProduction() client := protocol.ClientDispatcher(con, zapLogger) - yamllsConnector := NewConnector(config, client, documents) + yamllsConnector := NewConnector(context.Background(), config, client, documents) if yamllsConnector.server == nil { t.Fatal("Could not connect to yaml-language-server") diff --git a/internal/adapter/yamlls/yamlls.go b/internal/adapter/yamlls/yamlls.go index c9e5b4f7..9349f0b2 100644 --- a/internal/adapter/yamlls/yamlls.go +++ b/internal/adapter/yamlls/yamlls.go @@ -23,7 +23,7 @@ type Connector struct { client protocol.Client } -func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector { +func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector { yamllsCmd := exec.Command(yamllsConfiguration.Path, "--stdio") stdin, err := yamllsCmd.StdinPipe() @@ -43,12 +43,6 @@ func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol. return &Connector{} } - go func() { - for { - io.Copy(os.Stderr, strderr) - } - }() - readWriteCloser := readWriteCloseSubprocess{ stout, stdin, @@ -68,11 +62,15 @@ func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol. return &Connector{} } } + + go func() { + io.Copy(os.Stderr, strderr) + }() + yamllsConnector := Connector{documents: documents, config: yamllsConfiguration, client: client} - ctx := context.Background() zapLogger, _ := zap.NewProduction() - ctx, _, server := protocol.NewClient(ctx, yamllsConnector, jsonrpc2.NewStream(readWriteCloser), zapLogger) + _, _, server := protocol.NewClient(ctx, yamllsConnector, jsonrpc2.NewStream(readWriteCloser), zapLogger) yamllsConnector.server = server return &yamllsConnector diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index b388d610..d1b7bd26 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -63,7 +63,7 @@ func (h *langHandler) initializationWithConfig(ctx context.Context) { func configureYamlls(ctx context.Context, h *langHandler) { config := h.helmlsConfig if config.YamllsConfiguration.Enabled { - h.yamllsConnector = yamlls.NewConnector(config.YamllsConfiguration, h.client, h.documents) + h.yamllsConnector = 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) diff --git a/internal/handler/text_document.go b/internal/handler/text_document.go index bc9e1283..bc965e52 100644 --- a/internal/handler/text_document.go +++ b/internal/handler/text_document.go @@ -31,11 +31,9 @@ func (h *langHandler) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocume if err != nil { logger.Error("Error getting chart info for file", doc.URI, err) } - notification, err := lsplocal.NotificationFromLint(ctx, h.connPool, chart, doc) + notification := lsplocal.NotificationFromLint(chart, doc) - h.client.PublishDiagnostics(ctx, notification) - - return err + return h.client.PublishDiagnostics(ctx, notification) } func (h *langHandler) DidClose(ctx context.Context, params *lsp.DidCloseTextDocumentParams) (err error) { @@ -53,10 +51,9 @@ func (h *langHandler) DidSave(ctx context.Context, params *lsp.DidSaveTextDocume } h.yamllsConnector.DocumentDidSave(doc, *params) - notification, err := lsplocal.NotificationFromLint(ctx, h.connPool, chart, doc) + notification := lsplocal.NotificationFromLint(chart, doc) - h.client.PublishDiagnostics(ctx, notification) - return nil + return h.client.PublishDiagnostics(ctx, notification) } func (h *langHandler) DidChange(ctx context.Context, params *lsp.DidChangeTextDocumentParams) (err error) { diff --git a/internal/lsp/lint.go b/internal/lsp/lint.go index 5950a1d2..a297e787 100644 --- a/internal/lsp/lint.go +++ b/internal/lsp/lint.go @@ -1,7 +1,6 @@ package lsp import ( - "context" "fmt" "strconv" "strings" @@ -14,36 +13,31 @@ import ( "github.com/mrjosh/helm-ls/pkg/lint/support" "github.com/pkg/errors" - // nolint "github.com/mrjosh/helm-ls/pkg/lint/rules" - "go.lsp.dev/jsonrpc2" lsp "go.lsp.dev/protocol" "go.lsp.dev/uri" ) var logger = log.GetLogger() -func NotificationFromLint(ctx context.Context, conn jsonrpc2.Conn, chart *charts.Chart, doc *Document) (*lsp.PublishDiagnosticsParams, error) { +func GetDiagnosticsNotification(chart *charts.Chart, doc *Document) *lsp.PublishDiagnosticsParams { vals := chart.ValuesFiles.MainValuesFile.Values if chart.ValuesFiles.OverlayValuesFile != nil { vals = chartutil.CoalesceTables(chart.ValuesFiles.OverlayValuesFile.Values, chart.ValuesFiles.MainValuesFile.Values) } - diagnostics, err := GetDiagnostics(doc.URI, vals) - if err != nil { - return nil, err - } + diagnostics := GetDiagnostics(doc.URI, vals) doc.DiagnosticsCache.HelmDiagnostics = diagnostics return &lsp.PublishDiagnosticsParams{ URI: doc.URI, Diagnostics: doc.DiagnosticsCache.GetMergedDiagnostics(), - }, nil + } } // GetDiagnostics will run helm linter against the currect document URI using the given values // and converts the helm.support.Message to lsp.Diagnostics -func GetDiagnostics(uri uri.URI, vals chartutil.Values) ([]lsp.Diagnostic, error) { +func GetDiagnostics(uri uri.URI, vals chartutil.Values) []lsp.Diagnostic { var ( filename = uri.Filename() paths = strings.Split(filename, "/") @@ -60,7 +54,6 @@ func GetDiagnostics(uri uri.URI, vals chartutil.Values) ([]lsp.Diagnostic, error } } - logger.Println(dir) client := action.NewLint() result := client.Run([]string{dir}, vals) @@ -77,7 +70,7 @@ func GetDiagnostics(uri uri.URI, vals chartutil.Values) ([]lsp.Diagnostic, error diagnostics = append(diagnostics, *d) } - return diagnostics, nil + return diagnostics } func GetDiagnosticFromLinterErr(supMsg support.Message) (*lsp.Diagnostic, string, error) {