-
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 781eb68
Showing
12 changed files
with
81 additions
and
247 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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
/* | ||
* Copyright 2022 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.scalafmt; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
import org.scalafmt.Scalafmt; | ||
import org.scalafmt.config.ScalafmtConfig; | ||
import org.scalafmt.config.ScalafmtConfig$; | ||
|
||
import com.diffplug.spotless.FileSignature; | ||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
import scala.collection.immutable.Set$; | ||
|
||
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.
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
version = 3.5.9 | ||
runner.dialect = scala213 | ||
style = defaultWithAlign # For pretty alignment. | ||
maxColumn = 20 # For my teensy narrow display |
Oops, something went wrong.