github.com/v2fly/tools@v0.100.0/internal/lsp/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 lsp
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/v2fly/tools/internal/lsp/protocol"
    11  	"github.com/v2fly/tools/internal/lsp/source"
    12  )
    13  
    14  func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
    15  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    16  	defer release()
    17  	if !ok {
    18  		return nil, err
    19  	}
    20  	edits, err := source.Rename(ctx, snapshot, fh, params.Position, params.NewName)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	var docChanges []protocol.TextDocumentEdit
    26  	for uri, e := range edits {
    27  		fh, err := snapshot.GetVersionedFile(ctx, uri)
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		docChanges = append(docChanges, documentChanges(fh, e)...)
    32  	}
    33  	return &protocol.WorkspaceEdit{
    34  		DocumentChanges: docChanges,
    35  	}, nil
    36  }
    37  
    38  func (s *Server) prepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.Range, error) {
    39  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    40  	defer release()
    41  	if !ok {
    42  		return nil, err
    43  	}
    44  	// Do not return errors here, as it adds clutter.
    45  	// Returning a nil result means there is not a valid rename.
    46  	item, usererr, err := source.PrepareRename(ctx, snapshot, fh, params.Position)
    47  	if err != nil {
    48  		// Return usererr here rather than err, to avoid cluttering the UI with
    49  		// internal error details.
    50  		return nil, usererr
    51  	}
    52  	// TODO(suzmue): return ident.Name as the placeholder text.
    53  	return &item.Range, nil
    54  }