Skip to content

Commit

Permalink
bugfix: Don't change packages in files if they move outside the works…
Browse files Browse the repository at this point in the history
…pace

Connected to #4888
  • Loading branch information
tgodzik committed Feb 11, 2023
1 parent a24dcac commit 6952110
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2103,10 +2103,17 @@ class MetalsLspService(
): CompletableFuture[WorkspaceEdit] =
CancelTokens.future { _ =>
val moves = params.getFiles.asScala.toSeq.map { rename =>
packageProvider.willMovePath(
rename.getOldUri().toAbsolutePath,
rename.getNewUri().toAbsolutePath,
)
val oldPath = rename.getOldUri().toAbsolutePath
val newPath = rename.getNewUri().toAbsolutePath
/* Changing package for files moved out of the workspace will cause unexpected issues
* This showed up with emacs automated backups.
*/
if (newPath.startWith(workspace))
packageProvider.willMovePath(oldPath, newPath)
else
Future.successful(
new WorkspaceEdit(Map.empty[String, util.List[TextEdit]].asJava)
)
}
Future.sequence(moves).map(_.mergeChanges)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ trait CommonMtagsEnrichments {
None
}
}

def startWith(other: AbsolutePath): Boolean =
path.toNIO.startsWith(other.toNIO)
}

implicit class XtensionJavaPriorityQueue[A](q: util.PriorityQueue[A]) {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/src/test/scala/tests/RenameFilesLspSuite.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tests

import java.nio.file.Files

class RenameFilesLspSuite extends BaseRenameFilesLspSuite("rename_files") {
private val prefix = "a/src/main/scala"

Expand Down Expand Up @@ -34,6 +36,19 @@ class RenameFilesLspSuite extends BaseRenameFilesLspSuite("rename_files") {
expectedRenames = Map("A" -> "A.B"),
)

/* We should not rename if moving outside the workspace */
renamed(
"outside-workspace",
s"""|/$prefix/A/Sun.scala
|package A
|object Sun
|""".stripMargin,
fileRenames = Map(
s"$prefix/A/Sun.scala" -> Files.createTempFile("Sun", ".scala").toString()
),
expectedRenames = Map.empty,
)

renamed(
"single-package-non-matching-structure",
s"""|/$prefix/A/B/Sun.scala
Expand Down

0 comments on commit 6952110

Please sign in to comment.