Skip to content
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
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 25 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,4 +109,20 @@ private FormatterStep createStep() {
}
}

public class BiomeFlexmark extends BiomeStepConfig<BiomeFlexmark> {

public BiomeFlexmark(String version) {
super(getProject(), FlexmarkExtension.this::replaceStep, version);
}

@Override
protected String getLanguage() {
return "md";
}

@Override
protected BiomeFlexmark getThis() {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,8 @@ public BiomeGeneric(String version) {
* <li>css (CSS, requires biome &gt;= 1.9.0)</li>
* <li>json (JSON)</li>
* <li>jsonc (JSON + comments)</li>
* <li>yaml (YAML, requires biome &gt;= 2.6.2)</li>
* <li>md (Markdown, requires biome &gt;= 2.6.2)</li>
* </ul>
*
* @param language The language of the files to format.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<JacksonYamlGradleConfig> {
protected JacksonYamlConfig jacksonConfig;

Expand Down Expand Up @@ -78,4 +97,21 @@ protected final FormatterStep createStep() {
return JacksonYamlStep.create(jacksonConfig, version, provisioner());
}
}

public class BiomeYaml extends BiomeStepConfig<BiomeYaml> {

public BiomeYaml(String version) {
super(getProject(), YamlExtension.this::replaceStep, version);
}

@Override
protected String getLanguage() {
return "yaml";
}

@Override
protected BiomeYaml getThis() {
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions testlib/src/main/resources/biome/md/fileAfter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Spotless

Some _emphasis_ and **strong** text.

- first item
- second item

```java
class Foo {}
```
10 changes: 10 additions & 0 deletions testlib/src/main/resources/biome/md/fileBefore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Spotless

Some *emphasis* and __strong__ text.

* first item
* second item

```java
class Foo {}
```
8 changes: 8 additions & 0 deletions testlib/src/main/resources/biome/yaml/fileAfter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: spotless
version: 1.0
languages:
- java
- kotlin
nested:
key: "value"
list: [a, b, c]
8 changes: 8 additions & 0 deletions testlib/src/main/resources/biome/yaml/fileBefore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: spotless
version: 1.0
languages:
- java
- kotlin
nested:
key: "value"
list: [ a,b , c ]