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

ScalaFmt takes filepath into consideration. #1854

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-2024 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 @@ -51,4 +51,9 @@ public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception {
public String apply(String input) {
return Scalafmt.format(input, config, Set$.MODULE$.empty()).get();
}

@Override
public String apply(String input, File file) {
return Scalafmt.format(input, config, Set$.MODULE$.empty(), file.getAbsolutePath()).get();
}
}
6 changes: 5 additions & 1 deletion testlib/src/main/java/com/diffplug/spotless/StepHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public StepHarness testUnaffected(String idempotentElement) {
public StepHarness testResource(String resourceBefore, String resourceAfter) {
String before = ResourceHarness.getTestResource(resourceBefore);
String after = ResourceHarness.getTestResource(resourceAfter);
return test(before, after);
String actual = formatter().compute(LineEnding.toUnix(before), new File(resourceBefore));
assertEquals(after, actual, "Step application failed");
actual = formatter().compute(LineEnding.toUnix(after), new File(resourceAfter));
assertEquals(after, actual, "Step is not idempotent");
return this;
}

/** Asserts that the given elements in the resources directory are transformed as expected. */
Expand Down
2 changes: 1 addition & 1 deletion testlib/src/main/resources/scala/scalafmt/basic.dirty
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ foobar("annot", {
@foobar ("annot", {
val x = 2
val y = 2 // y=2
x + y
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version = 3.7.3
runner.dialect = scala213
style = defaultWithAlign # For pretty alignment.
maxColumn = 200 # For my mega-wide display
fileOverride {
"glob:**/scala/scalafmt/**" {
maxColumn = 20 # For my teensy narrow display
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 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 @@ -40,6 +40,13 @@ void behaviorCustomConfig() {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

@Test
void behaviorFileOverride() {
FormatterStep step = ScalaFmtStep.create(ScalaFmtStep.DEFAULT_VERSION, TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.fileoverride.conf"));
StepHarness.forStep(step)
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

@Test
void behaviorDefaultConfigVersion_3_0_0() {
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);
Expand Down
Loading