Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement package auto adding in newly created Scala files
PackageProvider was introduced and can be used to implement autocompletion and package rename after file move. The set of created files was introduced because we cannot apply the workspace edit before the file is open.
- Loading branch information
Tomasz Dudzik
committed
Aug 12, 2019
1 parent
afdcd37
commit db4cbce
Showing
5 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
metals/src/main/scala/scala/meta/internal/metals/PackageProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package scala.meta.internal.metals | ||
|
||
import org.eclipse.lsp4j.Position | ||
import org.eclipse.lsp4j.Range | ||
import org.eclipse.lsp4j.TextEdit | ||
import org.eclipse.lsp4j.WorkspaceEdit | ||
|
||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.io.AbsolutePath | ||
|
||
class PackageProvider(private val buildTargets: BuildTargets) { | ||
|
||
def addPackageWorkspaceEdit(path: AbsolutePath): Option[WorkspaceEdit] = { | ||
def createWorkspaceEdit(packageName: String): WorkspaceEdit = { | ||
val textEdit = new TextEdit( | ||
new Range(new Position(0, 0), new Position(0, 0)), | ||
s"package $packageName\n\n" | ||
) | ||
|
||
val textEdits = List(textEdit).asJava | ||
val changes = Map(path.toString -> textEdits).asJava | ||
new WorkspaceEdit(changes) | ||
} | ||
|
||
createPackageName(path).map(createWorkspaceEdit) | ||
} | ||
|
||
private def createPackageName(path: AbsolutePath): Option[String] = { | ||
if (path.isScala && path.toFile.length() == 0) { | ||
val sourceItem = buildTargets.inverseSourceItem(path) | ||
val relativeDirectory = sourceItem | ||
.map(path.toRelative) | ||
.flatMap(relativePath => Option(relativePath.toNIO.getParent)) | ||
.map(_.toString) | ||
|
||
relativeDirectory.map(_.replace("/", ".")) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
tests/unit/src/test/scala/tests/AddPackageSlowSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package tests | ||
|
||
import java.nio.file.Files | ||
|
||
import scala.meta.internal.metals.RecursivelyDelete | ||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
|
||
object AddPackageSlowSuite extends BaseSlowSuite("add-package") { | ||
|
||
testAsync("single-level") { | ||
cleanCompileCache("a") | ||
RecursivelyDelete(workspace.resolve("a")) | ||
Files.createDirectories( | ||
workspace.resolve("a/src/main/scala/a").toNIO | ||
) | ||
for { | ||
_ <- server.initialize(""" | ||
|/metals.json | ||
|{ | ||
| "a": { } | ||
|} | ||
""".stripMargin) | ||
_ = workspace | ||
.resolve("a/src/main/scala/a/Main.scala") | ||
.toFile | ||
.createNewFile() | ||
_ <- server.didOpen("a/src/main/scala/a/Main.scala") | ||
_ = assertNoDiff( | ||
workspace.resolve("a/src/main/scala/a/Main.scala").readText, | ||
""" | ||
|package a | ||
""".stripMargin | ||
) | ||
} yield () | ||
} | ||
|
||
testAsync("multilevel") { | ||
cleanCompileCache("a") | ||
RecursivelyDelete(workspace.resolve("a")) | ||
Files.createDirectories( | ||
workspace.resolve("a/src/main/scala/a/b/c").toNIO | ||
) | ||
for { | ||
_ <- server.initialize(""" | ||
|/metals.json | ||
|{ | ||
| "a": { } | ||
|} | ||
""".stripMargin) | ||
_ = workspace | ||
.resolve("a/src/main/scala/a/b/c/Main.scala") | ||
.toFile | ||
.createNewFile() | ||
_ <- server.didOpen("a/src/main/scala/a/b/c/Main.scala") | ||
_ = assertNoDiff( | ||
workspace.resolve("a/src/main/scala/a/b/c/Main.scala").readText, | ||
""" | ||
|package a.b.c | ||
""".stripMargin | ||
) | ||
} yield () | ||
} | ||
|
||
testAsync("no-package") { | ||
cleanCompileCache("a") | ||
RecursivelyDelete(workspace.resolve("a")) | ||
Files.createDirectories( | ||
workspace.resolve("a/src/main/scala").toNIO | ||
) | ||
for { | ||
_ <- server.initialize(""" | ||
|/metals.json | ||
|{ | ||
| "a": { } | ||
|} | ||
""".stripMargin) | ||
_ = workspace | ||
.resolve("a/src/main/scala/Main.scala") | ||
.toFile | ||
.createNewFile() | ||
_ <- server.didOpen("a/src/main/scala/Main.scala") | ||
_ = assertNoDiff( | ||
workspace.resolve("a/src/main/scala/Main.scala").readText, | ||
"" | ||
) | ||
} yield () | ||
} | ||
|
||
testAsync("java-file") { | ||
cleanCompileCache("a") | ||
RecursivelyDelete(workspace.resolve("a")) | ||
Files.createDirectories( | ||
workspace.resolve("a/src/main/java/a").toNIO | ||
) | ||
for { | ||
_ <- server.initialize(""" | ||
|/metals.json | ||
|{ | ||
| "a": { } | ||
|} | ||
""".stripMargin) | ||
_ = workspace | ||
.resolve("a/src/main/java/a/Main.java") | ||
.toFile | ||
.createNewFile() | ||
_ <- server.didOpen("a/src/main/java/a/Main.java") | ||
_ = assertNoDiff( | ||
workspace.resolve("a/src/main/java/a/Main.java").readText, | ||
"" | ||
) | ||
} yield () | ||
} | ||
} |