mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
This change provides support to rename identifiers within a single package. The renaming is performed by finding all references to an identifier, and then creating text edits to replace the existing text with the new identifier. Editing an import spec is not supported. Fixes #27571 Change-Id: I0881b65a1b3c72d7c53d7d6ab1ea386160dc00fb Reviewed-on: https://go-review.googlesource.com/c/tools/+/182585 Run-TryBot: Suzy Mueller <suzmue@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package lsp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
view := s.session.ViewOf(uri)
|
|
f, m, err := getGoFile(ctx, view, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn, err := m.PointSpan(params.Position)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rng, err := spn.Range(m.Converter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
edits, err := source.Rename(ctx, view, f, rng.Start, params.NewName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
changes := make(map[string][]protocol.TextEdit)
|
|
for uri, textEdits := range edits {
|
|
_, m, err := getGoFile(ctx, view, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
protocolEdits, err := ToProtocolEdits(m, textEdits)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
changes[string(uri)] = protocolEdits
|
|
}
|
|
|
|
return &protocol.WorkspaceEdit{Changes: &changes}, nil
|
|
}
|