diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e6658fad..31f0337a39 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +- Add `biome()` to `yaml` and `flexmark` for formatting YAML and Markdown with Biome (requires Biome `2.6.2` or later). ([#3155](https://github.com/diffplug/spotless/issues/3155)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) - Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 82c9dc377c..82da2377dd 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -88,14 +88,14 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [Protobuf](#protobuf) ([buf](#buf), [clang-format](#clang-format)) - [Python](#python) ([black](#black)) - [FreshMark](#freshmark) aka markdown - - [Flexmark](#flexmark) aka markdown + - [Flexmark](#flexmark) aka markdown ([flexmark](#flexmark), [Biome](#biome)) - [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter)) - [SQL](#sql) ([dbeaver](#dbeaver), [prettier](#prettier), [IntelliJ IDEA](#intellij-idea)) - [Maven POM](#maven-pom) ([sortPom](#sortpom)) - [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier), [ESLint](#eslint-typescript), [Biome](#biome)) - [Javascript](#javascript) ([prettier](#prettier), [ESLint](#eslint-javascript), [Biome](#biome)) - [JSON](#json) ([simple](#simple), [gson](#gson), [jackson](#jackson), [Biome](#biome), [jsonPatch](#jsonPatch)) - - [YAML](#yaml) + - [YAML](#yaml) ([jackson](#jackson-1), [prettier](#prettier), [Biome](#biome)) - [Shell](#shell) - [Gherkin](#gherkin) - Multiple languages @@ -985,6 +985,19 @@ spotless { } ``` +Instead of flexmark, you can also use [Biome](#biome) to format Markdown files: + +```gradle +spotless { + flexmark { + target '**/*.md' + biome('2.6.9') // has its own section below + } +} +``` + +Note regarding biome: Biome supports formatting Markdown as of 2.6.2. + ## Antlr4 `com.diffplug.gradle.spotless.Antlr4Extension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/8.12.0/com/diffplug/gradle/spotless/Antlr4Extension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/Antlr4Extension.java) @@ -1424,10 +1437,13 @@ spotless { target 'src/**/*.yaml' // you have to set the target manually jackson() // has its own section below prettier() // has its own section below + biome('2.6.9') // has its own section below } } ``` +Note regarding biome: Biome supports formatting YAML as of 2.6.2. + ### Jackson Uses Jackson for `yaml` files. @@ -1761,7 +1777,9 @@ is pretty fast. It can currently format JavaScript, TypeScript, JSX, and JSON, a You can use Biome in any language-specific format for supported languages, but usually you will be creating a generic format. -Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable). +Note regarding biome: Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable), +and formatting YAML and Markdown as of 2.6.2. Biome 2.6.0 and 2.6.1 were not published as GitHub releases +and therefore cannot be downloaded by Spotless. ```gradle spotless { @@ -1944,6 +1962,10 @@ The following languages are currently recognized: * `tsx` -- TypeScript + JSX (React) * `ts?` -- TypeScript, with or without JSX, depending on the file extension * `json` -- JSON +* `jsonc` -- JSON + comments +* `css` -- CSS (requires Biome >= 1.9.0) +* `yaml` -- YAML (requires Biome >= 2.6.2) +* `md` -- Markdown (requires Biome >= 2.6.2) ## IntelliJ IDEA diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java index 9611b7399b..432d6f14fd 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java @@ -41,6 +41,25 @@ public FlexmarkFormatterConfig flexmark(String version) { return new FlexmarkFormatterConfig(version); } + /** + * Formatting Markdown requires Biome 2.6.2 or later. + */ + @Override + public BiomeFlexmark biome() { + return biome(null); + } + + /** + * Formatting Markdown requires Biome 2.6.2 or later. + * @param version Biome version to use. + */ + @Override + public BiomeFlexmark biome(String version) { + var biomeConfig = new BiomeFlexmark(version); + addStep(biomeConfig.createStep()); + return biomeConfig; + } + @Override protected void setupTask(SpotlessTask task) { // defaults to all markdown files @@ -90,4 +109,20 @@ private FormatterStep createStep() { } } + public class BiomeFlexmark extends BiomeStepConfig { + + public BiomeFlexmark(String version) { + super(getProject(), FlexmarkExtension.this::replaceStep, version); + } + + @Override + protected String getLanguage() { + return "md"; + } + + @Override + protected BiomeFlexmark getThis() { + return this; + } + } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 565fd22315..d3bd7131aa 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -911,6 +911,8 @@ public BiomeGeneric(String version) { *
  • css (CSS, requires biome >= 1.9.0)
  • *
  • json (JSON)
  • *
  • jsonc (JSON + comments)
  • + *
  • yaml (YAML, requires biome >= 2.6.2)
  • + *
  • md (Markdown, requires biome >= 2.6.2)
  • * * * @param language The language of the files to format. diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/YamlExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/YamlExtension.java index 9551538654..f814235e55 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/YamlExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/YamlExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,25 @@ public JacksonYamlGradleConfig jackson() { return new JacksonYamlGradleConfig(this); } + /** + * Formatting YAML requires Biome 2.6.2 or later. + */ + @Override + public BiomeYaml biome() { + return biome(null); + } + + /** + * Formatting YAML requires Biome 2.6.2 or later. + * @param version Biome version to use. + */ + @Override + public BiomeYaml biome(String version) { + var biomeConfig = new BiomeYaml(version); + addStep(biomeConfig.createStep()); + return biomeConfig; + } + public class JacksonYamlGradleConfig extends AJacksonGradleConfig { protected JacksonYamlConfig jacksonConfig; @@ -78,4 +97,21 @@ protected final FormatterStep createStep() { return JacksonYamlStep.create(jacksonConfig, version, provisioner()); } } + + public class BiomeYaml extends BiomeStepConfig { + + public BiomeYaml(String version) { + super(getProject(), YamlExtension.this::replaceStep, version); + } + + @Override + protected String getLanguage() { + return "yaml"; + } + + @Override + protected BiomeYaml getThis() { + return this; + } + } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java index 02ef09ec01..665e85e5ee 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 DiffPlug + * Copyright 2023-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,6 +80,58 @@ void asCssStepStable() throws Exception { assertFile("biome_test.css").sameAsResource("biome/css/fileAfter.css"); } + /** + * Tests that biome can be used as a Markdown formatting step, which requires + * biome 2.6.2 or later. + * + * @throws Exception When a test failure occurs. + */ + @Test + void asFlexmarkStep() throws Exception { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " flexmark {", + " target '**/*.md'", + " biome('2.6.9')", + " }", + "}"); + setFile("biome_test.md").toResource("biome/md/fileBefore.md"); + + var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("biome_test.md").sameAsResource("biome/md/fileAfter.md"); + } + + /** + * Tests that the Markdown formatting step formats files as Markdown even when + * they do not have a Markdown file extension. + * + * @throws Exception When a test failure occurs. + */ + @Test + void asFlexmarkStepWithOtherExtension() throws Exception { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " flexmark {", + " target '**/*.txt'", + " biome('2.6.9')", + " }", + "}"); + setFile("biome_test.txt").toResource("biome/md/fileBefore.md"); + + var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("biome_test.txt").sameAsResource("biome/md/fileAfter.md"); + } + /** * Tests that biome can be used as a generic formatting step. * @@ -180,6 +232,58 @@ void asTypeScriptStep() throws Exception { assertFile("biome_test.ts").sameAsResource("biome/ts/fileAfter.ts"); } + /** + * Tests that biome can be used as a YAML formatting step, which requires biome + * 2.6.2 or later. + * + * @throws Exception When a test failure occurs. + */ + @Test + void asYamlStep() throws Exception { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " yaml {", + " target '**/*.yaml'", + " biome('2.6.9')", + " }", + "}"); + setFile("biome_test.yaml").toResource("biome/yaml/fileBefore.yaml"); + + var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("biome_test.yaml").sameAsResource("biome/yaml/fileAfter.yaml"); + } + + /** + * Tests that the YAML formatting step formats files as YAML even when they + * do not have a YAML file extension. + * + * @throws Exception When a test failure occurs. + */ + @Test + void asYamlStepWithOtherExtension() throws Exception { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " yaml {", + " target '**/*.tmpl'", + " biome('2.6.9')", + " }", + "}"); + setFile("biome_test.yaml.tmpl").toResource("biome/yaml/fileBefore.yaml"); + + var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("biome_test.yaml.tmpl").sameAsResource("biome/yaml/fileAfter.yaml"); + } + /** * Tests that the language can be specified for the generic format step. * diff --git a/testlib/src/main/resources/biome/md/fileAfter.md b/testlib/src/main/resources/biome/md/fileAfter.md new file mode 100644 index 0000000000..b7989a1f0f --- /dev/null +++ b/testlib/src/main/resources/biome/md/fileAfter.md @@ -0,0 +1,10 @@ +# Spotless + +Some _emphasis_ and **strong** text. + +- first item +- second item + +```java +class Foo {} +``` diff --git a/testlib/src/main/resources/biome/md/fileBefore.md b/testlib/src/main/resources/biome/md/fileBefore.md new file mode 100644 index 0000000000..a08cbcf438 --- /dev/null +++ b/testlib/src/main/resources/biome/md/fileBefore.md @@ -0,0 +1,10 @@ +# Spotless + +Some *emphasis* and __strong__ text. + +* first item +* second item + +```java +class Foo {} +``` diff --git a/testlib/src/main/resources/biome/yaml/fileAfter.yaml b/testlib/src/main/resources/biome/yaml/fileAfter.yaml new file mode 100644 index 0000000000..dfa3135603 --- /dev/null +++ b/testlib/src/main/resources/biome/yaml/fileAfter.yaml @@ -0,0 +1,8 @@ +name: spotless +version: 1.0 +languages: + - java + - kotlin +nested: + key: "value" + list: [a, b, c] diff --git a/testlib/src/main/resources/biome/yaml/fileBefore.yaml b/testlib/src/main/resources/biome/yaml/fileBefore.yaml new file mode 100644 index 0000000000..9dd90d7ee8 --- /dev/null +++ b/testlib/src/main/resources/biome/yaml/fileBefore.yaml @@ -0,0 +1,8 @@ +name: spotless +version: 1.0 +languages: + - java + - kotlin +nested: + key: "value" + list: [ a,b , c ]