Skip to content

Commit 163ff8b

Browse files
skewb1kgopherbot
authored andcommitted
gopls: improve SignatureHelp triggering
- Allow manually triggering signature help inside strings. - Prevent window from disappearing when typing or navigating inside strings. - Add ')' as a retrigger character to support editors that don't send signature help requests on every keystroke. Change-Id: I60da67cb9262e01c4acfa436815048f9f7ba7ec2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/684895 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent f7f80ea commit 163ff8b

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

gopls/internal/golang/signature_help.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
// SignatureHelp returns information about the signature of the innermost
2525
// function call enclosing the position, or nil if there is none.
26-
func SignatureHelp(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) (*protocol.SignatureInformation, error) {
26+
func SignatureHelp(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, params *protocol.SignatureHelpParams) (*protocol.SignatureInformation, error) {
2727
ctx, done := event.Start(ctx, "golang.SignatureHelp")
2828
defer done()
2929

@@ -33,7 +33,7 @@ func SignatureHelp(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle
3333
if err != nil {
3434
return nil, fmt.Errorf("getting file for SignatureHelp: %w", err)
3535
}
36-
pos, err := pgf.PositionPos(position)
36+
pos, err := pgf.PositionPos(params.Position)
3737
if err != nil {
3838
return nil, err
3939
}
@@ -78,12 +78,10 @@ loop:
7878
// Don't show signature help in this case.
7979
return nil, nil
8080
case *ast.BasicLit:
81-
if node.Kind == token.STRING {
82-
// golang/go#43397: don't offer signature help when the user is typing
83-
// in a string literal. Most LSP clients use ( or , as trigger
84-
// characters, but within a string literal these should not trigger
85-
// signature help (and it can be annoying when this happens after
86-
// you've already dismissed the help!).
81+
// golang/go#43397: don't offer signature help when the user is typing
82+
// in a string literal unless it was manually invoked or help is already active.
83+
if node.Kind == token.STRING &&
84+
(params.Context == nil || (params.Context.TriggerKind != protocol.SigInvoked && !params.Context.IsRetrigger)) {
8785
return nil, nil
8886
}
8987
}
@@ -119,7 +117,7 @@ loop:
119117
// Special handling for error.Error, which is the only builtin method.
120118
if obj.Name() == "Error" {
121119
return &protocol.SignatureInformation{
122-
Label: "Error()",
120+
Label: "Error() string",
123121
// TODO(skewb1k): move the docstring for error.Error to builtin.go and reuse it across all relevant LSP methods.
124122
Documentation: stringToSigInfoDocumentation("Error returns the error message.", snapshot.Options()),
125123
Parameters: nil,

gopls/internal/server/general.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
161161
},
162162
SignatureHelpProvider: &protocol.SignatureHelpOptions{
163163
TriggerCharacters: []string{"(", ","},
164+
// Used to update or dismiss signature help when it's already active,
165+
// typically after a call expression is closed.
166+
RetriggerCharacters: []string{")"},
164167
},
165168
TextDocumentSync: &protocol.TextDocumentSyncOptions{
166169
Change: protocol.Incremental,

gopls/internal/server/signature_help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s *server) SignatureHelp(ctx context.Context, params *protocol.SignatureHe
2828
return nil, nil // empty result
2929
}
3030

31-
info, err := golang.SignatureHelp(ctx, snapshot, fh, params.Position)
31+
info, err := golang.SignatureHelp(ctx, snapshot, fh, params)
3232
if err != nil {
3333
// TODO(rfindley): is this correct? Apparently, returning an error from
3434
// signatureHelp is distracting in some editors, though I haven't confirmed

gopls/internal/test/marker/testdata/completion/testy.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ func _() {
5757
}
5858

5959
func issue63578(err error) {
60-
err.Error() //@signature(")", "Error()", -1)
60+
err.Error() //@signature(")", "Error() string", -1)
6161
}

0 commit comments

Comments
 (0)