Skip to content

Commit 519e442

Browse files
alexvetternedtwigg
authored andcommitted
add support for scalafmt >= v0.7.0-RC1
1 parent e05e753 commit 519e442

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ScalaFmtStep {
3737
// prevent direct instantiation
3838
private ScalaFmtStep() {}
3939

40-
private static final String DEFAULT_VERSION = "0.5.7";
40+
private static final String DEFAULT_VERSION = "1.1.0";
4141
static final String NAME = "scalafmt";
4242
static final String MAVEN_COORDINATE = "com.geirsson:scalafmt-core_2.11:";
4343

@@ -92,11 +92,28 @@ FormatterFunc createFormat() throws Exception {
9292

9393
Class<?> optionCls = classLoader.loadClass("scala.Option");
9494
Class<?> configCls = classLoader.loadClass("org.scalafmt.config.Config");
95-
Method fromHocon = configCls.getMethod("fromHocon", String.class, optionCls);
96-
Object fromHoconEmptyPath = configCls.getMethod("fromHocon$default$2").invoke(null);
9795

98-
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
99-
Object either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
96+
Object either;
97+
98+
try {
99+
// scalafmt >= v0.7.0-RC1
100+
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
101+
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);
102+
103+
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
104+
105+
Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
106+
either = invokeNoArg(configured, "toEither");
107+
} catch (NoSuchMethodException e) {
108+
// In case of a NoSuchMethodException try old configuration API
109+
// scalafmt <= v0.6.8
110+
Method fromHocon = configCls.getMethod("fromHocon", String.class, optionCls);
111+
Object fromHoconEmptyPath = configCls.getMethod("fromHocon$default$2").invoke(null);
112+
113+
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
114+
either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
115+
}
116+
100117
config = invokeNoArg(invokeNoArg(either, "right"), "get");
101118
}
102119
return input -> {

plugin-gradle/src/test/resources/scala/scalafmt/basic.clean

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ object a
99
def foo[
1010
T: Int#Double#Triple,
1111
R <% String](
12-
@annot1 x: Int @annot2 =
12+
@annot1
13+
x: Int @annot2 =
1314
2,
1415
y: Int = 3)
1516
: Int = {

testlib/src/test/resources/scala/scalafmt/basic.clean

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
x + y
55
})
66
object a extends b with c {
7-
def foo[T: Int#Double#Triple, R <% String](@annot1 x: Int @annot2 = 2,
7+
def foo[T: Int#Double#Triple, R <% String](@annot1
8+
x: Int @annot2 = 2,
89
y: Int = 3): Int = {
910
"match" match {
1011
case 1 | 2 =>

testlib/src/test/resources/scala/scalafmt/basic.cleanWithCustomConf

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ object a
99
def foo[
1010
T: Int#Double#Triple,
1111
R <% String](
12-
@annot1 x: Int @annot2 =
12+
@annot1
13+
x: Int @annot2 =
1314
2,
1415
y: Int = 3)
1516
: Int = {

0 commit comments

Comments
 (0)