1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/tools/internal/lsp/protocol"
13 "golang.org/x/tools/internal/span"
14 "golang.org/x/tools/internal/tool"
17 // signature implements the signature verb for gopls
18 type signature struct {
22 func (r *signature) Name() string { return "signature" }
23 func (r *signature) Usage() string { return "<position>" }
24 func (r *signature) ShortHelp() string { return "display selected identifier's signature" }
25 func (r *signature) DetailedHelp(f *flag.FlagSet) {
26 fmt.Fprint(f.Output(), `
29 $ # 1-indexed location (:line:column or :#offset) of the target identifier
30 $ gopls signature helper/helper.go:8:6
31 $ gopls signature helper/helper.go:#53
36 func (r *signature) Run(ctx context.Context, args ...string) error {
38 return tool.CommandLineErrorf("signature expects 1 argument (position)")
41 conn, err := r.app.connect(ctx)
45 defer conn.terminate(ctx)
47 from := span.Parse(args[0])
48 file := conn.AddFile(ctx, from.URI())
53 loc, err := file.mapper.Location(from)
58 tdpp := protocol.TextDocumentPositionParams{
59 TextDocument: protocol.TextDocumentIdentifier{
60 URI: protocol.URIFromSpanURI(from.URI()),
62 Position: loc.Range.Start,
64 p := protocol.SignatureHelpParams{
65 TextDocumentPositionParams: tdpp,
68 s, err := conn.SignatureHelp(ctx, &p)
73 if s == nil || len(s.Signatures) == 0 {
74 return tool.CommandLineErrorf("%v: not a function", from)
77 // there is only ever one possible signature,
78 // see toProtocolSignatureHelp in lsp/signature_help.go
79 signature := s.Signatures[0]
80 fmt.Printf("%s\n", signature.Label)
81 if signature.Documentation != "" {
82 fmt.Printf("\n%s\n", signature.Documentation)