Skip to content

Commit

Permalink
fix(lints): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 29, 2024
1 parent 3aafb82 commit 15c7b82
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
5 changes: 1 addition & 4 deletions cmds/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions examples/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<space>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', '<space>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 <c-x><c-o>
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', '<C-k>', vim.lsp.buf.signature_help, opts)
vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
vim.keymap.set('n', '<space>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, opts)
vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
vim.keymap.set('n', '<space>f', function()
vim.lsp.buf.format { async = true }
end, opts)
end,
})

2 changes: 1 addition & 1 deletion internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 7 additions & 9 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -68,11 +62,15 @@ func NewConnector(yamllsConfiguration util.YamllsConfiguration, client protocol.
return &Connector{}
}
}

go func() {
io.Copy(os.Stderr, strderr)

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

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

Error return value of `io.Copy` is not checked (errcheck)
}()

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
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions internal/handler/text_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 34 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / tests (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint

Check failure on line 34 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint

Check failure on line 34 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint

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) {
Expand All @@ -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)

Check failure on line 54 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / tests (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint

Check failure on line 54 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint) (typecheck)

Check failure on line 54 in internal/handler/text_document.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

undefined: lsplocal.NotificationFromLint (typecheck)

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) {
Expand Down
17 changes: 5 additions & 12 deletions internal/lsp/lint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lsp

import (
"context"
"fmt"
"strconv"
"strings"
Expand All @@ -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, "/")
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 15c7b82

Please sign in to comment.