Skip to content

Commit

Permalink
Make KtfmtStep round-trippable
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 31, 2024
1 parent 4e10fd4 commit 6e6ceaa
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@

import static com.diffplug.spotless.kotlin.KtfmtStep.Style.DEFAULT;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -30,19 +29,38 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.RoundedStep;
import com.diffplug.spotless.ThrowingEx;

/**
* Wraps up <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> as a FormatterStep.
*/
public class KtfmtStep {
// prevent direct instantiation
private KtfmtStep() {}

public class KtfmtStep implements RoundedStep {
private static final long serialVersionUID = 1L;
private static final String DEFAULT_VERSION = "0.46";
static final String NAME = "ktfmt";
static final String PACKAGE = "com.facebook";
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:";
private static final String NAME = "ktfmt";
private static final String MAVEN_COORDINATE = "com.facebook:ktfmt:";

private final String version;
/**
* Option that allows to apply formatting options to perform a 4-space block and continuation indent.
*/
@Nullable
private final Style style;
@Nullable
private final KtfmtFormattingOptions options;
/** The jar that contains the formatter. */
private final JarState.Promised jarState;

private KtfmtStep(String version,
JarState.Promised jarState,
@Nullable Style style,
@Nullable KtfmtFormattingOptions options) {
this.version = Objects.requireNonNull(version, "version");
this.style = style;
this.options = options;
this.jarState = Objects.requireNonNull(jarState, "jarState");
}

/**
* Used to allow multiple style option through formatting options and since when is each of them available.
Expand Down Expand Up @@ -136,39 +154,38 @@ public static FormatterStep create(String version, Provisioner provisioner) {
public static FormatterStep create(String version, Provisioner provisioner, @Nullable Style style, @Nullable KtfmtFormattingOptions options) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(
NAME, () -> new State(version, provisioner, style, options), State::createFormat);
return FormatterStep.create(NAME,
new KtfmtStep(version, JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner)), style, options),
KtfmtStep::equalityState,
State::createFormat);
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;
private State equalityState() {
return new State(version, jarState.get(), style, options);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;
private static final String PACKAGE = "com.facebook.ktfmt";
private final String version;

private final String pkg;
/**
* Option that allows to apply formatting options to perform a 4 spaces block and continuation indent.
*/
@Nullable
private final Style style;
/**
*
*/
@Nullable
private final KtfmtFormattingOptions options;
/** The jar that contains the formatter. */
final JarState jarState;
private final JarState jarState;

State(String version, Provisioner provisioner, @Nullable Style style, @Nullable KtfmtFormattingOptions options) throws IOException {
State(String version,
JarState jarState,
@Nullable Style style,
@Nullable KtfmtFormattingOptions options) {
this.version = version;
this.options = options;
this.pkg = PACKAGE;
this.style = style;
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
this.jarState = jarState;
}

FormatterFunc createFormat() throws Exception {
Expand Down Expand Up @@ -259,7 +276,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)

// fallback to old, pre-0.19 ktfmt interface.
if (style == Style.DEFAULT || style == Style.DROPBOX) {
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions$Companion");
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(PACKAGE + ".FormattingOptions$Companion");
Object companion = formattingOptionsCompanionClazz.getConstructors()[0].newInstance((Object) null);
Method formattingOptionsMethod = formattingOptionsCompanionClazz.getDeclaredMethod("dropboxStyle");
return formattingOptionsMethod.invoke(companion);
Expand All @@ -271,19 +288,19 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
private Class<?> getFormatterClazz(ClassLoader classLoader) throws Exception {
Class<?> formatterClazz;
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.format.Formatter");
formatterClazz = classLoader.loadClass(PACKAGE + ".format.Formatter");
} else {
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
formatterClazz = classLoader.loadClass(PACKAGE + ".FormatterKt");
}
return formatterClazz;
}

private Class<?> getFormattingOptionsClazz(ClassLoader classLoader) throws Exception {
Class<?> formattingOptionsClazz;
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.format.FormattingOptions");
formattingOptionsClazz = classLoader.loadClass(PACKAGE + ".format.FormattingOptions");
} else {
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
formattingOptionsClazz = classLoader.loadClass(PACKAGE + ".FormattingOptions");
}
return formattingOptionsClazz;
}
Expand Down

0 comments on commit 6e6ceaa

Please sign in to comment.