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
33 gopls signature flags are:
38 func (r *signature) Run(ctx context.Context, args ...string) error {
40 return tool.CommandLineErrorf("signature expects 1 argument (position)")
43 conn, err := r.app.connect(ctx)
47 defer conn.terminate(ctx)
49 from := span.Parse(args[0])
50 file := conn.AddFile(ctx, from.URI())
55 loc, err := file.mapper.Location(from)
60 tdpp := protocol.TextDocumentPositionParams{
61 TextDocument: protocol.TextDocumentIdentifier{
62 URI: protocol.URIFromSpanURI(from.URI()),
64 Position: loc.Range.Start,
66 p := protocol.SignatureHelpParams{
67 TextDocumentPositionParams: tdpp,
70 s, err := conn.SignatureHelp(ctx, &p)
75 if s == nil || len(s.Signatures) == 0 {
76 return tool.CommandLineErrorf("%v: not a function", from)
79 // there is only ever one possible signature,
80 // see toProtocolSignatureHelp in lsp/signature_help.go
81 signature := s.Signatures[0]
82 fmt.Printf("%s\n", signature.Label)
83 if signature.Documentation != "" {
84 fmt.Printf("\n%s\n", signature.Documentation)