-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert scalafmt integration to use a compile-only sourceset
- Loading branch information
1 parent
b535dce
commit ea185c5
Showing
11 changed files
with
62 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.diffplug.spotless.glue.scalafmt; | ||
|
||
import com.diffplug.spotless.FileSignature; | ||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
import org.scalafmt.Scalafmt; | ||
import org.scalafmt.config.ScalafmtConfig; | ||
import org.scalafmt.config.ScalafmtConfig$; | ||
|
||
import scala.collection.immutable.Set$; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
public class ScalafmtFormatterFunc implements FormatterFunc { | ||
private final FileSignature configSignature; | ||
|
||
public ScalafmtFormatterFunc(FileSignature configSignature) { | ||
this.configSignature = configSignature; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
ScalafmtConfig config; | ||
if (configSignature.files().isEmpty()) { | ||
// Note that reflection is used here only because Scalafmt has a method called | ||
// default which happens to be a reserved Java keyword. The only way to call | ||
// such methods is by reflection, see | ||
// https://vlkan.com/blog/post/2015/11/20/scala-method-with-java-reserved-keyword/ | ||
Method method = ScalafmtConfig$.MODULE$.getClass().getDeclaredMethod("default"); | ||
config = (ScalafmtConfig) method.invoke(ScalafmtConfig$.MODULE$); | ||
} else { | ||
File file = configSignature.getOnlyFile(); | ||
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); | ||
config = Scalafmt.parseHoconConfig(configStr).get(); | ||
} | ||
return Scalafmt.format(input, config, Set$.MODULE$.empty()).get(); | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_1.1.0
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
testlib/src/main/resources/scala/scalafmt/basic.cleanWithCustomConf_2.0.1
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
testlib/src/main/resources/scala/scalafmt/basic.clean_1.1.0
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
testlib/src/main/resources/scala/scalafmt/basic.clean_2.0.1
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
testlib/src/main/resources/scala/scalafmt/basicPost2.0.0.clean
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
testlib/src/main/resources/scala/scalafmt/basicPost2.0.0.cleanWithCustomConf
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.