@@ -53,7 +53,6 @@ import (
5353 "go/types"
5454 "maps"
5555 "os"
56- "path"
5756 "path/filepath"
5857 "regexp"
5958 "slices"
@@ -199,12 +198,14 @@ func prepareRenamePackageName(ctx context.Context, snapshot *cache.Snapshot, pgf
199198 if meta .Name == "main" {
200199 return nil , fmt .Errorf ("can't rename package \" main\" " )
201200 }
201+ // TODO(mkalil): support renaming x_test packages.
202202 if strings .HasSuffix (string (meta .Name ), "_test" ) {
203203 return nil , fmt .Errorf ("can't rename x_test packages" )
204204 }
205205 if meta .Module == nil {
206206 return nil , fmt .Errorf ("can't rename package: missing module information for package %q" , meta .PkgPath )
207207 }
208+ // TODO(mkalil): why is renaming the root package of a module unsupported?
208209 if meta .Module .Path == string (meta .PkgPath ) {
209210 return nil , fmt .Errorf ("can't rename package: package path %q is the same as module path %q" , meta .PkgPath , meta .Module .Path )
210211 }
@@ -215,20 +216,9 @@ func prepareRenamePackageName(ctx context.Context, snapshot *cache.Snapshot, pgf
215216 return nil , err
216217 }
217218
218- pkgName := string (meta .Name )
219- fullPath := string (meta .PkgPath )
220- text := pkgName
221- // Before displaying the full package path, verify that the PackageMove
222- // setting is enabled and that the package name matches its directory
223- // basename. Checking the value of meta.Module above ensures that the
224- // current view is either a GoMod or a GoWork view, which are the only views
225- // for which we should enable package move.
226- if snapshot .Options ().PackageMove && path .Base (fullPath ) == pkgName {
227- text = fullPath
228- }
229219 return & PrepareItem {
230220 Range : rng ,
231- Text : text ,
221+ Text : string ( meta . PkgPath ) ,
232222 }, nil
233223}
234224
@@ -420,7 +410,7 @@ func editsToDocChanges(ctx context.Context, snapshot *cache.Snapshot, edits map[
420410
421411// Rename returns a map of TextEdits for each file modified when renaming a
422412// given identifier within a package.
423- func Rename (ctx context.Context , snapshot * cache.Snapshot , f file.Handle , pp protocol.Position , newName string , renameSubpkgs bool ) ([]protocol.DocumentChange , error ) {
413+ func Rename (ctx context.Context , snapshot * cache.Snapshot , f file.Handle , pp protocol.Position , newName string ) ([]protocol.DocumentChange , error ) {
424414 ctx , done := event .Start (ctx , "golang.Rename" )
425415 defer done ()
426416
@@ -461,10 +451,11 @@ func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp pro
461451 newPkgPath PackagePath
462452 err error
463453 )
464- if newPkgDir , newPkgName , newPkgPath , err = checkPackageRename (snapshot .Options (), pkg , f , newName ); err != nil {
454+ moveSubpackages := snapshot .Options ().RenameMovesSubpackages
455+ if newPkgDir , newPkgName , newPkgPath , err = checkPackageRename (pkg , f , newName , moveSubpackages ); err != nil {
465456 return nil , err
466457 }
467- editMap , err = renamePackage (ctx , snapshot , f , newPkgName , newPkgPath , newPkgDir , renameSubpkgs )
458+ editMap , err = renamePackage (ctx , snapshot , f , newPkgName , newPkgPath , newPkgDir , moveSubpackages )
468459 if err != nil {
469460 return nil , err
470461 }
@@ -524,7 +515,7 @@ func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp pro
524515 }
525516 if inPackageName {
526517 oldDir := f .URI ().DirPath ()
527- if renameSubpkgs {
518+ if snapshot . Options (). RenameMovesSubpackages {
528519 // Update the last component of the file's enclosing directory.
529520 changes = append (changes , protocol .DocumentChangeRename (
530521 protocol .URIFromPath (oldDir ),
@@ -962,15 +953,15 @@ func renameExported(pkgs []*cache.Package, declPkgPath PackagePath, declObjPath
962953//
963954// f is the file originating the rename, and therefore f.URI().Dir() is the
964955// current package directory. newName, newPath, and newDir describe the renaming.
965- func renamePackage (ctx context.Context , s * cache.Snapshot , f file.Handle , newName PackageName , newPath PackagePath , newDir string , renameSubpkgs bool ) (map [protocol.DocumentURI ][]diff.Edit , error ) {
956+ func renamePackage (ctx context.Context , s * cache.Snapshot , f file.Handle , newName PackageName , newPath PackagePath , newDir string , moveSubpackages bool ) (map [protocol.DocumentURI ][]diff.Edit , error ) {
966957 // Rename the package decl and all imports.
967958 renamingEdits := make (map [protocol.DocumentURI ][]diff.Edit )
968- err := updatePackageDeclsAndImports (ctx , s , f , newName , newPath , renamingEdits , renameSubpkgs )
959+ err := updatePackageDeclsAndImports (ctx , s , f , newName , newPath , renamingEdits , moveSubpackages )
969960 if err != nil {
970961 return nil , err
971962 }
972963
973- err = updateModFiles (ctx , s , f .URI ().DirPath (), newDir , renamingEdits , renameSubpkgs )
964+ err = updateModFiles (ctx , s , f .URI ().DirPath (), newDir , renamingEdits , moveSubpackages )
974965 if err != nil {
975966 return nil , err
976967 }
@@ -981,7 +972,7 @@ func renamePackage(ctx context.Context, s *cache.Snapshot, f file.Handle, newNam
981972// Update any affected replace directives in go.mod files.
982973// TODO(adonovan): should this operate on all go.mod files,
983974// irrespective of whether they are included in the workspace?
984- func updateModFiles (ctx context.Context , s * cache.Snapshot , oldDir string , newPkgDir string , renamingEdits map [protocol.DocumentURI ][]diff.Edit , renameSubpkgs bool ) error {
975+ func updateModFiles (ctx context.Context , s * cache.Snapshot , oldDir string , newPkgDir string , renamingEdits map [protocol.DocumentURI ][]diff.Edit , moveSubpackages bool ) error {
985976 modFiles := s .View ().ModFiles ()
986977 for _ , m := range modFiles {
987978 fh , err := s .ReadFile (ctx , m )
@@ -1008,8 +999,8 @@ func updateModFiles(ctx context.Context, s *cache.Snapshot, oldDir string, newPk
1008999
10091000 // TODO: Is there a risk of converting a '\' delimited replacement to a '/' delimited replacement?
10101001
1011- if renameSubpkgs && ! strings .HasPrefix (filepath .ToSlash (replacedDir )+ "/" , filepath .ToSlash (oldDir )+ "/" ) ||
1012- ! renameSubpkgs && ! (filepath .ToSlash (replacedDir ) == filepath .ToSlash (oldDir )) {
1002+ if moveSubpackages && ! strings .HasPrefix (filepath .ToSlash (replacedDir )+ "/" , filepath .ToSlash (oldDir )+ "/" ) ||
1003+ ! moveSubpackages && ! (filepath .ToSlash (replacedDir ) == filepath .ToSlash (oldDir )) {
10131004 continue //not affected by the package renanming
10141005 }
10151006
@@ -1068,7 +1059,7 @@ func updateModFiles(ctx context.Context, s *cache.Snapshot, oldDir string, newPk
10681059// It updates package clauses and import paths for the renamed package as well
10691060// as any other packages affected by the directory renaming among all packages
10701061// known to the snapshot.
1071- func updatePackageDeclsAndImports (ctx context.Context , s * cache.Snapshot , f file.Handle , newName PackageName , newPkgPath PackagePath , renamingEdits map [protocol.DocumentURI ][]diff.Edit , renameSubpkgs bool ) error {
1062+ func updatePackageDeclsAndImports (ctx context.Context , s * cache.Snapshot , f file.Handle , newName PackageName , newPkgPath PackagePath , renamingEdits map [protocol.DocumentURI ][]diff.Edit , moveSubpackages bool ) error {
10721063 if strings .HasSuffix (string (newName ), "_test" ) {
10731064 return fmt .Errorf ("cannot rename to _test package" )
10741065 }
@@ -1112,8 +1103,8 @@ func updatePackageDeclsAndImports(ctx context.Context, s *cache.Snapshot, f file
11121103 // Subtle: check this condition before checking for valid module info
11131104 // below, because we should not fail this operation if unrelated packages
11141105 // lack module info.
1115- if renameSubpkgs && ! pathutil .InDir (string (oldPkgPath ), string (mp .PkgPath )) ||
1116- ! renameSubpkgs && mp .PkgPath != oldPkgPath {
1106+ if moveSubpackages && ! pathutil .InDir (filepath . FromSlash ( string (oldPkgPath )), filepath . FromSlash ( string (mp .PkgPath ) )) ||
1107+ ! moveSubpackages && mp .PkgPath != oldPkgPath {
11171108 continue // not affected by the package renaming
11181109 }
11191110
@@ -1138,7 +1129,6 @@ func updatePackageDeclsAndImports(ctx context.Context, s *cache.Snapshot, f file
11381129 return err
11391130 }
11401131 }
1141-
11421132 imp := ImportPath (newImportPath ) // TODO(adonovan): what if newImportPath has vendor/ prefix?
11431133 if err := renameImports (ctx , s , mp , imp , pkgName , renamingEdits ); err != nil {
11441134 return err
0 commit comments