-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1085 from vovapolu/master
New scalafmt sbt plugin
- Loading branch information
Showing
34 changed files
with
471 additions
and
21 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
25 changes: 25 additions & 0 deletions
25
scalafmt-cli-sbt/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.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,25 @@ | ||
package org.scalafmt.sbt | ||
import scala.meta.internal.tokenizers.PlatformTokenizerCache | ||
import sbt._ | ||
import Keys._ | ||
|
||
object ScalafmtPlugin extends AutoPlugin { | ||
override def trigger: PluginTrigger = allRequirements | ||
object autoImport { | ||
val scalafmt: Command = | ||
Command.args("scalafmt", "run the scalafmt command line interface.") { | ||
case (state, args) => | ||
org.scalafmt.cli.Cli.main("--non-interactive" +: args.toArray) | ||
PlatformTokenizerCache.megaCache.clear() | ||
state | ||
} | ||
} | ||
override def globalSettings: Seq[Def.Setting[_]] = | ||
Seq( | ||
commands += autoImport.scalafmt | ||
) ++ | ||
addCommandAlias("scalafmtTest", "scalafmt --test") ++ | ||
addCommandAlias("scalafmtDiffTest", "scalafmt --diff --test") ++ | ||
addCommandAlias("scalafmtDiff", "scalafmt --diff") | ||
|
||
} |
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt-cli/p3/src/main/java/TestJava.java
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,6 @@ | ||
public class TestJava | ||
{ | ||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
} | ||
} |
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt-cli/p3/src/test/java/MainTestJava.java
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,6 @@ | ||
public class MainTestJava | ||
{ | ||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt-cli/project/plugins.sbt
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,3 @@ | ||
addSbtPlugin ( | ||
"com.geirsson" % "sbt-cli-scalafmt" % System.getProperty("plugin.version")) | ||
|
File renamed without changes.
1 change: 1 addition & 0 deletions
1
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/.scalafmt.conf
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 @@ | ||
style = IntelliJ |
4 changes: 4 additions & 0 deletions
4
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/.scalafmt6.conf
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,4 @@ | ||
style = default | ||
project.excludeFilters = [ | ||
MainTest | ||
] |
148 changes: 148 additions & 0 deletions
148
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/build.sbt
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,148 @@ | ||
import java.io.File | ||
|
||
fork in ThisBuild := true | ||
|
||
lazy val p123 = project | ||
.in(file(".")) | ||
.aggregate( | ||
p1, | ||
p2, | ||
p3 | ||
) | ||
|
||
lazy val p1 = project.settings( | ||
scalaVersion := "2.10.5" | ||
) | ||
lazy val p2 = project.settings( | ||
scalaVersion := "2.11.8" | ||
) | ||
lazy val p3 = project.settings( | ||
scalaVersion := "2.12.1" | ||
) | ||
lazy val p4 = project.settings( | ||
scalaVersion := "2.12.1" | ||
) | ||
lazy val p5 = project.settings( | ||
scalaVersion := "2.12.1", | ||
scalafmtOnCompile := true | ||
) | ||
lazy val p6 = project.settings( | ||
scalaVersion := "2.12.1", | ||
scalafmtConfig := Some(file(".scalafmt6.conf")) | ||
) | ||
lazy val p7 = project.settings( | ||
scalaVersion := "2.12.1", | ||
scalafmtConfig := None | ||
) | ||
|
||
def assertContentsEqual(file: File, expected: String): Unit = { | ||
val obtained = | ||
scala.io.Source.fromFile(file).getLines().mkString("\n") | ||
|
||
if (obtained.trim != expected.trim) { | ||
val msg = | ||
s"""File: $file | ||
|Obtained output: | ||
|$obtained | ||
|Expected: | ||
|$expected | ||
|""".stripMargin | ||
System.err.println(msg) | ||
throw new Exception(msg) | ||
} | ||
} | ||
|
||
TaskKey[Unit]("check") := { | ||
(1 to 4).foreach { i => | ||
val expectedTest = | ||
""" | ||
|object Test { | ||
| foo( | ||
| a, // comment | ||
| b | ||
| ) | ||
|} | ||
""".stripMargin | ||
val expectedMainTest = expectedTest.replaceFirst("Test", "MainTest") | ||
assertContentsEqual( | ||
file(s"p$i/src/main/scala/Test.scala"), | ||
expectedTest | ||
) | ||
assertContentsEqual( | ||
file(s"p$i/src/test/scala/MainTest.scala"), | ||
expectedMainTest | ||
) | ||
} | ||
|
||
|
||
assertContentsEqual( | ||
file(s"p5/src/main/scala/Test.scala"), | ||
""" | ||
|object Test { | ||
| def foo(a: Int, // comment | ||
| b: Double) = ??? | ||
|} | ||
""".stripMargin | ||
) | ||
assertContentsEqual( | ||
file(s"p5/src/test/scala/MainTest.scala"), | ||
""" | ||
|object MainTest { | ||
| def foo(a: Int, // comment | ||
| b: Double) = ??? | ||
|} | ||
""".stripMargin | ||
) | ||
|
||
|
||
assertContentsEqual( | ||
file(s"p6/src/main/scala/Test.scala"), | ||
""" | ||
|object Test { | ||
| foo(a, // comment | ||
| b) | ||
|} | ||
""".stripMargin | ||
) | ||
assertContentsEqual( | ||
file(s"p6/src/test/scala/MainTest.scala"), | ||
""" | ||
|object | ||
|MainTest | ||
|{ | ||
| foo(a, // comment | ||
| b) | ||
|} | ||
| | ||
""".stripMargin | ||
) | ||
|
||
|
||
assertContentsEqual( | ||
file(s"p7/src/main/scala/Test.scala"), | ||
""" | ||
|object Test { | ||
| foo(a, // comment | ||
| b) | ||
|} | ||
""".stripMargin | ||
) | ||
assertContentsEqual( | ||
file(s"p7/src/test/scala/MainTest.scala"), | ||
""" | ||
|object MainTest { | ||
| foo(a, // comment | ||
| b) | ||
|} | ||
""".stripMargin | ||
) | ||
|
||
assertContentsEqual( | ||
file("project/plugins.sbt"), | ||
""" | ||
|addSbtPlugin( | ||
| "com.geirsson" % "sbt-scalafmt" % System.getProperty("plugin.version") | ||
|) | ||
""".stripMargin | ||
) | ||
} |
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 @@ | ||
../sbt-cli/p1 |
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 @@ | ||
../sbt-cli/p2 |
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 @@ | ||
../sbt-cli/p3 |
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,4 @@ | ||
lazy val settings = | ||
Seq( | ||
scalaVersion := | ||
"2.12.1") |
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 @@ | ||
../p3/src |
6 changes: 6 additions & 0 deletions
6
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/p5/src/main/scala/Test.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,6 @@ | ||
object | ||
Test | ||
{ | ||
def foo(a: Int, // comment | ||
b: Double) = ??? | ||
} |
6 changes: 6 additions & 0 deletions
6
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/p5/src/test/scala/MainTest.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,6 @@ | ||
object | ||
MainTest | ||
{ | ||
def foo(a: Int, // comment | ||
b: Double) = ??? | ||
} |
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 @@ | ||
../p3/src |
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 @@ | ||
../p3/src |
1 change: 1 addition & 0 deletions
1
scalafmt-sbt-tests/src/sbt-test/scalafmt-sbt/sbt/project/build.properties
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 @@ | ||
sbt.version=1.0.0 |
File renamed without changes.
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,31 @@ | ||
-> p123/compile:scalafmtCheck | ||
-> p123/test:scalafmtCheck | ||
> p123/compile:scalafmt | ||
> p123/test:scalafmt | ||
> p123/compile:scalafmtCheck | ||
> p123/test:scalafmtCheck | ||
|
||
> scalafmtSbt | ||
> scalafmtSbtCheck | ||
|
||
> p4/scalafmtOnly src/main/scala/Test.scala src/test/scala/MainTest.scala p4.sbt | ||
> p4/compile:scalafmtCheck | ||
> p4/test:scalafmtCheck | ||
> p4/scalafmtSbtCheck | ||
|
||
> p5/compile | ||
> p5/test:compile | ||
> p5/compile:scalafmtCheck | ||
> p5/test:scalafmtCheck | ||
|
||
> p6/compile:scalafmt | ||
> p6/test:scalafmt | ||
> p6/compile:scalafmtCheck | ||
> p6/test:scalafmtCheck | ||
|
||
> p7/compile:scalafmt | ||
> p7/test:scalafmt | ||
> p7/compile:scalafmtCheck | ||
> p7/test:scalafmtCheck | ||
|
||
> check |
Oops, something went wrong.