1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package cbt
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Files._
import scalariform.formatter.ScalaFormatter
import scalariform.formatter.preferences.FormattingPreferences
import scalariform.parser.ScalaParserException
trait Scalariform extends BaseBuild {
final def scalariformFormat: ExitCode = {
Scalariform.format(sourceFiles, scalariformPreferences, scalaVersion)
ExitCode.Success
}
def scalariformPreferences: FormattingPreferences = Scalariform.defaultPreferences
}
object Scalariform {
val defaultPreferences: FormattingPreferences = {
import scalariform.formatter.preferences._
FormattingPreferences()
.setPreference(AlignParameters, true)
.setPreference(AlignArguments, true)
.setPreference(AlignSingleLineCaseStatements, true)
.setPreference(MultilineScaladocCommentsStartOnFirstLine, true)
.setPreference(SpaceInsideParentheses, true)
.setPreference(SpacesWithinPatternBinders, true)
.setPreference(SpacesAroundMultiImports, true)
.setPreference(DoubleIndentClassDeclaration, false)
}
private val scalaFileMatcher = FileSystems.getDefault.getPathMatcher("glob:**.scala")
def format(files: Seq[File], preferences: FormattingPreferences, scalaVersion: String): Unit = {
var reformattedCount: Int = 0
for (file <- files if file.exists) {
val path = file.toPath
if(scalaFileMatcher.matches(path)) {
try {
val sourceCode = new String(readAllBytes(path))
val formatted = ScalaFormatter.format(
sourceCode,
preferences,
Some(scalaVersion)
)
if (sourceCode != formatted) {
write(path, formatted.getBytes)
reformattedCount += 1
}
} catch {
case e: ScalaParserException => System.err.println(s"Scalariform parser error: ${e.getMessage} when formatting: $file")
}
}
}
if (reformattedCount > 0) System.err.println(s"Formatted $reformattedCount Scala sources")
}
}
|