From 186a7436c9f39f8d8ecd15c017486210bcd732f3 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 28 Oct 2020 23:51:50 -0400 Subject: [PATCH] internal/lsp/source: respect user's hover kind in signature help I had originally wanted to create a shared code path for hover in all cases, but hover has a lot more differences from the documentation in signature help and completion than I expected. You can't use markdown, and you probably don't want links--it would take a bigger refactor to extract something that worked for each feature. Handling the Structured and SingleLine hover setting also doesn't seem necessary--those settings are really specific to the way the client presents the hover, which isn't related to signature help or completion. For completion, all we need is an extra check on the hover kind for the NoDocumentation option. Fixes golang/go#38577 Change-Id: Ib2037906c13f5be26813fcd2c20989e4d1b6c9bd Reviewed-on: https://go-review.googlesource.com/c/tools/+/266139 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick Trust: Rebecca Stambler --- internal/lsp/source/completion/completion.go | 2 +- internal/lsp/source/signature_help.go | 5 ++-- internal/lsp/source/types_format.go | 30 ++++++++++++++------ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index 417b2879f1..78dac5b06b 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -510,7 +510,7 @@ func Completion(ctx context.Context, snapshot source.Snapshot, fh source.FileHan opts: &completionOptions{ matcher: opts.Matcher, unimported: opts.CompleteUnimported, - documentation: opts.CompletionDocumentation, + documentation: opts.CompletionDocumentation && opts.HoverKind != source.NoDocumentation, fullDocumentation: opts.HoverKind == source.FullDocumentation, placeholders: opts.UsePlaceholders, literal: opts.LiteralCompletions && opts.InsertTextFormat == protocol.SnippetTextFormat, diff --git a/internal/lsp/source/signature_help.go b/internal/lsp/source/signature_help.go index b49880dc20..03ac4e8a59 100644 --- a/internal/lsp/source/signature_help.go +++ b/internal/lsp/source/signature_help.go @@ -7,7 +7,6 @@ package source import ( "context" "go/ast" - "go/doc" "go/token" "go/types" @@ -123,7 +122,7 @@ FindCall: } return &protocol.SignatureInformation{ Label: name + s.Format(), - Documentation: doc.Synopsis(s.doc), + Documentation: s.doc, Parameters: paramInfo, }, activeParam, nil } @@ -140,7 +139,7 @@ func builtinSignature(ctx context.Context, snapshot Snapshot, callExpr *ast.Call activeParam := activeParameter(callExpr, len(sig.params), sig.variadic, pos) return &protocol.SignatureInformation{ Label: sig.name + sig.Format(), - Documentation: doc.Synopsis(sig.doc), + Documentation: sig.doc, Parameters: paramInfo, }, activeParam, nil diff --git a/internal/lsp/source/types_format.go b/internal/lsp/source/types_format.go index 2455bdfdb9..5ddd8e078f 100644 --- a/internal/lsp/source/types_format.go +++ b/internal/lsp/source/types_format.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "go/ast" + "go/doc" "go/printer" "go/token" "go/types" @@ -79,8 +80,8 @@ func (s *signature) Params() []string { // NewBuiltinSignature returns signature for the builtin object with a given // name, if a builtin object with the name exists. -func NewBuiltinSignature(ctx context.Context, snapshot Snapshot, name string) (*signature, error) { - builtin, err := snapshot.BuiltinPackage(ctx) +func NewBuiltinSignature(ctx context.Context, s Snapshot, name string) (*signature, error) { + builtin, err := s.BuiltinPackage(ctx) if err != nil { return nil, err } @@ -103,10 +104,17 @@ func NewBuiltinSignature(ctx context.Context, snapshot Snapshot, name string) (* variadic = true } } - params, _ := formatFieldList(ctx, snapshot, decl.Type.Params, variadic) - results, needResultParens := formatFieldList(ctx, snapshot, decl.Type.Results, false) + params, _ := formatFieldList(ctx, s, decl.Type.Params, variadic) + results, needResultParens := formatFieldList(ctx, s, decl.Type.Results, false) + d := decl.Doc.Text() + switch s.View().Options().HoverKind { + case SynopsisDocumentation: + d = doc.Synopsis(d) + case NoDocumentation: + d = "" + } return &signature{ - doc: decl.Doc.Text(), + doc: d, name: name, needResultParens: needResultParens, params: params, @@ -188,12 +196,18 @@ func NewSignature(ctx context.Context, s Snapshot, pkg Package, sig *types.Signa results = append(results, el.Name()+" "+typ) } } - var doc string + var d string if comment != nil { - doc = comment.Text() + d = comment.Text() + } + switch s.View().Options().HoverKind { + case SynopsisDocumentation: + d = doc.Synopsis(d) + case NoDocumentation: + d = "" } return &signature{ - doc: doc, + doc: d, params: params, results: results, variadic: sig.Variadic(),