gopls/internal/golang: add condition for enabling package move

If the package name does not match its directory basename, we shouldn't
support renaming to move the package. Don't return the entire package
path in PrepareRename.

This is a follow-up to address https://go-review.git.corp.google.com/c/tools/+/708655/comment/408e97ee_31e00e80/

Change-Id: I74548153a2c4af7b417e132c7d88dd797871f5fe
Reviewed-on: https://go-review.googlesource.com/c/tools/+/710275
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/gopls/internal/golang/rename.go b/gopls/internal/golang/rename.go
index 0b1d750..137a113 100644
--- a/gopls/internal/golang/rename.go
+++ b/gopls/internal/golang/rename.go
@@ -215,9 +215,16 @@
 		return nil, err
 	}
 
-	text := string(meta.Name)
-	if snapshot.Options().PackageMove {
-		text = string(meta.PkgPath)
+	pkgName := string(meta.Name)
+	fullPath := string(meta.PkgPath)
+	text := pkgName
+	// Before displaying the full package path, verify that the PackageMove
+	// setting is enabled and that the package name matches its directory
+	// basename. Checking the value of meta.Module above ensures that the
+	// current view is either a GoMod or a GoWork view, which are the only views
+	// for which we should enable package move.
+	if snapshot.Options().PackageMove && path.Base(fullPath) == pkgName {
+		text = fullPath
 	}
 	return &PrepareItem{
 		Range: rng,
diff --git a/gopls/internal/test/marker/testdata/rename/prepare_move.txt b/gopls/internal/test/marker/testdata/rename/prepare_move.txt
index c41cffe..283f754 100644
--- a/gopls/internal/test/marker/testdata/rename/prepare_move.txt
+++ b/gopls/internal/test/marker/testdata/rename/prepare_move.txt
@@ -12,3 +12,6 @@
 
 -- b/b.go --
 package b //@ preparerename("b", "golang.org/lsptests/b")
+
+-- a/other.go --
+package other //@ preparerename("other", "other") // package move disabled when the package name does not match its directory base name