github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/cmd/prepare_rename.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/april1989/origin-go-tools/internal/lsp/protocol"
    13  	"github.com/april1989/origin-go-tools/internal/span"
    14  	"github.com/april1989/origin-go-tools/internal/tool"
    15  	errors "golang.org/x/xerrors"
    16  )
    17  
    18  // prepareRename implements the prepare_rename verb for gopls.
    19  type prepareRename struct {
    20  	app *Application
    21  }
    22  
    23  func (r *prepareRename) Name() string      { return "prepare_rename" }
    24  func (r *prepareRename) Usage() string     { return "<position>" }
    25  func (r *prepareRename) ShortHelp() string { return "test validity of a rename operation at location" }
    26  func (r *prepareRename) 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 prepare_rename helper/helper.go:8:6
    32  	$ gopls prepare_rename helper/helper.go:#53
    33  
    34  	gopls prepare_rename flags are:
    35  `)
    36  	f.PrintDefaults()
    37  }
    38  
    39  // ErrInvalidRenamePosition is returned when prepareRename is run at a position that
    40  // is not a candidate for renaming.
    41  var ErrInvalidRenamePosition = errors.New("request is not valid at the given position")
    42  
    43  func (r *prepareRename) Run(ctx context.Context, args ...string) error {
    44  	if len(args) != 1 {
    45  		return tool.CommandLineErrorf("prepare_rename expects 1 argument (file)")
    46  	}
    47  
    48  	conn, err := r.app.connect(ctx)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	defer conn.terminate(ctx)
    53  
    54  	from := span.Parse(args[0])
    55  	file := conn.AddFile(ctx, from.URI())
    56  	if file.err != nil {
    57  		return file.err
    58  	}
    59  	loc, err := file.mapper.Location(from)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	p := protocol.PrepareRenameParams{
    64  		TextDocumentPositionParams: protocol.TextDocumentPositionParams{
    65  			TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
    66  			Position:     loc.Range.Start,
    67  		},
    68  	}
    69  	result, err := conn.PrepareRename(ctx, &p)
    70  	if err != nil {
    71  		return errors.Errorf("prepare_rename failed: %w", err)
    72  	}
    73  	if result == nil {
    74  		return ErrInvalidRenamePosition
    75  	}
    76  
    77  	l := protocol.Location{Range: *result}
    78  	s, err := file.mapper.Span(l)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	fmt.Println(s)
    84  	return nil
    85  }