diff options
Diffstat (limited to 'plugins/scalafmt/Scalafmt.scala')
-rw-r--r-- | plugins/scalafmt/Scalafmt.scala | 51 |
1 files changed, 40 insertions, 11 deletions
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 + } + } + } |