2019-06-11 13:09:43 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
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)
|
2019-09-27 11:17:59 -06:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
ident, err := source.Identifier(ctx, view, f, params.Position)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
edits, err := ident.Rename(ctx, params.NewName)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
var docChanges []protocol.TextDocumentEdit
|
2019-09-06 12:55:14 -06:00
|
|
|
for uri, e := range edits {
|
2019-11-12 15:58:37 -07:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fh := ident.Snapshot.Handle(ctx, f)
|
|
|
|
docChanges = append(docChanges, documentChanges(fh, e)...)
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
return &protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: docChanges,
|
|
|
|
}, nil
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-08-22 11:31:03 -06:00
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) prepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.Range, error) {
|
2019-08-22 11:31:03 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.session.ViewOf(uri)
|
2019-09-27 11:17:59 -06:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
2019-08-22 11:31:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
ident, err := source.Identifier(ctx, view, f, params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil // ignore errors
|
|
|
|
}
|
2019-09-05 18:04:28 -06:00
|
|
|
// Do not return errors here, as it adds clutter.
|
|
|
|
// Returning a nil result means there is not a valid rename.
|
2019-11-12 15:58:37 -07:00
|
|
|
item, err := ident.PrepareRename(ctx)
|
2019-08-22 11:31:03 -06:00
|
|
|
if err != nil {
|
2019-11-12 15:58:37 -07:00
|
|
|
return nil, nil // ignore errors
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|
|
|
|
// TODO(suzmue): return ident.Name as the placeholder text.
|
2019-09-05 18:04:28 -06:00
|
|
|
return &item.Range, nil
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|