Skip to content

Commit afb76f0

Browse files
authored
Merge branch 'main' into feat/dep-fixup
2 parents 8ae601c + 376b561 commit afb76f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2401
-43
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Added support for Markdown with `flexmark` at `0.62.2` ([#1011](https://github.com/diffplug/spotless/pull/1011)).
1315

1416
## [2.20.3] - 2021-12-15
1517
### Fixed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ output = [
4242
'| Toggle with [`spotless:off` and `spotless:on`](plugin-gradle/#spotlessoff-and-spotlesson) | {{yes}} | {{yes}} | {{no}} | {{no}} |',
4343
'| [Ratchet from](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ratchet) `origin/main` or other git ref | {{yes}} | {{yes}} | {{no}} | {{no}} |',
4444
'| Define [line endings using git](https://github.com/diffplug/spotless/tree/main/plugin-gradle#line-endings-and-encodings-invisible-stuff) | {{yes}} | {{yes}} | {{yes}} | {{no}} |',
45-
'| Fast incremental format and up-to-date check | {{yes}} | {{no}} | {{no}} | {{no}} |',
45+
'| Fast incremental format and up-to-date check | {{yes}} | {{yes}} | {{no}} | {{no}} |',
4646
'| Fast format on fresh checkout using buildcache | {{yes}} | {{no}} | {{no}} | {{no}} |',
4747
lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
4848
lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -64,6 +64,7 @@ lib('kotlin.KtLintStep') +'{{yes}} | {{yes}}
6464
lib('kotlin.KtfmtStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6565
lib('kotlin.DiktatStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6666
lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
67+
lib('markdown.FlexmarkStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
6768
lib('npm.PrettierFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6869
lib('npm.TsFmtFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6970
lib('pom.SortPomStepStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
@@ -81,7 +82,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
8182
| Toggle with [`spotless:off` and `spotless:on`](plugin-gradle/#spotlessoff-and-spotlesson) | :+1: | :+1: | :white_large_square: | :white_large_square: |
8283
| [Ratchet from](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ratchet) `origin/main` or other git ref | :+1: | :+1: | :white_large_square: | :white_large_square: |
8384
| Define [line endings using git](https://github.com/diffplug/spotless/tree/main/plugin-gradle#line-endings-and-encodings-invisible-stuff) | :+1: | :+1: | :+1: | :white_large_square: |
84-
| Fast incremental format and up-to-date check | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
85+
| Fast incremental format and up-to-date check | :+1: | :+1: | :white_large_square: | :white_large_square: |
8586
| Fast format on fresh checkout using buildcache | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
8687
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
8788
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
@@ -103,6 +104,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
103104
| [`kotlin.KtfmtStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
104105
| [`kotlin.DiktatStep`](lib/src/main/java/com/diffplug/spotless/kotlin/DiktatStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
105106
| [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
107+
| [`markdown.FlexmarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
106108
| [`npm.PrettierFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
107109
| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
108110
| [`pom.SortPomStepStep`](lib/src/main/java/com/diffplug/spotless/pom/SortPomStepStep.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ artifactIdGradle=spotless-plugin-gradle
1616
# Build requirements
1717
VER_JAVA=1.8
1818
VER_SPOTBUGS=4.5.0
19+
VER_JSR_305=3.0.2
1920

2021
# Dependencies provided by Spotless plugin
2122
VER_SLF4J=[1.6,2.0[

gradle/java-setup.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ tasks.named('spotbugsMain') {
3535
dependencies {
3636
compileOnly 'net.jcip:jcip-annotations:1.0'
3737
compileOnly "com.github.spotbugs:spotbugs-annotations:${VER_SPOTBUGS}"
38-
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
38+
compileOnly "com.google.code.findbugs:jsr305:${VER_JSR_305}"
3939
}

lib/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ apply from: rootProject.file('gradle/java-publish.gradle')
88

99
def NEEDS_GLUE = [
1010
'sortPom',
11-
'ktlint'
11+
'ktlint',
12+
'flexmark'
1213
]
1314
for (glue in NEEDS_GLUE) {
1415
sourceSets.register(glue) {
@@ -32,6 +33,9 @@ dependencies {
3233
ktlintCompileOnly "com.pinterest:ktlint:$VER_KTLINT"
3334
ktlintCompileOnly "com.pinterest.ktlint:ktlint-core:$VER_KTLINT"
3435
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-standard:$VER_KTLINT"
36+
37+
// used for markdown formatting
38+
flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.62.2'
3539
}
3640

3741
// we'll hold the core lib to a high standard
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.markdown;
17+
18+
import com.vladsch.flexmark.formatter.Formatter;
19+
import com.vladsch.flexmark.parser.Parser;
20+
import com.vladsch.flexmark.parser.ParserEmulationProfile;
21+
import com.vladsch.flexmark.parser.PegdownExtensions;
22+
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
23+
import com.vladsch.flexmark.util.ast.Document;
24+
import com.vladsch.flexmark.util.data.MutableDataHolder;
25+
import com.vladsch.flexmark.util.data.MutableDataSet;
26+
27+
import com.diffplug.spotless.FormatterFunc;
28+
29+
/**
30+
* The formatter function for <a href="https://github.com/vsch/flexmark-java">flexmark-java</a>.
31+
*/
32+
public class FlexmarkFormatterFunc implements FormatterFunc {
33+
34+
/**
35+
* The emulation profile is used by both the parser and the formatter and generally determines the markdown flavor.
36+
* COMMONMARK is the default defined by flexmark-java.
37+
*/
38+
private static final String DEFAULT_EMULATION_PROFILE = "COMMONMARK";
39+
40+
private final Parser parser;
41+
private final Formatter formatter;
42+
43+
public FlexmarkFormatterFunc() {
44+
// flexmark-java has a separate parser and renderer (formatter)
45+
// this is build from the example in https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter
46+
47+
// The emulation profile generally determines the markdown flavor. We use the same one for both the parser and
48+
// the formatter, to make sure this formatter func is idempotent.
49+
final ParserEmulationProfile emulationProfile = ParserEmulationProfile.valueOf(DEFAULT_EMULATION_PROFILE);
50+
51+
final MutableDataHolder parserOptions = createParserOptions(emulationProfile);
52+
final MutableDataHolder formatterOptions = createFormatterOptions(parserOptions, emulationProfile);
53+
54+
parser = Parser.builder(parserOptions).build();
55+
formatter = Formatter.builder(formatterOptions).build();
56+
}
57+
58+
/**
59+
* Creates the parser options.
60+
* See: https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter#options
61+
*
62+
* @param emulationProfile the emulation profile (or flavor of markdown) the parser should use
63+
* @return the created parser options
64+
*/
65+
private static MutableDataHolder createParserOptions(ParserEmulationProfile emulationProfile) {
66+
final MutableDataHolder parserOptions = PegdownOptionsAdapter.flexmarkOptions(PegdownExtensions.ALL).toMutable();
67+
parserOptions.set(Parser.PARSER_EMULATION_PROFILE, emulationProfile);
68+
return parserOptions;
69+
}
70+
71+
/**
72+
* Creates the formatter options, copies the parser extensions and changes defaults that make sense for a formatter.
73+
* See: https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter#options
74+
*
75+
* @param parserOptions the options used for the parser
76+
* @param emulationProfile the emulation profile (or flavor of markdown) the formatter should use
77+
* @return the created formatter options
78+
*/
79+
private static MutableDataHolder createFormatterOptions(MutableDataHolder parserOptions, ParserEmulationProfile emulationProfile) {
80+
final MutableDataHolder formatterOptions = new MutableDataSet();
81+
formatterOptions.set(Parser.EXTENSIONS, Parser.EXTENSIONS.get(parserOptions));
82+
formatterOptions.set(Formatter.FORMATTER_EMULATION_PROFILE, emulationProfile);
83+
return formatterOptions;
84+
}
85+
86+
@Override
87+
public String apply(String input) throws Exception {
88+
final Document parsedMarkdown = parser.parse(input);
89+
return formatter.render(parsedMarkdown);
90+
}
91+
}

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static boolean defaultReflowLongStrings() {
115115
static final class State implements Serializable {
116116
private static final long serialVersionUID = 1L;
117117

118-
/** The jar that contains the eclipse formatter. */
118+
/** The jar that contains the formatter. */
119119
final JarState jarState;
120120
final String stepName;
121121
final String version;

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static final class State implements Serializable {
8282
/** Are the files being linted Kotlin script files. */
8383
private final boolean isScript;
8484
private final String pkg;
85-
/** The jar that contains the eclipse formatter. */
85+
/** The jar that contains the formatter. */
8686
final JarState jarState;
8787
private final TreeMap<String, String> userData;
8888
private final boolean useParams;

lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static final class State implements Serializable {
108108
* Option that allows to apply formatting options to perform a 4 spaces block and continuation indent.
109109
*/
110110
private final Style style;
111-
/** The jar that contains the eclipse formatter. */
111+
/** The jar that contains the formatter. */
112112
final JarState jarState;
113113

114114
State(String version, Provisioner provisioner, Style style) throws IOException {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016-2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.markdown;
17+
18+
import java.io.Serializable;
19+
import java.lang.reflect.Constructor;
20+
import java.util.Objects;
21+
22+
import com.diffplug.spotless.FormatterFunc;
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.JarState;
25+
import com.diffplug.spotless.Provisioner;
26+
27+
/** A step for <a href="https://github.com/vsch/flexmark-java">flexmark-java</a>. */
28+
public class FlexmarkStep {
29+
// prevent direct instantiation
30+
private FlexmarkStep() {}
31+
32+
private static final String DEFAULT_VERSION = "0.62.2";
33+
private static final String NAME = "flexmark-java";
34+
private static final String MAVEN_COORDINATE = "com.vladsch.flexmark:flexmark-all:";
35+
36+
/** Creates a formatter step for the default version. */
37+
public static FormatterStep create(Provisioner provisioner) {
38+
return create(defaultVersion(), provisioner);
39+
}
40+
41+
/** Creates a formatter step for the given version. */
42+
public static FormatterStep create(String version, Provisioner provisioner) {
43+
Objects.requireNonNull(version, "version");
44+
Objects.requireNonNull(provisioner, "provisioner");
45+
return FormatterStep.createLazy(NAME,
46+
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner)),
47+
State::createFormat);
48+
}
49+
50+
public static String defaultVersion() {
51+
return DEFAULT_VERSION;
52+
}
53+
54+
private static class State implements Serializable {
55+
private static final long serialVersionUID = 1L;
56+
57+
/** The jar that contains the formatter. */
58+
final JarState jarState;
59+
60+
State(JarState jarState) {
61+
this.jarState = jarState;
62+
}
63+
64+
FormatterFunc createFormat() throws Exception {
65+
final ClassLoader classLoader = jarState.getClassLoader();
66+
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.markdown.FlexmarkFormatterFunc");
67+
final Constructor<?> constructor = formatterFunc.getConstructor();
68+
return (FormatterFunc) constructor.newInstance();
69+
}
70+
71+
}
72+
}

0 commit comments

Comments
 (0)