-
Notifications
You must be signed in to change notification settings - Fork 472
Convert scalafmt integration to use a compile-only sourceset #1283
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
Changes from all commits
781eb68
137392f
ecece44
abe0be9
df59712
677415e
30d72d8
872424a
24ca9ec
735c7b5
a75877c
e9d3150
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way around this may be that instead of defining There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be okay putting Scala into our codebase if it was really useful for some task, but this is small enough that I'd prefer the reflection. Note the little refactor I did in a75877c. |
||
// 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(); | ||
nedtwigg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.