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

Added scalafmt 3 support #913

Merged
merged 4 commits into from
Aug 20, 2021
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changed
* Added support for [scalafmt 3.0.0](https://github.com/scalameta/scalafmt/releases/tag/v3.0.0) and bump default scalafmt version to `3.0.0` ([#913](https://github.com/diffplug/spotless/pull/913)).

## [2.15.2] - 2021-07-20
### Fixed
Expand Down
14 changes: 9 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 @@ -34,16 +34,18 @@
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

/** Wraps up [scalafmt](https://github.com/olafurpg/scalafmt) as a FormatterStep. */
/** Wraps up [scalafmt](https://github.com/scalameta/scalafmt) as a FormatterStep. */
public class ScalaFmtStep {
// prevent direct instantiation
private ScalaFmtStep() {}

private static final Pattern VERSION_PRE_2_0 = Pattern.compile("[10]\\.(\\d+)\\.\\d+");
private static final String DEFAULT_VERSION = "2.0.1";
private static final Pattern VERSION_PRE_3_0 = Pattern.compile("2\\.(\\d+)\\.\\d+");
private static final String DEFAULT_VERSION = "3.0.0";
static final String NAME = "scalafmt";
static final String MAVEN_COORDINATE_PRE_2_0 = "com.geirsson:scalafmt-core_2.11:";
static final String MAVEN_COORDINATE = "org.scalameta:scalafmt-core_2.11:";
static final String MAVEN_COORDINATE_PRE_3_0 = "org.scalameta:scalafmt-core_2.11:";
static final String MAVEN_COORDINATE = "org.scalameta:scalafmt-core_2.13:";

public static FormatterStep create(Provisioner provisioner) {
return create(defaultVersion(), provisioner, null);
Expand All @@ -69,9 +71,11 @@ static final class State implements Serializable {

State(String version, Provisioner provisioner, @Nullable File configFile) throws IOException {
String mavenCoordinate;
Matcher versionMatcher = VERSION_PRE_2_0.matcher(version);
if (versionMatcher.matches()) {
Matcher versionMatcher;
if ((versionMatcher = VERSION_PRE_2_0.matcher(version)).matches()) {
mavenCoordinate = MAVEN_COORDINATE_PRE_2_0;
} else if ((versionMatcher = VERSION_PRE_3_0.matcher(version)).matches()) {
mavenCoordinate = MAVEN_COORDINATE_PRE_3_0;
} else {
mavenCoordinate = MAVEN_COORDINATE;
}
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Changed
* Added support for [scalafmt 3.0.0](https://github.com/scalameta/scalafmt/releases/tag/v3.0.0) and bump default scalafmt version to `3.0.0` ([#913](https://github.com/diffplug/spotless/pull/913)).

## [5.14.2] - 2021-07-20
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,6 @@ public void integration() throws IOException {
setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf");
setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}
}
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changed
* Added support for [scalafmt 3.0.0](https://github.com/scalameta/scalafmt/releases/tag/v3.0.0) and bump default scalafmt version to `3.0.0` ([#913](https://github.com/diffplug/spotless/pull/913)).

## [2.12.2] - 2021-07-20
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ public class IncludesExcludesTest extends MavenIntegrationHarness {
private static final String JAVA_FORMATTED = "java/eclipse/JavaCodeFormatted.test";
private static final String JAVA_UNFORMATTED = "java/eclipse/JavaCodeUnformatted.test";
private static final String SCALA_UNFORMATTED = "scala/scalafmt/basic.dirty";
private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_2.0.1";
private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_3.0.0";

@Test
public void testDefaultIncludesJava() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,7 +93,7 @@ public void testConfigurationDependency() throws Exception {
assertFile("two/src/main/java/test1.java").sameAsResource("java/eclipse/JavaCodeFormatted.test");
assertFile("two/src/test/java/test2.java").sameAsResource("java/eclipse/JavaCodeFormatted.test");

assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ public class ScalafmtTest extends MavenIntegrationHarness {
public void testScalafmtWithDefaultConfig() throws Exception {
writePomWithScalaSteps("<scalafmt/>");

runTest("scala/scalafmt/basic.clean_2.0.1");
runTest("scala/scalafmt/basic.clean_3.0.0");
}

@Test
Expand All @@ -36,7 +36,7 @@ public void testScalafmtWithCustomConfig() throws Exception {
" <file>${project.basedir}/scalafmt.conf</file>",
"</scalafmt>");

runTest("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
runTest("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

private void runTest(String s) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a
extends b
with c {
def foo[
T: Int#Double#Triple,
R <% String
](
@annot1
x: Int @annot2 =
2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> =>
2
}
}
}
20 changes: 20 additions & 0 deletions testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a extends b with c {
def foo[T: Int#Double#Triple, R <% String](
@annot1
x: Int @annot2 = 2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> => 2
}
}
}
20 changes: 20 additions & 0 deletions testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a extends b with c {
def foo[T: Int#Double#Triple, R <% String](
@annot1
x: Int @annot2 = 2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> => 2
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a
extends b
with c {
def foo[
T: Int#Double#Triple,
R <% String
](
@annot1
x: Int @annot2 =
2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> =>
2
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void behaviorDefaultConfig() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_1.1.0");
StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), null))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_2.0.1");
StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_3.0.0");
}

@Test
Expand All @@ -47,6 +49,8 @@ public void behaviorCustomConfig() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_1.1.0");
StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

@Test
Expand All @@ -63,6 +67,20 @@ public void behaviorCustomConfigVersion_2_0_0() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost2.0.0.cleanWithCustomConf");
}

@Test
public void behaviorDefaultConfigVersion_3_0_0() throws Exception {
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);
StepHarness.forStep(step)
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean");
}

@Test
public void behaviorCustomConfigVersion_3_0_0() throws Exception {
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"));
StepHarness.forStep(step)
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf");
}

@Test
public void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down Expand Up @@ -106,5 +124,9 @@ public void invalidConfiguration() throws Exception {
exception = assertThrows(InvocationTargetException.class,
() -> StepHarness.forStep(ScalaFmtStep.create("2.0.1", provisioner, invalidConfFile)).test("", ""));
assertThat(exception.getTargetException().getMessage(), containsString("Invalid field: invalidScalaFmtConfigField"));

exception = assertThrows(InvocationTargetException.class,
() -> StepHarness.forStep(ScalaFmtStep.create("3.0.0", provisioner, invalidConfFile)).test("", ""));
assertThat(exception.getTargetException().getMessage(), containsString("found option 'invalidScalaFmtConfigField' which wasn't expected"));
}
}