Skip to content

Commit

Permalink
Cleanups and TOOOs
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 1, 2023
1 parent 702c7fd commit ce223e2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 22 deletions.
4 changes: 4 additions & 0 deletions cmds/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package cmds
import (
"fmt"

"github.com/mrjosh/helm-ls/internal/log"
"github.com/spf13/cobra"
)

var logger = log.GetLogger()

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Expand All @@ -20,6 +23,7 @@ func newVersionCmd() *cobra.Command {
fmt.Sprintf("Golang: %s", versionInfo.GoVersion),
fmt.Sprintf("Compiled by: %s", versionInfo.CompiledBy),
)
logger.Debug("Additional debug info")
},
}
}
9 changes: 7 additions & 2 deletions internal/adapter/yamlls/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (
func handleConfiguration(req jsonrpc2.Request) [5]interface{} {
var params lsp.ConfigurationParams
if err := json.Unmarshal(req.Params(), &params); err != nil {
logger.Println("Error ")
logger.Error("Error parsing configuration request from yamlls", err)
}
settings := [5]interface{}{YamllsSettings{
Schemas: map[string]string{"kubernetes": "**"},
Completion: true,
Hover: true,
},
}
settings := [5]interface{}{YamllsSettings{Schemas: map[string]string{"kubernetes": "**"}, Completion: true, Hover: true}}
return settings
}
23 changes: 19 additions & 4 deletions internal/adapter/yamlls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@ func handleDiagnostics(req jsonrpc2.Request, clientConn jsonrpc2.Conn, documents
if !ok {
logger.Println("Error handling diagnostic. Could not get document: " + params.URI.Filename())
}
doc.DiagnosticsCache.Yamldiagnostics = filterDiagnostics(params.Diagnostics, doc.Ast, doc.Content)
params.Diagnostics = doc.DiagnosticsCache.GetMergedDiagnostics()
doc.DiagnosticsCache.YamlDiagnostics = filterDiagnostics(params.Diagnostics, doc.Ast, doc.Content)

clientConn.Notify(context.Background(), lsp.MethodTextDocumentPublishDiagnostics, &params)
//TODO: if set to true diagnostics from yamlls will be shown directly when they appear
// this can cause a lot of distraction, as you will get a lot of diagnostics when in the
// middle of typing template blocks
// if set to false diagnostics will only be sent to the client at the same time the
// helm lint diagnostics are sent, which only happens after saving a file
//
// potential problem: the first time we get diagnostics they should always be sent to
// the client
var showYamllsDiagnosticsAlways = true

if showYamllsDiagnosticsAlways {
params.Diagnostics = doc.DiagnosticsCache.GetMergedDiagnostics()
clientConn.Notify(context.Background(), lsp.MethodTextDocumentPublishDiagnostics, &params)

Check failure on line 37 in internal/adapter/yamlls/diagnostics.go

View workflow job for this annotation

GitHub Actions / lint (1.19.1, ubuntu-latest)

Error return value of `clientConn.Notify` is not checked (errcheck)
}
}

func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content string) (filtered []lsp.Diagnostic) {
Expand All @@ -48,7 +60,10 @@ func diagnisticIsRelevant(diagnostic lsp.Diagnostic, node *sitter.Node) bool {
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":
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

Expand Down
3 changes: 3 additions & 0 deletions internal/adapter/yamlls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
lsp "go.lsp.dev/protocol"
)

// Calls the Completion method of yamlls to get a fitting hover response
// TODO: clarify why the hover method of yamlls can't be used
func (yamllsConnector YamllsConnector) CallHover(params lsp.HoverParams, word string) lsp.Hover {
if yamllsConnector.Conn == nil {
return lsp.Hover{}
Expand All @@ -20,6 +22,7 @@ func (yamllsConnector YamllsConnector) CallHover(params lsp.HoverParams, word st
TextDocumentPositionParams: params.TextDocumentPositionParams,
}
)

_, err := (*yamllsConnector.Conn).Call(context.Background(), lsp.MethodTextDocumentCompletion, completionParams, response)
if err != nil {
return util.BuildHoverResponse(documentation, lsp.Range{})
Expand Down
1 change: 0 additions & 1 deletion internal/adapter/yamlls/trimTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

func trimTemplateForYamllsFromAst(ast *sitter.Tree, text string) string {
var result = []byte(text)
// logger.Println(ast.RootNode())
prettyPrintNode(ast.RootNode(), []byte(text), result)
return string(result)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ type YamllsConnector struct {
}

func NewYamllsConnector(workingDir string, clientConn jsonrpc2.Conn, documents *lsplocal.DocumentStore) *YamllsConnector {

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

View workflow job for this annotation

GitHub Actions / lint (1.19.1, ubuntu-latest)

unused-parameter: parameter 'workingDir' seems to be unused, consider removing or renaming it as _ (revive)
// TODO: make the path to the executable configureable

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

View workflow job for this annotation

GitHub Actions / lint (1.19.1, ubuntu-latest)

`configureable` is a misspelling of `configurable` (misspell)
yamllsCmd := exec.Command("yaml-language-server", "--stdio")

stdin, err := yamllsCmd.StdinPipe()
if err != nil {
logger.Println("Could not start yaml-language-server, some features may be missing.")
logger.Println("Could not connect to stdin of yaml-language-server, some features may be missing.")
return &YamllsConnector{}
}
stout, err := yamllsCmd.StdoutPipe()
if err != nil {
logger.Println("Could not start yaml-language-server, some features may be missing.")
logger.Println("Could not connect to stdout of yaml-language-server, some features may be missing.")
return &YamllsConnector{}
}

Expand All @@ -39,13 +40,13 @@ func NewYamllsConnector(workingDir string, clientConn jsonrpc2.Conn, documents *
if err != nil {
switch e := err.(type) {
case *exec.Error:
logger.Println("Could not start yaml-language-server, some features may be missing. Spawning subprocess failed.")
logger.Println("Could not start yaml-language-server, some features may be missing. Spawning subprocess failed.", err)
return &YamllsConnector{}
case *exec.ExitError:
logger.Println("Could not start yaml-language-server, some features may be missing. Command exit rc =", e.ExitCode())
return &YamllsConnector{}
default:
logger.Println("Could not start yaml-language-server, some features may be missing. Spawning subprocess failed.")
logger.Println("Could not start yaml-language-server, some features may be missing. Spawning subprocess failed.", err)
return &YamllsConnector{}
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string
word string
)

logger.Println("currentNode", currentNode)
logger.Println("relevantChildNode", relevantChildNode.Type())
logger.Debug("currentNode", currentNode)
logger.Debug("relevantChildNode", relevantChildNode.Type())

switch relevantChildNode.Type() {
case gotemplate.NodeTypeIdentifier:
word = relevantChildNode.Content([]byte(doc.Content))
case gotemplate.NodeTypeDot:
logger.Println("TraverseIdentifierPathUp")
logger.Debug("TraverseIdentifierPathUp")
word = lsplocal.TraverseIdentifierPathUp(relevantChildNode, doc)
case gotemplate.NodeTypeDotSymbol:
logger.Println("GetFieldIdentifierPath")
logger.Debug("GetFieldIdentifierPath")
word = lsplocal.GetFieldIdentifierPath(relevantChildNode, doc)
}
return word, nil
Expand Down
9 changes: 9 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package log

import (
"os"
"sync"

"github.com/sirupsen/logrus"
)

type logger interface {
Println(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
Printf(format string, args ...interface{})
}
Expand All @@ -26,5 +28,12 @@ func GetLogger() logger {
func createLogger() logger {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
//TODO: make this also configurable with lsp configs
// Check the value of the environment variable
if os.Getenv("LOG_LEVEL") == "debug" {
logger.SetLevel(logrus.DebugLevel)
} else {
logger.SetLevel(logrus.InfoLevel)
}
return logger
}
16 changes: 11 additions & 5 deletions internal/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package lsp
import lsp "go.lsp.dev/protocol"

type diagnosticsCache struct {
Yamldiagnostics []lsp.Diagnostic
Helmdiagnostics []lsp.Diagnostic
YamlDiagnostics []lsp.Diagnostic
HelmDiagnostics []lsp.Diagnostic
}

// TODO: this should be configurable
// max diagnostics that are shown for a single file
const yamlDiagnosticsLimit = 50

func NewDiagnosticsCache() diagnosticsCache {
return diagnosticsCache{
[]lsp.Diagnostic{},
Expand All @@ -16,11 +20,13 @@ func NewDiagnosticsCache() diagnosticsCache {

func (d diagnosticsCache) GetMergedDiagnostics() (merged []lsp.Diagnostic) {
merged = []lsp.Diagnostic{}
for _, diagnostic := range d.Yamldiagnostics {
for _, diagnostic := range d.HelmDiagnostics {
merged = append(merged, diagnostic)
}
for _, diagnostic := range d.Helmdiagnostics {
merged = append(merged, diagnostic)
for i, diagnostic := range d.YamlDiagnostics {
if i < yamlDiagnosticsLimit {
merged = append(merged, diagnostic)
}
}
return merged
}
2 changes: 1 addition & 1 deletion internal/lsp/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NotifcationFromLint(ctx context.Context, conn jsonrpc2.Conn, doc *Document)
if err != nil {
return nil, err
}
doc.DiagnosticsCache.Helmdiagnostics = diagnostics
doc.DiagnosticsCache.HelmDiagnostics = diagnostics

return nil, conn.Notify(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion internal/tree-sitter/gotemplate/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Grammar

Taken from https://github.com/ngalaiko/tree-sitter-go-template, or currently this updated branch: https://github.com/msvechla/tree-sitter-go-template/tree/fix_brackets.
Taken from https://github.com/ngalaiko/tree-sitter-go-template, or currently this updated branch: https://github.com/qvalentin/tree-sitter-go-template

## Binding

Expand Down

0 comments on commit ce223e2

Please sign in to comment.