Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"

"github.com/google/go-jsonnet/formatter"
"github.com/grafana/jsonnet-language-server/pkg/server"
"github.com/grafana/jsonnet-language-server/pkg/utils"
"github.com/jdbaldry/go-language-server-protocol/jsonrpc2"
Expand Down Expand Up @@ -69,10 +70,10 @@ Environment variables:
}

func main() {
jpaths := filepath.SplitList(os.Getenv("JSONNET_PATH"))
tankaMode := false
lint := false
evalDiags := false
config := server.Configuration{
JPaths: filepath.SplitList(os.Getenv("JSONNET_PATH")),
FormattingOptions: formatter.DefaultOptions(),
}
log.SetLevel(log.InfoLevel)

for i, arg := range os.Args {
Expand All @@ -83,19 +84,19 @@ func main() {
printVersion(os.Stdout)
os.Exit(0)
} else if arg == "-J" || arg == "--jpath" {
jpaths = append([]string{getArgValue(i)}, jpaths...)
config.JPaths = append([]string{getArgValue(i)}, config.JPaths...)
} else if arg == "-t" || arg == "--tanka" {
tankaMode = true
config.ResolvePathsWithTanka = true
} else if arg == "-l" || arg == "--log-level" {
logLevel, err := log.ParseLevel(getArgValue(i))
if err != nil {
log.Fatalf("Invalid log level: %s", err)
}
log.SetLevel(logLevel)
} else if arg == "--lint" {
lint = true
config.EnableLintDiagnostics = true
} else if arg == "--eval-diags" {
evalDiags = true
config.EnableEvalDiagnostics = true
}

}
Expand All @@ -107,14 +108,7 @@ func main() {
conn := jsonrpc2.NewConn(stream)
client := protocol.ClientDispatcher(conn)

s := server.NewServer(name, version, client)
if tankaMode {
s = s.WithTankaVM(jpaths)
} else {
s = s.WithStaticVM(jpaths)
}
s.LintDiags = lint
s.EvalDiags = evalDiags
s := server.NewServer(name, version, client, config)

conn.Go(ctx, protocol.Handlers(
protocol.ServerHandler(s, jsonrpc2.MethodNotFound)))
Expand Down
36 changes: 33 additions & 3 deletions pkg/server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ import (
"github.com/jdbaldry/go-language-server-protocol/jsonrpc2"
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
)

type Configuration struct {
ResolvePathsWithTanka bool
JPaths []string
ExtVars map[string]string
FormattingOptions formatter.Options

EnableEvalDiagnostics bool
EnableLintDiagnostics bool
}

func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.DidChangeConfigurationParams) error {
settingsMap, ok := params.Settings.(map[string]interface{})
if !ok {
Expand All @@ -20,24 +31,43 @@ func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.Di

for sk, sv := range settingsMap {
switch sk {
case "log_level":
level, err := log.ParseLevel(sv.(string))
if err != nil {
return fmt.Errorf("%w: %v", jsonrpc2.ErrInvalidParams, err)
}
log.SetLevel(level)
case "resolve_paths_with_tanka":
s.configuration.ResolvePathsWithTanka = sv.(bool)
case "jpath":
svList := sv.([]interface{})
s.configuration.JPaths = make([]string, len(svList))
for i, v := range svList {
s.configuration.JPaths[i] = v.(string)
}
case "enable_eval_diagnostics":
s.configuration.EnableEvalDiagnostics = sv.(bool)
case "enable_lint_diagnostics":
s.configuration.EnableLintDiagnostics = sv.(bool)
case "ext_vars":
newVars, err := s.parseExtVars(sv)
if err != nil {
return fmt.Errorf("%w: ext_vars parsing failed: %v", jsonrpc2.ErrInvalidParams, err)
}
s.extVars = newVars

s.configuration.ExtVars = newVars
case "formatting":
newFmtOpts, err := s.parseFormattingOpts(sv)
if err != nil {
return fmt.Errorf("%w: formatting options parsing failed: %v", jsonrpc2.ErrInvalidParams, err)
}
s.fmtOpts = newFmtOpts
s.configuration.FormattingOptions = newFmtOpts

default:
return fmt.Errorf("%w: unsupported settings key: %q", jsonrpc2.ErrInvalidParams, sk)
}
}
log.Infof("configuration updated: %+v", s.configuration)

return nil
}

Expand Down
95 changes: 74 additions & 21 deletions pkg/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func TestConfiguration(t *testing.T) {

func TestConfiguration_Formatting(t *testing.T) {
type kase struct {
name string
settings interface{}
expectedOptions formatter.Options
expectedErr error
name string
settings interface{}
expectedConfiguration Configuration
expectedErr error
}

testCases := []kase{
Expand All @@ -146,21 +146,23 @@ func TestConfiguration_Formatting(t *testing.T) {
// not setting StripAllButComments
},
},
expectedOptions: func() formatter.Options {
opts := formatter.DefaultOptions()
opts.Indent = 4
opts.MaxBlankLines = 10
opts.StringStyle = formatter.StringStyleSingle
opts.CommentStyle = formatter.CommentStyleLeave
opts.PrettyFieldNames = true
opts.PadArrays = false
opts.PadObjects = true
opts.SortImports = false
opts.UseImplicitPlus = true
opts.StripEverything = false
opts.StripComments = false
return opts
}(),
expectedConfiguration: Configuration{
FormattingOptions: func() formatter.Options {
opts := formatter.DefaultOptions()
opts.Indent = 4
opts.MaxBlankLines = 10
opts.StringStyle = formatter.StringStyleSingle
opts.CommentStyle = formatter.CommentStyleLeave
opts.PrettyFieldNames = true
opts.PadArrays = false
opts.PadObjects = true
opts.SortImports = false
opts.UseImplicitPlus = true
opts.StripEverything = false
opts.StripComments = false
return opts
}(),
},
},
{
name: "invalid string style",
Expand All @@ -185,7 +187,58 @@ func TestConfiguration_Formatting(t *testing.T) {
settings: map[string]interface{}{
"formatting": map[string]interface{}{},
},
expectedOptions: formatter.DefaultOptions(),
expectedConfiguration: Configuration{FormattingOptions: formatter.DefaultOptions()},
},
{
name: "all settings",
settings: map[string]interface{}{
"formatting": map[string]interface{}{
"Indent": 4,
"MaxBlankLines": 10,
"StringStyle": "double",
"CommentStyle": "slash",
"PrettyFieldNames": false,
"PadArrays": true,
"PadObjects": false,
"SortImports": false,
"UseImplicitPlus": false,
"StripEverything": true,
"StripComments": true,
"StripAllButComments": true,
},
"ext_vars": map[string]interface{}{
"hello": "world",
},
"resolve_paths_with_tanka": false,
"jpath": []interface{}{"blabla", "blabla2"},
"enable_eval_diagnostics": false,
"enable_lint_diagnostics": true,
},
expectedConfiguration: Configuration{
FormattingOptions: func() formatter.Options {
opts := formatter.DefaultOptions()
opts.Indent = 4
opts.MaxBlankLines = 10
opts.StringStyle = formatter.StringStyleDouble
opts.CommentStyle = formatter.CommentStyleSlash
opts.PrettyFieldNames = false
opts.PadArrays = true
opts.PadObjects = false
opts.SortImports = false
opts.UseImplicitPlus = false
opts.StripEverything = true
opts.StripComments = true
opts.StripAllButComments = true
return opts
}(),
ExtVars: map[string]string{
"hello": "world",
},
ResolvePathsWithTanka: false,
JPaths: []string{"blabla", "blabla2"},
EnableEvalDiagnostics: false,
EnableLintDiagnostics: true,
},
},
}

Expand All @@ -208,7 +261,7 @@ func TestConfiguration_Formatting(t *testing.T) {
return
}

assert.Equal(t, tc.expectedOptions, s.fmtOpts)
assert.Equal(t, tc.expectedConfiguration, s.configuration)
})
}
}
15 changes: 9 additions & 6 deletions pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,9 @@ func TestDefinition(t *testing.T) {
},
}

