@@ -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.
0 commit comments