Skip to content

Commit

Permalink
Massive speedup. FormatterFunc is instantiated lazily and is not invo…
Browse files Browse the repository at this point in the history
…lved in serialization/equality, so good to eagerly initialize in the constructor.
  • Loading branch information
nedtwigg committed Aug 23, 2022
1 parent 735c7b5 commit a75877c
Showing 1 changed file with 6 additions and 8 deletions.
Expand Up @@ -30,15 +30,9 @@
import scala.collection.immutable.Set$;

public class ScalafmtFormatterFunc implements FormatterFunc {
private final FileSignature configSignature;
private final ScalafmtConfig config;

public ScalafmtFormatterFunc(FileSignature configSignature) {
this.configSignature = configSignature;
}

@Override
public String apply(String input) throws Exception {
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
Expand All @@ -51,6 +45,10 @@ public String apply(String input) throws Exception {
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();
}
}

0 comments on commit a75877c

Please sign in to comment.