Skip to content
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

feat(#2183): flexmark configuration support #2280

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ dependencies {
compatDiktat2Dot0Dot0CompileOnly "com.saveourtool.diktat:diktat-runner:2.0.0"
// flexmark
flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.64.8'
implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has to be flexmarkCompileOnly, not implementation. Spotless has way too many dependencies to deal with all the transitive conflicts. You can only use flexmark classes inside of FlexmarkFormatterFunc, which gets called with a special classloader after Gradle/Maven has done the dependency resolution.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nedtwigg okay, got it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nedtwigg I'm a bit confused. If we 're using only flexmarkCompileOnly it means that I can't get access to flexmark-util-data API inside of FlexmarkStep, which makes it impossible to run with custom configuration or am I wrong?

// gherkin
gherkinCompileOnly 'io.cucumber:gherkin-utils:9.0.0'
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.16'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class FlexmarkFormatterFunc implements FormatterFunc {
private final Parser parser;
private final Formatter formatter;

public FlexmarkFormatterFunc() {
public FlexmarkFormatterFunc(final MutableDataHolder customFormatterOptions) {
// flexmark-java has a separate parser and renderer (formatter)
// this is build from the example in https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter

Expand All @@ -52,7 +52,7 @@ public FlexmarkFormatterFunc() {
final MutableDataHolder formatterOptions = createFormatterOptions(parserOptions, emulationProfile);

parser = Parser.builder(parserOptions).build();
formatter = Formatter.builder(formatterOptions).build();
formatter = Formatter.builder(MutableDataSet.merge(formatterOptions, customFormatterOptions)).build();
}

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ private static MutableDataHolder createFormatterOptions(MutableDataHolder parser
}

@Override
public String apply(String input) throws Exception {
public String apply(String input) {
final Document parsedMarkdown = parser.parse(input);
return formatter.render(parsedMarkdown);
}
Expand Down
82 changes: 67 additions & 15 deletions lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/
package com.diffplug.spotless.markdown;

import static com.diffplug.spotless.JarState.Promised;
import static com.diffplug.spotless.JarState.from;
import static com.diffplug.spotless.JarState.promise;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.Objects;

import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
Expand All @@ -31,49 +38,94 @@ public class FlexmarkStep implements Serializable {
private static final String MAVEN_COORDINATE = "com.vladsch.flexmark:flexmark-all:";
public static final String NAME = "flexmark-java";

private final JarState.Promised jarState;
private final Promised jarState;
private final transient MutableDataSet options;

private FlexmarkStep(JarState.Promised jarState) {
private FlexmarkStep(
final Promised jarState,
final MutableDataSet options
) {
this.jarState = jarState;
this.options = options;
}

/** Creates a formatter step for the custom version. */
public static FormatterStep create(
final String version,
final Provisioner provisioner
) {
return FlexmarkStep.create(
version,
provisioner,
new MutableDataSet()
);
}

/** Creates a formatter step for the default version. */
public static FormatterStep create(Provisioner provisioner) {
return create(defaultVersion(), provisioner);
public static FormatterStep create(final Provisioner provisioner) {
return FlexmarkStep.create(
FlexmarkStep.defaultVersion(),
provisioner,
new MutableDataSet()
);
}

/** Creates a formatter step for the default version with custom options. */
public static FormatterStep create(
final Provisioner provisioner,
final MutableDataSet options
) {
return FlexmarkStep.create(
FlexmarkStep.defaultVersion(),
provisioner,
options
);
}

/** Creates a formatter step for the given version. */
public static FormatterStep create(String version, Provisioner provisioner) {
public static FormatterStep create(
final String version,
final Provisioner provisioner,
final MutableDataSet options
) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.create(NAME,
new FlexmarkStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner))),
FlexmarkStep::equalityState,
State::createFormat);
return FormatterStep.create(
FlexmarkStep.NAME,
new FlexmarkStep(
promise(() -> from(FlexmarkStep.MAVEN_COORDINATE + version, provisioner)),
options
),
FlexmarkStep::equalityState,
State::createFormat
);
}

public static String defaultVersion() {
return DEFAULT_VERSION;
return FlexmarkStep.DEFAULT_VERSION;
}

private State equalityState() {
return new State(jarState.get());
return new State(this.jarState.get(), this.options);
}

private static class State implements Serializable {
private static final long serialVersionUID = 1L;

private final JarState jarState;

State(JarState jarState) {
private final MutableDataSet options;

State(final JarState jarState, final MutableDataSet options) {
this.jarState = jarState;
this.options = options;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
final ClassLoader classLoader = this.jarState.getClassLoader();
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.markdown.FlexmarkFormatterFunc");
final Constructor<?> constructor = formatterFunc.getConstructor();
return (FormatterFunc) constructor.newInstance();
final Constructor<?> constructor = formatterFunc.getConstructor(MutableDataHolder.class);
return (FormatterFunc) constructor.newInstance(this.options);
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dependencies {
implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8'

testImplementation projects.testlib
testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,38 @@

import javax.inject.Inject;

import com.vladsch.flexmark.util.data.MutableDataSet;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.markdown.FlexmarkStep;

public class FlexmarkExtension extends FormatExtension {
static final String NAME = "flexmark";

@Inject
public FlexmarkExtension(SpotlessExtension spotless) {
public FlexmarkExtension(final SpotlessExtension spotless) {
super(spotless);
}

public FlexmarkFormatterConfig flexmark() {
return flexmark(FlexmarkStep.defaultVersion());
}

public FlexmarkFormatterConfig flexmark(String version) {
public FlexmarkFormatterConfig flexmark(final String version) {
return new FlexmarkFormatterConfig(version);
}

public FlexmarkFormatterConfig flexmark(
final String version,
final MutableDataSet options
) {
return new FlexmarkFormatterConfig(version, options);
}

@Override
protected void setupTask(SpotlessTask task) {
protected void setupTask(final SpotlessTask task) {
// defaults to all markdown files
if (target == null) {
if (this.target == null) {
throw noDefaultTargetException();
}
super.setupTask(task);
Expand All @@ -50,14 +59,29 @@ protected void setupTask(SpotlessTask task) {
public class FlexmarkFormatterConfig {

private final String version;
private final MutableDataSet options;

FlexmarkFormatterConfig(final String version) {
this.version = Objects.requireNonNull(version);
this.options = new MutableDataSet();
addStep(createStep());
}

FlexmarkFormatterConfig(String version) {
FlexmarkFormatterConfig(
final String version,
final MutableDataSet options
) {
this.version = Objects.requireNonNull(version);
this.options = options;
addStep(createStep());
}

private FormatterStep createStep() {
return FlexmarkStep.create(this.version, provisioner());
return FlexmarkStep.create(
this.version,
provisioner(),
this.options
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions testlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8'
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can't be here either.

implementation gradleTestKit()
}

Expand Down
Loading