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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
|
2020-02-13 11:46:49 -07:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.Go)
|
|
|
|
if !ok {
|
2019-08-16 11:49:17 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
edits, err := source.Rename(ctx, snapshot, fh, params.Position, params.NewName)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
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 {
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-11-12 15:58:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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) {
|
2020-02-13 11:46:49 -07:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.Go)
|
|
|
|
if !ok {
|
2019-08-22 11:31:03 -06:00
|
|
|
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.
|
2019-12-17 22:06:31 -07:00
|
|
|
item, err := source.PrepareRename(ctx, snapshot, fh, params.Position)
|
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
|
|
|
}
|