| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/file" |
| 12 |
"golang.org/x/tools/gopls/internal/golang" |
| 13 |
"golang.org/x/tools/gopls/internal/label" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol" |
| 15 |
"golang.org/x/tools/internal/event" |
| 16 |
) |
| 17 |
|
| 18 |
func (s *server) Rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) { |
| 19 |
countRename.Inc() |
| 20 |
ctx, done := event.Start(ctx, "server.Rename", label.URI.Of(params.TextDocument.URI)) |
| 21 |
defer done() |
| 22 |
|
| 23 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
defer release() |
| 28 |
|
| 29 |
if kind := snapshot.FileKind(fh); kind != file.Go { |
| 30 |
return nil, fmt.Errorf("cannot rename in file of type %s", kind) |
| 31 |
} |
| 32 |
|
| 33 |
changes, err := golang.Rename(ctx, snapshot, fh, params.Range, params.NewName) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
return protocol.NewWorkspaceEdit(changes...), nil |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func (s *server) PrepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.PrepareRenamePlaceholder, error) { |
| 47 |
ctx, done := event.Start(ctx, "server.PrepareRename", label.URI.Of(params.TextDocument.URI)) |
| 48 |
defer done() |
| 49 |
|
| 50 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
defer release() |
| 55 |
|
| 56 |
if kind := snapshot.FileKind(fh); kind != file.Go { |
| 57 |
return nil, fmt.Errorf("cannot rename in file of type %s", kind) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
item, usererr, err := golang.PrepareRename(ctx, snapshot, fh, params.Range) |
| 63 |
if err != nil { |
| 64 |
|
| 65 |
|
| 66 |
return nil, usererr |
| 67 |
} |
| 68 |
return &protocol.PrepareRenamePlaceholder{ |
| 69 |
Range: item.Range, |
| 70 |
Placeholder: item.Text, |
| 71 |
}, nil |
| 72 |
} |