-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from qvalentin/feat-yamlls
feat(yamlls): initial support of yamlls integration
- Loading branch information
Showing
39 changed files
with
19,943 additions
and
19,015 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- a minimal example config for setting up neovim with helm-ls and yamlls | ||
|
||
-- setup lazy plugin manager | ||
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" | ||
if not vim.loop.fs_stat(lazypath) then | ||
vim.fn.system({ | ||
"git", | ||
"clone", | ||
"--filter=blob:none", | ||
"https://github.com/folke/lazy.nvim.git", | ||
"--branch=stable", -- latest stable release | ||
lazypath, | ||
}) | ||
end | ||
vim.opt.rtp:prepend(lazypath) | ||
vim.g.mapleader = " " | ||
|
||
require("lazy").setup({ | ||
-- towolf/vim-helm provides basic syntax highlighting and filetype detection | ||
-- ft = 'helm' is important to not start yamlls | ||
{ 'towolf/vim-helm', ft = 'helm' }, | ||
|
||
{ "neovim/nvim-lspconfig", event = { "BufReadPre", "BufNewFile", "BufEnter" } } | ||
}) | ||
|
||
|
||
local lspconfig = require('lspconfig') | ||
|
||
-- setup helm-ls | ||
lspconfig.helm_ls.setup { | ||
settings = { | ||
['helm-ls'] = { | ||
yamlls = { | ||
path = "yaml-language-server", | ||
} | ||
} | ||
} | ||
} | ||
|
||
-- setup yamlls | ||
lspconfig.yamlls.setup {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package yamlls | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
lsp "go.lsp.dev/protocol" | ||
) | ||
|
||
func (yamllsConnector Connector) CallCompletion(params lsp.CompletionParams) *lsp.CompletionList { | ||
if yamllsConnector.Conn == nil { | ||
return &lsp.CompletionList{} | ||
} | ||
|
||
logger.Println("Calling yamlls for completions") | ||
var response = reflect.New(reflect.TypeOf(lsp.CompletionList{})).Interface() | ||
_, err := (*yamllsConnector.Conn).Call(context.Background(), lsp.MethodTextDocumentCompletion, params, response) | ||
if err != nil { | ||
logger.Error("Error Calling yamlls for completions", err) | ||
return &lsp.CompletionList{} | ||
} | ||
|
||
logger.Debug("Got completions from yamlls", response) | ||
return response.(*lsp.CompletionList) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package yamlls | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"go.lsp.dev/jsonrpc2" | ||
lsp "go.lsp.dev/protocol" | ||
) | ||
|
||
func (yamllsConnector Connector) handleConfiguration(req jsonrpc2.Request) []interface{} { | ||
var params lsp.ConfigurationParams | ||
if err := json.Unmarshal(req.Params(), ¶ms); err != nil { | ||
logger.Error("Error parsing configuration request from yamlls", err) | ||
} | ||
logger.Debug("Yamlls ConfigurationParams", params) | ||
settings := []interface{}{yamllsConnector.config.YamllsSettings} | ||
return settings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package yamlls | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
lsplocal "github.com/mrjosh/helm-ls/internal/lsp" | ||
sitter "github.com/smacker/go-tree-sitter" | ||
"go.lsp.dev/jsonrpc2" | ||
lsp "go.lsp.dev/protocol" | ||
) | ||
|
||
func (yamllsConnector *Connector) handleDiagnostics(req jsonrpc2.Request, clientConn jsonrpc2.Conn, documents *lsplocal.DocumentStore) { | ||
var params lsp.PublishDiagnosticsParams | ||
if err := json.Unmarshal(req.Params(), ¶ms); err != nil { | ||
logger.Println("Error handling diagnostic", err) | ||
} | ||
|
||
doc, ok := documents.Get(params.URI) | ||
if !ok { | ||
logger.Println("Error handling diagnostic. Could not get document: " + params.URI.Filename()) | ||
} | ||
|
||
doc.DiagnosticsCache.SetYamlDiagnostics(filterDiagnostics(params.Diagnostics, doc.Ast, doc.Content)) | ||
if doc.DiagnosticsCache.ShouldShowDiagnosticsOnNewYamlDiagnostics() { | ||
logger.Debug("Publishing yamlls diagnostics") | ||
params.Diagnostics = doc.DiagnosticsCache.GetMergedDiagnostics() | ||
err := clientConn.Notify(context.Background(), lsp.MethodTextDocumentPublishDiagnostics, ¶ms) | ||
if err != nil { | ||
logger.Println("Error calling yamlls for diagnostics", err) | ||
} | ||
} | ||
} | ||
|
||
func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content string) (filtered []lsp.Diagnostic) { | ||
filtered = []lsp.Diagnostic{} | ||
for _, diagnostic := range diagnostics { | ||
node := lsplocal.NodeAtPosition(ast, diagnostic.Range.Start) | ||
childNode := lsplocal.FindRelevantChildNode(ast.RootNode(), lsplocal.GetSitterPointForLspPos(diagnostic.Range.Start)) | ||
if node.Type() == "text" && childNode.Type() == "text" { | ||
logger.Debug("Diagnostic", diagnostic) | ||
logger.Debug("Node", node.Content([]byte(content))) | ||
if diagnisticIsRelevant(diagnostic, childNode) { | ||
diagnostic.Message = "Yamlls: " + diagnostic.Message | ||
filtered = append(filtered, diagnostic) | ||
} | ||
} | ||
} | ||
return filtered | ||
} | ||
|
||
func diagnisticIsRelevant(diagnostic lsp.Diagnostic, node *sitter.Node) bool { | ||
logger.Debug("Diagnostic", diagnostic.Message) | ||
switch diagnostic.Message { | ||
case "Map keys must be unique": | ||
return !lsplocal.IsInElseBranch(node) | ||
case "All mapping items must start at the same column", | ||
"Implicit map keys need to be followed by map values", | ||
"Implicit keys need to be on a single line", | ||
"A block sequence may not be used as an implicit map key": | ||
// TODO: could add a check if is is caused by includes | ||
return false | ||
|
||
default: | ||
return true | ||
} | ||
|
||
} |
Oops, something went wrong.