Skip to content

Commit a89ba3a

Browse files
committed
Progress with doc and tests
1 parent d3052d3 commit a89ba3a

File tree

8 files changed

+80
-7
lines changed

8 files changed

+80
-7
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Added `skipLinesMatching` option to `licenseHeader` to support formats where license header cannot be immediately added to the top of the file (e.g. xml, sh). ([#1441](https://github.com/diffplug/spotless/pull/1441)).
15+
* Add YAML support through Jackson ([#1478](https://github.com/diffplug/spotless/pull/1478))
1516
### Fixed
1617
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
1718

lib/src/main/java/com/diffplug/spotless/yaml/YamlJacksonStep.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,8 +92,7 @@ FormatterFunc toFormatter() {
9292
// Configure the ObjectMapper
9393
// https://github.com/FasterXML/jackson-databind#commonly-used-features
9494
{
95-
serializationFeatureClass =
96-
classLoader.loadClass("com.fasterxml.jackson.databind.SerializationFeature");
95+
serializationFeatureClass = classLoader.loadClass("com.fasterxml.jackson.databind.SerializationFeature");
9796
enableFeature = objectMapperClass.getMethod("enable", serializationFeatureClass);
9897
disableFeature = objectMapperClass.getMethod("disable", serializationFeatureClass);
9998
}

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Added `skipLinesMatching` option to `licenseHeader` to support formats where license header cannot be immediately added to the top of the file (e.g. xml, sh). ([#1441](https://github.com/diffplug/spotless/pull/1441))
8+
* Add YAML support through Jackson ([#1478](https://github.com/diffplug/spotless/pull/1478))
89
### Fixed
910
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
1011
### Changes

plugin-maven/README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ user@machine repo % mvn spotless:check
5959
- [Markdown](#markdown) ([flexmark](#flexmark))
6060
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
6161
- [JSON](#json)
62+
- [YAML](#yaml)
6263
- Multiple languages
6364
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
6465
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -712,7 +713,7 @@ For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#n
712713

713714
## JSON
714715

715-
- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/json.java)
716+
- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java)
716717

717718
```xml
718719
<configuration>
@@ -759,6 +760,42 @@ for details.
759760

760761
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
761762

763+
764+
## YAML
765+
766+
- `com.diffplug.spotless.maven.FormatterFactory.addStepFactory(FormatterStepFactory)` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/yaml/Yaml.java)
767+
768+
```xml
769+
<configuration>
770+
<yaml>
771+
<includes> <!-- These are the default includes -->
772+
<include>**/*.yaml</include>
773+
<include>**/*.yml</include>
774+
</includes>
775+
776+
<jackson /> <!-- has its own section below -->
777+
</yaml>
778+
</configuration>
779+
```
780+
781+
### jackson
782+
783+
Uses Jackson and YAMLFactory to pretty print objects:
784+
785+
```xml
786+
<jackson>
787+
<version>2.13.4</version> <!-- optional: The version of 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' to be used -->
788+
<enabledFeatures> <!-- optional: Customize the set of enabled features -->
789+
<enabledFeature>INDENT_OUTPUT<enabledFeature/>
790+
</enabledFeatures>
791+
<disabledFeatures> <!-- optional: Customize the set of disabled features -->
792+
<disabledFeature>DEFAULT_HAS_TO_DISABLED_FEATURE<disabledFeature/>
793+
</disabledFeatures>
794+
</jackson>
795+
```
796+
797+
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
798+
762799
## Prettier
763800

764801
[homepage](https://prettier.io/). [changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md). [official plugins](https://prettier.io/docs/en/plugins.html#official-plugins). [community plugins](https://prettier.io/docs/en/plugins.html#community-plugins). Prettier is a formatter that can format almost every anything - JavaScript, JSX, Angular, Vue, Flow, TypeScript, CSS, Less, SCSS, HTML, JSON, GraphQL, Markdown (including GFM and MDX), and YAML. It can format even more [using plugins](https://prettier.io/docs/en/plugins.html) (PHP, Ruby, Swift, XML, Apex, Elm, Java (!!), Kotlin, pgSQL, .properties, solidity, svelte, toml, shellscript, ...).

plugin-maven/src/main/java/com/diffplug/spotless/maven/yaml/JacksonYaml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JacksonYaml implements FormatterStepFactory {
3131
private String version = YamlJacksonStep.defaultVersion();
3232

3333
@Parameter
34-
private String[] enabledFeatures = new String[] { "INDENT_OUTPUT" };
34+
private String[] enabledFeatures = new String[]{"INDENT_OUTPUT"};
3535

3636
@Parameter
3737
private String[] disabledFeatures = new String[0];

plugin-maven/src/main/java/com/diffplug/spotless/maven/yaml/Yaml.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.maven.yaml;
1717

18-
import java.util.Collections;
1918
import java.util.Set;
2019

20+
import com.diffplug.common.collect.Sets;
2121
import com.diffplug.spotless.maven.FormatterFactory;
2222

2323
/**
@@ -26,7 +26,7 @@
2626
public class Yaml extends FormatterFactory {
2727
@Override
2828
public Set<String> defaultIncludes() {
29-
return Collections.emptySet();
29+
return Sets.newHashSet("**/*.yaml", "**/*.yml");
3030
}
3131

3232
@Override

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ protected void writePomWithJsonSteps(String... steps) throws IOException {
160160
writePom(groupWithSteps("json", including("**/*.json"), steps));
161161
}
162162

163+
protected void writePomWithYamlSteps(String... steps) throws IOException {
164+
writePom(groupWithSteps("yaml", steps));
165+
}
166+
163167
protected void writePom(String... configuration) throws IOException {
164168
writePom(null, configuration, null);
165169
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 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.maven.yaml;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
public class YamlTest extends MavenIntegrationHarness {
23+
@Test
24+
public void testFormatJson_WithSimple_defaultConfig() throws Exception {
25+
writePomWithJsonSteps("<yaml><jackson/></yaml>");
26+
27+
setFile("yaml_test.json").toResource("yaml/separator_comments.yaml");
28+
mavenRunner().withArguments("spotless:apply").runNoError().error();
29+
assertFile("yaml_test.json").sameAsResource("yaml/separator_comments.clean.yaml");
30+
}
31+
}

0 commit comments

Comments
 (0)