Skip to content

Commit

Permalink
Merge pull request #866 from tgodzik/add-package-object
Browse files Browse the repository at this point in the history
Automatically add package objects when creating package.scala file
  • Loading branch information
tgodzik authored Aug 14, 2019
2 parents 98d4302 + 22ddb41 commit 656ed8c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,43 @@ import org.eclipse.lsp4j.WorkspaceEdit

import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath
import java.nio.file.Path

class PackageProvider(private val buildTargets: BuildTargets) {

def workspaceEdit(path: AbsolutePath): Option[WorkspaceEdit] = {
packageName(path).map(workspaceEdit(path, _))
packageStatement(path).map(workspaceEdit(path, _))
}

private def packageName(path: AbsolutePath): Option[String] = {
// package.scala file needs to be handled separately
// as its package name is different than package name of normal scala file
if (path.isScala &&
path.toFile.length() == 0 &&
path.filename != "package.scala") {
val sourceItem = buildTargets.inverseSourceItem(path)
sourceItem
private def packageStatement(path: AbsolutePath): Option[String] = {

def packageObjectStatement(path: Iterator[Path]): Option[String] = {
val pathList = path.toList
val packageDeclaration =
if (pathList.size > 1)
s"package ${pathList.dropRight(1).mkString(".")}\n\n"
else ""
pathList.lastOption.map { packageObjectName =>
s"""|${packageDeclaration}package object $packageObjectName {
|
|}
|""".stripMargin
}
}

if (path.isScala && path.toFile.length() == 0) {
buildTargets
.inverseSourceItem(path)
.map(path.toRelative)
.flatMap(relativePath => Option(relativePath.toNIO.getParent))
.map { parent =>
parent.iterator().asScala.mkString(".")
.flatMap { parent =>
val pathIterator = parent.iterator().asScala
if (path.filename == "package.scala") {
packageObjectStatement(pathIterator)
} else {
val packageName = parent.iterator().asScala.mkString(".")
Some(s"package $packageName\n\n")
}
}
} else {
None
Expand All @@ -34,13 +52,12 @@ class PackageProvider(private val buildTargets: BuildTargets) {

private def workspaceEdit(
path: AbsolutePath,
packageName: String
packageStatement: String
): WorkspaceEdit = {
val textEdit = new TextEdit(
new Range(new Position(0, 0), new Position(0, 0)),
s"package $packageName\n\n"
packageStatement
)

val textEdits = List(textEdit).asJava
val changes = Map(path.toString -> textEdits).asJava
new WorkspaceEdit(changes)
Expand Down
36 changes: 35 additions & 1 deletion tests/unit/src/test/scala/tests/AddPackageSlowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,41 @@ object AddPackageSlowSuite extends BaseSlowSuite("add-package") {
_ <- server.didOpen("a/src/main/scala/a/package.scala")
_ = assertNoDiff(
workspace.resolve("a/src/main/scala/a/package.scala").readText,
""
"""|package object a {
|
|}
|""".stripMargin
)
} yield ()
}

testAsync("package-file-multi") {
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/package.scala")
.toFile
.createNewFile()
_ <- server.didOpen("a/src/main/scala/a/b/c/package.scala")
_ = assertNoDiff(
workspace.resolve("a/src/main/scala/a/b/c/package.scala").readText,
"""|package a.b
|
|package object c {
|
|}
|""".stripMargin
)
} yield ()
}
Expand Down

0 comments on commit 656ed8c

Please sign in to comment.