diff options
author | Jan Christopher Vogt <oss.nsp@cvogt.org> | 2016-10-02 18:17:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-02 18:17:52 -0400 |
commit | 174c52b2c24b8491eef687ee5eb3c3b77c34a61c (patch) | |
tree | 8ecd0f087fe6e9abd377db49f6ddb564865947d6 | |
parent | e185113257ef7f38460f93fa1ccc93415b7055f9 (diff) | |
parent | aea470195de6cb8f0111e5908917d64889f3c753 (diff) | |
download | cbt-174c52b2c24b8491eef687ee5eb3c3b77c34a61c.tar.gz cbt-174c52b2c24b8491eef687ee5eb3c3b77c34a61c.tar.bz2 cbt-174c52b2c24b8491eef687ee5eb3c3b77c34a61c.zip |
Merge pull request #255 from rockjam/scalafmt-config
upgrade scalafmt 0.3.1 -> 0.4.2; load scalafmt config from config file
-rw-r--r-- | examples/scalafmt-example/.scalafmt.conf | 11 | ||||
-rw-r--r-- | examples/scalafmt-example/build/build.scala | 9 | ||||
-rw-r--r-- | examples/scalafmt-example/src/Main.scala | 12 | ||||
-rw-r--r-- | plugins/scalafmt/Scalafmt.scala | 51 | ||||
-rw-r--r-- | plugins/scalafmt/build/build.scala | 5 |
5 files changed, 66 insertions, 22 deletions
diff --git a/examples/scalafmt-example/.scalafmt.conf b/examples/scalafmt-example/.scalafmt.conf new file mode 100644 index 0000000..4fc0088 --- /dev/null +++ b/examples/scalafmt-example/.scalafmt.conf @@ -0,0 +1,11 @@ +style: defaultWithAlign +danglingParentheses: true + +spaces { + inImportCurlyBraces: true +} + +rewriteTokens { + "->": "→" + "=>": "⇒" +} diff --git a/examples/scalafmt-example/build/build.scala b/examples/scalafmt-example/build/build.scala index b8a819b..4f5545e 100644 --- a/examples/scalafmt-example/build/build.scala +++ b/examples/scalafmt-example/build/build.scala @@ -1,5 +1,4 @@ import cbt._ -import org.scalafmt.ScalafmtStyle class Build(val context: Context) extends BaseBuild with Scalafmt { override def compile = { @@ -7,8 +6,6 @@ class Build(val context: Context) extends BaseBuild with Scalafmt { super.compile } - override def scalafmtConfig: ScalafmtStyle = ScalafmtStyle.defaultWithAlign - def breakFormatting = { import java.nio.file._ import java.nio.charset.Charset @@ -17,7 +14,11 @@ class Build(val context: Context) extends BaseBuild with Scalafmt { sourceFiles foreach { file => val path = file.toPath val fileLines = Files.readAllLines(path, utf8).asScala - val brokenLines = fileLines map (_.dropWhile(_ == ' ')) + val brokenLines = fileLines map (l => + l.dropWhile(_ == ' ') + .replaceAll("⇒", "=>") + .replaceAll("→", "->") + ) Files.write(path, brokenLines.asJava, utf8) } System.err.println("Done breaking formatting") diff --git a/examples/scalafmt-example/src/Main.scala b/examples/scalafmt-example/src/Main.scala index 595cede..465e27a 100644 --- a/examples/scalafmt-example/src/Main.scala +++ b/examples/scalafmt-example/src/Main.scala @@ -1,16 +1,16 @@ -import scala.concurrent.{Await, Future} +import scala.concurrent.{ Await, Future } import scala.concurrent.duration._ object Main extends App { println("fooo") val futureRes = Await.result(Future.successful(1), 5.seconds) List(1, 2, 4, 5, 6) match { - case h :: _ => println("not empty list") - case Nil => println("empty list") + case h :: _ ⇒ println("not empty list") + case Nil ⇒ println("empty list") } - List(1 -> 2, 2 -> 3, 3 -> 4) match { - case (1, 2) :: _ => 90 -> 1 - case (22, 44) :: _ => 1 -> 150 + List(1 → 2, 2 → 3, 3 → 4) match { + case (1, 2) :: _ ⇒ 90 → 1 + case (22, 44) :: _ ⇒ 1 → 150 } } diff --git a/plugins/scalafmt/Scalafmt.scala b/plugins/scalafmt/Scalafmt.scala index a1c7a0d..1f8bf2d 100644 --- a/plugins/scalafmt/Scalafmt.scala +++ b/plugins/scalafmt/Scalafmt.scala @@ -1,11 +1,12 @@ package cbt import org.scalafmt.Error.Incomplete -import org.scalafmt.{FormatResult, ScalafmtStyle} - +import org.scalafmt.Formatted +import org.scalafmt.cli.StyleCache +import org.scalafmt.config.ScalafmtConfig import java.io.File import java.nio.file.Files._ -import java.nio.file.{FileSystems, Path} +import java.nio.file.{ FileSystems, Path, Paths } /** * This plugin provides scalafmt support for cbt. @@ -23,28 +24,49 @@ trait Scalafmt extends BaseBuild { } /** - * Scalafmt formatting config + * Scalafmt formatting config. + * + * Tries to get style in following order: + * • project local .scalafmt.conf + * • global ~/.scalafmt.conf + * • default scalafmt config + * + * Override this task if you want to provide + * scalafmt config programmatically on your own. */ - def scalafmtConfig: ScalafmtStyle = Scalafmt.defaultConfig + def scalafmtConfig: ScalafmtConfig = + Scalafmt.getStyle( + project = projectDirectory.toPath, + home = Option(System.getProperty("user.home")) map (p => Paths.get(p)) + ) } object Scalafmt { - val defaultConfig = ScalafmtStyle.default + def getStyle(project: Path, home: Option[Path]): ScalafmtConfig = { + val local = getConfigPath(project) + val global = home flatMap getConfigPath + val customStyle = for { + configPath <- local.orElse(global) + style <- StyleCache.getStyleForFile(configPath.toString) + } yield style + + customStyle.getOrElse(ScalafmtConfig.default) + } - def format(files: Seq[File], style: ScalafmtStyle): Unit = { + def format(files: Seq[File], style: ScalafmtConfig): Unit = { var reformattedCount: Int = 0 scalaSourceFiles(files) foreach { path => handleFormatted(path, style) { case (original, result) => result match { - case FormatResult.Success(formatted) => + case Formatted.Success(formatted) => if (original != formatted) { write(path, formatted.getBytes) reformattedCount += 1 } - case FormatResult.Failure(e: Incomplete) => + case Formatted.Failure(e: Incomplete) => System.err.println(s"Couldn't complete file reformat: $path") - case FormatResult.Failure(e) => + case Formatted.Failure(e) => System.err.println(s"Failed to format file: $path, cause: ${e}") } } @@ -61,10 +83,17 @@ object Scalafmt { } } - private def handleFormatted[T](path: Path, style: ScalafmtStyle)(handler: (String, FormatResult) => T): T = { + private def handleFormatted[T](path: Path, style: ScalafmtConfig)(handler: (String, Formatted) => T): T = { val original = new String(readAllBytes(path)) val result = org.scalafmt.Scalafmt.format(original, style) handler(original, result) } + private def getConfigPath(base: Path): Option[Path] = { + val location = base.resolve(".scalafmt.conf").toFile + Option(location.exists && location.isFile) collect { + case true => location.toPath.toAbsolutePath + } + } + } diff --git a/plugins/scalafmt/build/build.scala b/plugins/scalafmt/build/build.scala index 0d4900c..2631908 100644 --- a/plugins/scalafmt/build/build.scala +++ b/plugins/scalafmt/build/build.scala @@ -1,9 +1,12 @@ import cbt._ class Build(val context: Context) extends Plugin { + private val ScalafmtVersion = "0.4.2" + override def dependencies = super.dependencies ++ Resolver( mavenCentral ).bind( - ScalaDependency("com.geirsson", "scalafmt", "0.3.1") + ScalaDependency("com.geirsson", "scalafmt", ScalafmtVersion), + ScalaDependency("com.geirsson", "scalafmt-cli", ScalafmtVersion) ) } |