Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scalafmt >= v0.7.0-RC1 #124

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ScalaFmtStep {
// prevent direct instantiation
private ScalaFmtStep() {}

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

Expand Down Expand Up @@ -92,11 +92,28 @@ FormatterFunc createFormat() throws Exception {

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

String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
Object either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
Object either;

try {
// scalafmt >= v0.7.0-RC1
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);

String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
either = invokeNoArg(configured, "toEither");
} catch (NoSuchMethodException e) {
// In case of a NoSuchMethodException try old configuration API
// scalafmt <= v0.6.8
Method fromHocon = configCls.getMethod("fromHocon", String.class, optionCls);
Object fromHoconEmptyPath = configCls.getMethod("fromHocon$default$2").invoke(null);

String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
}

config = invokeNoArg(invokeNoArg(either, "right"), "get");
}
return input -> {
Expand Down
3 changes: 2 additions & 1 deletion plugin-gradle/src/test/resources/scala/scalafmt/basic.clean
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ object a
def foo[
T: Int#Double#Triple,
R <% String](
@annot1 x: Int @annot2 =
@annot1
x: Int @annot2 =
2,
y: Int = 3)
: Int = {
Expand Down
3 changes: 2 additions & 1 deletion testlib/src/test/resources/scala/scalafmt/basic.clean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
x + y
})
object a extends b with c {
def foo[T: Int#Double#Triple, R <% String](@annot1 x: Int @annot2 = 2,
def foo[T: Int#Double#Triple, R <% String](@annot1
x: Int @annot2 = 2,
y: Int = 3): Int = {
"match" match {
case 1 | 2 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ object a
def foo[
T: Int#Double#Triple,
R <% String](
@annot1 x: Int @annot2 =
@annot1
x: Int @annot2 =
2,
y: Int = 3)
: Int = {
Expand Down