Skip to content

Commit 47c06a8

Browse files
committed
ScalaFmt takes filepath into consideration.
fix formatting.
1 parent 04d5f28 commit 47c06a8

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,4 +51,9 @@ public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception {
5151
public String apply(String input) {
5252
return Scalafmt.format(input, config, Set$.MODULE$.empty()).get();
5353
}
54+
55+
@Override
56+
public String apply(String input, File file) {
57+
return Scalafmt.format(input, config, Set$.MODULE$.empty(), file.getAbsolutePath()).get();
58+
}
5459
}

testlib/src/main/java/com/diffplug/spotless/StepHarness.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ public StepHarness testUnaffected(String idempotentElement) {
8080
public StepHarness testResource(String resourceBefore, String resourceAfter) {
8181
String before = ResourceHarness.getTestResource(resourceBefore);
8282
String after = ResourceHarness.getTestResource(resourceAfter);
83-
return test(before, after);
83+
String actual = formatter().compute(LineEnding.toUnix(before), new File(resourceBefore));
84+
assertEquals(after, actual, "Step application failed");
85+
actual = formatter().compute(LineEnding.toUnix(after), new File(resourceAfter));
86+
assertEquals(after, actual, "Step is not idempotent");
87+
return this;
8488
}
8589

8690
/** Asserts that the given elements in the resources directory are transformed as expected. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version = 3.7.3
2+
runner.dialect = scala213
3+
style = defaultWithAlign # For pretty alignment.
4+
maxColumn = 200 # For my mega-wide display
5+
fileOverride {
6+
"glob:**/scala/scalafmt/**" {
7+
maxColumn = 20 # For my teensy narrow display
8+
}
9+
}

testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,13 @@ void behaviorCustomConfig() {
4040
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
4141
}
4242

43+
@Test
44+
void behaviorFileOverride() {
45+
FormatterStep step = ScalaFmtStep.create(ScalaFmtStep.DEFAULT_VERSION, TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.fileoverride.conf"));
46+
StepHarness.forStep(step)
47+
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
48+
}
49+
4350
@Test
4451
void behaviorDefaultConfigVersion_3_0_0() {
4552
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);

0 commit comments

Comments
 (0)