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-08-26 22:26:45 -06:00
|
|
|
edits, err := ident.Rename(ctx, view, params.NewName)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := make(map[string][]protocol.TextEdit)
|
2019-09-06 12:55:14 -06:00
|
|
|
for uri, e := range edits {
|
|
|
|
changes[protocol.NewURI(uri)] = e
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return &protocol.WorkspaceEdit{Changes: &changes}, nil
|
|
|
|
}
|
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-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.
|
|
|
|
item, err := source.PrepareRename(ctx, view, f, params.Position)
|
2019-08-22 11:31:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// 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
|
|
|
}
|