From 556e8df729836cb4cfa5bbca54c352076972c20d Mon Sep 17 00:00:00 2001 From: Duologic Date: Fri, 15 Mar 2024 13:02:51 +0100 Subject: [PATCH] feat: hide docstrings in completion by default --- main.go | 7 +++++-- pkg/server/completion.go | 8 ++++++-- pkg/server/configuration.go | 11 +++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 25f0520..3f8d076 100644 --- a/main.go +++ b/main.go @@ -55,8 +55,9 @@ Environment variables: func main() { config := server.Configuration{ - JPaths: filepath.SplitList(os.Getenv("JSONNET_PATH")), - FormattingOptions: formatter.DefaultOptions(), + JPaths: filepath.SplitList(os.Getenv("JSONNET_PATH")), + FormattingOptions: formatter.DefaultOptions(), + ShowDocstringInCompletion: false, } log.SetLevel(log.InfoLevel) @@ -82,6 +83,8 @@ func main() { config.EnableLintDiagnostics = true case "--eval-diags": config.EnableEvalDiagnostics = true + case "--show-docstrings": + config.ShowDocstringInCompletion = true } } diff --git a/pkg/server/completion.go b/pkg/server/completion.go index 0060b2d..de5cd7b 100644 --- a/pkg/server/completion.go +++ b/pkg/server/completion.go @@ -90,7 +90,7 @@ func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm } completionPrefix := strings.Join(indexes[:len(indexes)-1], ".") - return createCompletionItemsFromRanges(ranges, completionPrefix, line, position) + return s.createCompletionItemsFromRanges(ranges, completionPrefix, line, position) } func (s *Server) completionStdLib(line string) []protocol.CompletionItem { @@ -132,7 +132,7 @@ func (s *Server) completionStdLib(line string) []protocol.CompletionItem { return items } -func createCompletionItemsFromRanges(ranges []processing.ObjectRange, completionPrefix, currentLine string, position protocol.Position) []protocol.CompletionItem { +func (s *Server) createCompletionItemsFromRanges(ranges []processing.ObjectRange, completionPrefix, currentLine string, position protocol.Position) []protocol.CompletionItem { var items []protocol.CompletionItem labels := make(map[string]bool) @@ -147,6 +147,10 @@ func createCompletionItemsFromRanges(ranges []processing.ObjectRange, completion continue } + if !s.configuration.ShowDocstringInCompletion && strings.HasPrefix(label, "#") { + continue + } + // Ignore the current field if strings.Contains(currentLine, label+":") && completionPrefix == "self" { continue diff --git a/pkg/server/configuration.go b/pkg/server/configuration.go index c970842..0638c06 100644 --- a/pkg/server/configuration.go +++ b/pkg/server/configuration.go @@ -20,8 +20,9 @@ type Configuration struct { ExtCode map[string]string FormattingOptions formatter.Options - EnableEvalDiagnostics bool - EnableLintDiagnostics bool + EnableEvalDiagnostics bool + EnableLintDiagnostics bool + ShowDocstringInCompletion bool } func (s *Server) DidChangeConfiguration(_ context.Context, params *protocol.DidChangeConfigurationParams) error { @@ -70,6 +71,12 @@ func (s *Server) DidChangeConfiguration(_ context.Context, params *protocol.DidC } else { return fmt.Errorf("%w: unsupported settings value for enable_lint_diagnostics. expected boolean. got: %T", jsonrpc2.ErrInvalidParams, sv) } + case "show_docstring_in_completion": + if boolVal, ok := sv.(bool); ok { + s.configuration.ShowDocstringInCompletion = boolVal + } else { + return fmt.Errorf("%w: unsupported settings value for show_docstring_in_completion. expected boolean. got: %T", jsonrpc2.ErrInvalidParams, sv) + } case "ext_vars": newVars, err := s.parseExtVars(sv) if err != nil {