github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/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  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    13  	"github.com/powerman/golang-tools/internal/span"
    14  	"github.com/powerman/golang-tools/internal/tool"
    15  )
    16  
    17  // signature implements the signature verb for gopls
    18  type signature struct {
    19  	app *Application
    20  }
    21  
    22  func (r *signature) Name() string      { return "signature" }
    23  func (r *signature) Parent() string    { return r.app.Name() }
    24  func (r *signature) Usage() string     { return "<position>" }
    25  func (r *signature) ShortHelp() string { return "display selected identifier's signature" }
    26  func (r *signature) DetailedHelp(f *flag.FlagSet) {
    27  	fmt.Fprint(f.Output(), `
    28  Example:
    29  
    30  	$ # 1-indexed location (:line:column or :#offset) of the target identifier
    31  	$ gopls signature helper/helper.go:8:6
    32  	$ gopls signature helper/helper.go:#53
    33  `)
    34  	printFlagDefaults(f)
    35  }
    36  
    37  func (r *signature) Run(ctx context.Context, args ...string) error {
    38  	if len(args) != 1 {
    39  		return tool.CommandLineErrorf("signature expects 1 argument (position)")
    40  	}
    41  
    42  	conn, err := r.app.connect(ctx)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	defer conn.terminate(ctx)
    47  
    48  	from := span.Parse(args[0])
    49  	file := conn.AddFile(ctx, from.URI())
    50  	if file.err != nil {
    51  		return file.err
    52  	}
    53  
    54  	loc, err := file.mapper.Location(from)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	tdpp := protocol.TextDocumentPositionParams{
    60  		TextDocument: protocol.TextDocumentIdentifier{
    61  			URI: protocol.URIFromSpanURI(from.URI()),
    62  		},
    63  		Position: loc.Range.Start,
    64  	}
    65  	p := protocol.SignatureHelpParams{
    66  		TextDocumentPositionParams: tdpp,
    67  	}
    68  
    69  	s, err := conn.SignatureHelp(ctx, &p)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	if s == nil || len(s.Signatures) == 0 {
    75  		return tool.CommandLineErrorf("%v: not a function", from)
    76  	}
    77  
    78  	// there is only ever one possible signature,
    79  	// see toProtocolSignatureHelp in lsp/signature_help.go
    80  	signature := s.Signatures[0]
    81  	fmt.Printf("%s\n", signature.Label)
    82  	if signature.Documentation != "" {
    83  		fmt.Printf("\n%s\n", signature.Documentation)
    84  	}
    85  
    86  	return nil
    87  }