server := NewServer("any", "test version", nil)
server.getVM = testGetVM
server := NewServer("any", "test version", nil, Configuration{
JPaths: []string{"testdata"},
})
serverOpenTestFile(t, server, string(tc.filename))
response, err := server.definitionLink(context.Background(), params)
require.NoError(t, err)
Expand Down Expand Up @@ -775,8 +776,9 @@ func BenchmarkDefinition(b *testing.B) {
Position: tc.position,
},
}
server := NewServer("any", "test version", nil)
server.getVM = testGetVM
server := NewServer("any", "test version", nil, Configuration{
JPaths: []string{"testdata"},
})
serverOpenTestFile(b, server, string(tc.filename))

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -845,8 +847,9 @@ func TestDefinitionFail(t *testing.T) {
},
}

server := NewServer("any", "test version", nil)
server.getVM = testGetVM
server := NewServer("any", "test version", nil, Configuration{
JPaths: []string{"testdata"},
})
serverOpenTestFile(t, server, tc.filename)
got, err := server.definitionLink(context.Background(), params)

Expand Down
6 changes: 3 additions & 3 deletions pkg/server/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ func (s *server) diagnosticsLoop() {
}()

lintChannel := make(chan []protocol.Diagnostic, 1)
if s.LintDiags {
if s.configuration.EnableLintDiagnostics {
go func() {
lintChannel <- s.getLintDiags(doc)
}()
}

diags = append(diags, <-evalChannel...)

if s.LintDiags {
if s.configuration.EnableLintDiagnostics {
err = s.client.PublishDiagnostics(context.Background(), &protocol.PublishDiagnosticsParams{
URI: uri,
Diagnostics: diags,
Expand Down Expand Up @@ -149,7 +149,7 @@ func (s *server) diagnosticsLoop() {
}

func (s *server) getEvalDiags(doc *document) (diags []protocol.Diagnostic) {
if doc.err == nil && s.EvalDiags {
if doc.err == nil && s.configuration.EnableEvalDiagnostics {
vm, err := s.getVM(doc.item.URI.SpanURI().Filename())
if err != nil {
log.Errorf("getEvalDiags: %s: %v\n", errorRetrievingDocument, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormat
return nil, utils.LogErrorf("Formatting: %s: %w", errorRetrievingDocument, err)
}

formatted, err := formatter.Format(params.TextDocument.URI.SpanURI().Filename(), doc.item.Text, s.fmtOpts)
formatted, err := formatter.Format(params.TextDocument.URI.SpanURI().Filename(), doc.item.Text, s.configuration.FormattingOptions)
if err != nil {
log.Errorf("error formatting document: %v", err)
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/formatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestFormatting(t *testing.T) {
}
testCases := []kase{
{
name: "default settings",
settings: nil,
name: "default settings",
settings: nil,
fileContent: "{foo: 'bar'}",
expected: []protocol.TextEdit{
{Range: makeRange(t, "0:0-1:0"), NewText: ""},
Expand Down
Loading