golang.org/x/tools/gopls@v0.15.3/internal/cmd/signature.go (about)

     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.
     4  
     5  package cmd
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  	"golang.org/x/tools/internal/tool"
    14  )
    15  
    16  // signature implements the signature verb for gopls
    17  type signature struct {
    18  	app *Application
    19  }
    20  
    21  func (r *signature) Name() string      { return "signature" }
    22  func (r *signature) Parent() string    { return r.app.Name() }
    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(), `
    27  Example:
    28  
    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
    32  `)
    33  	printFlagDefaults(f)
    34  }
    35  
    36  func (r *signature) Run(ctx context.Context, args ...string) error {
    37  	if len(args) != 1 {
    38  		return tool.CommandLineErrorf("signature expects 1 argument (position)")
    39  	}
    40  
    41  	conn, err := r.app.connect(ctx, nil)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	defer conn.terminate(ctx)
    46  
    47  	from := parseSpan(args[0])
    48  	file, err := conn.openFile(ctx, from.URI())
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	loc, err := file.spanLocation(from)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	p := protocol.SignatureHelpParams{
    59  		TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
    60  	}
    61  
    62  	s, err := conn.SignatureHelp(ctx, &p)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if s == nil || len(s.Signatures) == 0 {
    68  		return tool.CommandLineErrorf("%v: not a function", from)
    69  	}
    70  
    71  	// there is only ever one possible signature,
    72  	// see toProtocolSignatureHelp in lsp/signature_help.go
    73  	signature := s.Signatures[0]
    74  	fmt.Printf("%s\n", signature.Label)
    75  	switch x := signature.Documentation.Value.(type) {
    76  	case string:
    77  		if x != "" {
    78  			fmt.Printf("\n%s\n", x)
    79  		}
    80  	case protocol.MarkupContent:
    81  		if x.Value != "" {
    82  			fmt.Printf("\n%s\n", x.Value)
    83  		}
    84  	}
    85  
    86  	return nil
    87  }