1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:04:41 -07:00
go/internal/lsp/rename.go

53 lines
1.3 KiB
Go
Raw Normal View History

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
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
}
ident, err := source.Identifier(ctx, view, f, rng.Start)
if err != nil {
return nil, err
}
edits, err := ident.Rename(ctx, 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
}