Skip to content

Commit 077f385

Browse files
madelinekalilgopherbot
authored andcommitted
gopls/internal/server: produce DocumentChange list in golang.Rename
Previously, golang.Rename returned isPkgRenaming and then the server package add additional DocumentChanges before returning the final list. This CL moves the logic for creating all edits and transforming them to a list of DocumentChanges to the golang package. Change-Id: Iaff2be270cc954ac1d0611cdfa901347247ce21c Reviewed-on: https://go-review.googlesource.com/c/tools/+/708955 Auto-Submit: Madeline Kalil <mkalil@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Bypass: Madeline Kalil <mkalil@google.com>
1 parent 9e69d8b commit 077f385

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

gopls/internal/golang/rename.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,56 +398,69 @@ func checkRenamable(obj types.Object, node ast.Node) error {
398398
return nil
399399
}
400400

401+
// editsToDocChanges converts a map of uris to arrays of text edits to a list of document changes.
402+
func editsToDocChanges(ctx context.Context, snapshot *cache.Snapshot, edits map[protocol.DocumentURI][]protocol.TextEdit) ([]protocol.DocumentChange, error) {
403+
var changes []protocol.DocumentChange
404+
for uri, e := range edits {
405+
fh, err := snapshot.ReadFile(ctx, uri)
406+
if err != nil {
407+
return nil, err
408+
}
409+
changes = append(changes, protocol.DocumentChangeEdit(fh, e))
410+
}
411+
return changes, nil
412+
}
413+
401414
// Rename returns a map of TextEdits for each file modified when renaming a
402415
// given identifier within a package and a boolean value of true for renaming
403416
// package and false otherwise.
404-
func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp protocol.Position, newName string) (map[protocol.DocumentURI][]protocol.TextEdit, bool, error) {
417+
func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp protocol.Position, newName string) ([]protocol.DocumentChange, error) {
405418
ctx, done := event.Start(ctx, "golang.Rename")
406419
defer done()
407420

408421
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, f.URI())
409422
if err != nil {
410-
return nil, false, err
423+
return nil, err
411424
}
412425
pos, err := pgf.PositionPos(pp)
413426
if err != nil {
414-
return nil, false, err
427+
return nil, err
415428
}
416429

417430
cur, ok := pgf.Cursor.FindByPos(pos, pos)
418431
if !ok {
419-
return nil, false, fmt.Errorf("can't find cursor for selection")
432+
return nil, fmt.Errorf("can't find cursor for selection")
420433
}
421434

422435
if edits, err := renameFuncSignature(ctx, pkg, pgf, pos, snapshot, cur, f, pp, newName); err != nil {
423-
return nil, false, err
436+
return nil, err
424437
} else if edits != nil {
425-
return edits, false, nil
438+
return editsToDocChanges(ctx, snapshot, edits)
426439
}
427440

428441
// Cursor within package name declaration?
429442
_, inPackageName, err := parsePackageNameDecl(ctx, snapshot, f, pp)
430443
if err != nil {
431-
return nil, false, err
444+
return nil, err
432445
}
433446

434447
var editMap map[protocol.DocumentURI][]diff.Edit
435448
if inPackageName {
436449
countRenamePackage.Inc()
437450
if !isValidPackagePath(pkg.String(), newName) {
438-
return nil, false, fmt.Errorf("invalid package path: %q (package moves are not yet supported, see go.dev/issue/57171)", newName)
451+
return nil, fmt.Errorf("invalid package path: %q (package moves are not yet supported, see go.dev/issue/57171)", newName)
439452
}
440453
// Only the last element of the path is required as input for [renamePackageName].
441454
newName = path.Base(newName)
442455
editMap, err = renamePackageName(ctx, snapshot, f, PackageName(newName))
443456
} else {
444457
if !isValidIdentifier(newName) {
445-
return nil, false, fmt.Errorf("invalid identifier to rename: %q", newName)
458+
return nil, fmt.Errorf("invalid identifier to rename: %q", newName)
446459
}
447460
editMap, err = renameOrdinary(ctx, snapshot, f.URI(), pp, newName)
448461
}
449462
if err != nil {
450-
return nil, false, err
463+
return nil, err
451464
}
452465

453466
// Convert edits to protocol form.
@@ -476,21 +489,33 @@ func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp pro
476489
// vendor/k8s.io/kubectl -> ../../staging/src/k8s.io/kubectl.
477490
fh, err := snapshot.ReadFile(ctx, uri)
478491
if err != nil {
479-
return nil, false, err
492+
return nil, err
480493
}
481494
data, err := fh.Content()
482495
if err != nil {
483-
return nil, false, err
496+
return nil, err
484497
}
485498
m := protocol.NewMapper(uri, data)
486499
textedits, err := protocol.EditsFromDiffEdits(m, edits)
487500
if err != nil {
488-
return nil, false, err
501+
return nil, err
489502
}
490503
result[uri] = textedits
491504
}
492505

493-
return result, inPackageName, nil
506+
changes, err := editsToDocChanges(ctx, snapshot, result)
507+
if err != nil {
508+
return nil, err
509+
}
510+
// Update the last component of the file's enclosing directory.
511+
if inPackageName {
512+
oldDir := f.URI().DirPath()
513+
newDir := filepath.Join(filepath.Dir(oldDir), path.Base(newName))
514+
changes = append(changes, protocol.DocumentChangeRename(
515+
protocol.URIFromPath(oldDir),
516+
protocol.URIFromPath(newDir)))
517+
}
518+
return changes, nil
494519
}
495520

496521
// renameOrdinary renames an ordinary (non-package) name throughout the workspace.

gopls/internal/server/rename.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package server
77
import (
88
"context"
99
"fmt"
10-
"path"
11-
"path/filepath"
1210

1311
"golang.org/x/tools/gopls/internal/file"
1412
"golang.org/x/tools/gopls/internal/golang"
@@ -32,34 +30,10 @@ func (s *server) Rename(ctx context.Context, params *protocol.RenameParams) (*pr
3230
return nil, fmt.Errorf("cannot rename in file of type %s", kind)
3331
}
3432

35-
// Because we don't handle directory renaming within golang.Rename, golang.Rename returns
36-
// boolean value isPkgRenaming to determine whether any DocumentChanges of type RenameFile should
37-
// be added to the return protocol.WorkspaceEdit value.
38-
edits, isPkgRenaming, err := golang.Rename(ctx, snapshot, fh, params.Position, params.NewName)
33+
changes, err := golang.Rename(ctx, snapshot, fh, params.Position, params.NewName)
3934
if err != nil {
4035
return nil, err
4136
}
42-
43-
var changes []protocol.DocumentChange
44-
for uri, e := range edits {
45-
fh, err := snapshot.ReadFile(ctx, uri)
46-
if err != nil {
47-
return nil, err
48-
}
49-
change := protocol.DocumentChangeEdit(fh, e)
50-
changes = append(changes, change)
51-
}
52-
53-
if isPkgRenaming {
54-
// Update the last component of the file's enclosing directory.
55-
oldDir := fh.URI().DirPath()
56-
newDir := filepath.Join(filepath.Dir(oldDir), path.Base(params.NewName))
57-
change := protocol.DocumentChangeRename(
58-
protocol.URIFromPath(oldDir),
59-
protocol.URIFromPath(newDir))
60-
changes = append(changes, change)
61-
}
62-
6337
return protocol.NewWorkspaceEdit(changes...), nil
6438
}
6539

0 commit comments

Comments
 (0)