github.com/v2fly/tools@v0.100.0/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/v2fly/tools/internal/lsp/protocol"
    13  	"github.com/v2fly/tools/internal/span"
    14  	"github.com/v2fly/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) 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  	f.PrintDefaults()
    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)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	defer conn.terminate(ctx)
    46  
    47  	from := span.Parse(args[0])
    48  	file := conn.AddFile(ctx, from.URI())
    49  	if file.err != nil {
    50  		return file.err
    51  	}
    52  
    53  	loc, err := file.mapper.Location(from)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	tdpp := protocol.TextDocumentPositionParams{
    59  		TextDocument: protocol.TextDocumentIdentifier{
    60  			URI: protocol.URIFromSpanURI(from.URI()),
    61  		},
    62  		Position: loc.Range.Start,
    63  	}
    64  	p := protocol.SignatureHelpParams{
    65  		TextDocumentPositionParams: tdpp,
    66  	}
    67  
    68  	s, err := conn.SignatureHelp(ctx, &p)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if s == nil || len(s.Signatures) == 0 {
    74  		return tool.CommandLineErrorf("%v: not a function", from)
    75  	}
    76  
    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)
    83  	}
    84  
    85  	return nil
    86  }