golang.org/x/tools/gopls@v0.15.3/internal/cmd/references.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  	"sort"
    12  
    13  	"golang.org/x/tools/gopls/internal/protocol"
    14  	"golang.org/x/tools/internal/tool"
    15  )
    16  
    17  // references implements the references verb for gopls
    18  type references struct {
    19  	IncludeDeclaration bool `flag:"d,declaration" help:"include the declaration of the specified identifier in the results"`
    20  
    21  	app *Application
    22  }
    23  
    24  func (r *references) Name() string      { return "references" }
    25  func (r *references) Parent() string    { return r.app.Name() }
    26  func (r *references) Usage() string     { return "[references-flags] <position>" }
    27  func (r *references) ShortHelp() string { return "display selected identifier's references" }
    28  func (r *references) DetailedHelp(f *flag.FlagSet) {
    29  	fmt.Fprint(f.Output(), `
    30  Example:
    31  
    32  	$ # 1-indexed location (:line:column or :#offset) of the target identifier
    33  	$ gopls references helper/helper.go:8:6
    34  	$ gopls references helper/helper.go:#53
    35  
    36  references-flags:
    37  `)
    38  	printFlagDefaults(f)
    39  }
    40  
    41  func (r *references) Run(ctx context.Context, args ...string) error {
    42  	if len(args) != 1 {
    43  		return tool.CommandLineErrorf("references expects 1 argument (position)")
    44  	}
    45  
    46  	conn, err := r.app.connect(ctx, nil)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	defer conn.terminate(ctx)
    51  
    52  	from := parseSpan(args[0])
    53  	file, err := conn.openFile(ctx, from.URI())
    54  	if err != nil {
    55  		return err
    56  	}
    57  	loc, err := file.spanLocation(from)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	p := protocol.ReferenceParams{
    62  		Context: protocol.ReferenceContext{
    63  			IncludeDeclaration: r.IncludeDeclaration,
    64  		},
    65  		TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
    66  	}
    67  	locations, err := conn.References(ctx, &p)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	var spans []string
    72  	for _, l := range locations {
    73  		f, err := conn.openFile(ctx, l.URI)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		// convert location to span for user-friendly 1-indexed line
    78  		// and column numbers
    79  		span, err := f.locationSpan(l)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		spans = append(spans, fmt.Sprint(span))
    84  	}
    85  
    86  	sort.Strings(spans)
    87  	for _, s := range spans {
    88  		fmt.Println(s)
    89  	}
    90  	return nil
    91  }