-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for setting scala version for scalafmt, and also convert …
- Loading branch information
Showing
18 changed files
with
122 additions
and
257 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
/* | ||
* 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 ScalafmtConfig config; | ||
|
||
public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception { | ||
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(); | ||
} | ||
} | ||
|
||
@Override | ||
public String apply(String input) { | ||
return Scalafmt.format(input, config, Set$.MODULE$.empty()).get(); | ||
} | ||
} |
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
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
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.
Oops, something went wrong.