Debian Codegraph @

gopls/internal/server/rename.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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 // PrepareRename implements the textDocument/prepareRename handler. It may
41 // return (nil, nil) if there is no rename at the cursor position, but it is
42 // not desirable to display an error to the user.
43 //
44 // TODO(rfindley): why wouldn't we want to show an error to the user, if the
45 // user initiated a rename request at the cursor?
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 // Do not return errors here, as it adds clutter.
61 // Returning a nil result means there is not a valid rename.
62 item, usererr, err := golang.PrepareRename(ctx, snapshot, fh, params.Range)
63 if err != nil {
64 // Return usererr here rather than err, to avoid cluttering the UI with
65 // internal error details.
66 return nil, usererr
67 }
68 return &protocol.PrepareRenamePlaceholder{
69 Range: item.Range,
70 Placeholder: item.Text,
71 }, nil
72 }