Skip to content

Commit 1ac85b7

Browse files
authored
feat: port clang-format to maven plugin (#2406)
2 parents 9a44f53 + 2c76560 commit 1ac85b7

File tree

7 files changed

+130
-11
lines changed

7 files changed

+130
-11
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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+
* Support for`clang-format` on maven-plugin ([#2406](https://github.com/diffplug/spotless/pull/2406))
1314

1415
## [3.0.2] - 2025-01-14
1516
### Fixed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
* Support for `clang-format` ([#2406](https://github.com/diffplug/spotless/pull/2406))
67

78
## [2.44.2] - 2025-01-14
89
* Eclipse-based tasks can now handle parallel configuration ([#2389](https://github.com/diffplug/spotless/issues/2389))

plugin-maven/README.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ user@machine repo % mvn spotless:check
4343
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
4444
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
4545
- [Scala](#scala) ([scalafmt](#scalafmt))
46-
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
46+
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt), [clang-format](#clang-format))
4747
- [Python](#python) ([black](#black))
4848
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
4949
- [Sql](#sql) ([dbeaver](#dbeaver))
@@ -56,7 +56,7 @@ user@machine repo % mvn spotless:check
5656
- [Gherkin](#gherkin)
5757
- [Go](#go)
5858
- [RDF](#RDF)
59-
- [Protobuf](#protobuf) ([buf](#buf))
59+
- [Protobuf](#protobuf) ([buf](#buf), [clang-format](#clang))
6060
- Multiple languages
6161
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
6262
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -558,6 +558,18 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
558558
</eclipseCdt>
559559
```
560560

561+
### clang-format
562+
563+
[homepage](https://clang.llvm.org/docs/ClangFormat.html). [changelog](https://releases.llvm.org/download.html). `clang-format` is a formatter for c, c++, c#, objective-c, protobuf, javascript, and java. You can use clang-format in any language-specific format, but usually you will be creating a generic format.
564+
565+
```xml
566+
<clangFormat>
567+
<version>14.0.0-1ubuntu1.1</version> <!-- optional version of clang-format -->
568+
<pathToExe>/path/to/buf</pathToExe> <!-- optional: if clang-format isn't in your path -->
569+
<style>LLVM</style> <!-- optional: can be LLVM, Google, Chromium, Mozilla, WebKit -->
570+
</clangFormat>
571+
```
572+
561573
## Python
562574

563575
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Python.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Black.java).
@@ -1218,17 +1230,17 @@ RDF parsing is done via [Apache Jena](https://jena.apache.org/) in the version t
12181230
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf).
12191231
```xml
12201232
<configuration>
1221-
<includes> <!-- optiona: default is **/*.proto -->
1222-
<include>proto/*.proto<include>
1223-
<includes>
1233+
<includes> <!-- optional: default is **/*.proto -->
1234+
<include>proto/*.proto</include>
1235+
</includes>
12241236

1225-
<excludes> <!-- optiona: if you want to ignore auto generated protos -->
1226-
<include>target/**/<include>
1227-
<excludes>
1237+
<excludes> <!-- optional: if you want to ignore auto generated protos -->
1238+
<exclude>target/**/<exclude>
1239+
</excludes>
12281240

12291241
<protobuf>
12301242
<buf /> <!-- has its own section below -->
1231-
</css>
1243+
</protobuf>
12321244
</configuration>
12331245
```
12341246

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024-2025 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.cpp;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.cpp.ClangFormatStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Clang implements FormatterStepFactory {
26+
@Parameter
27+
private String version;
28+
29+
@Parameter
30+
private String pathToExe;
31+
32+
@Parameter
33+
private String style;
34+
35+
@Override
36+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
37+
ClangFormatStep clang = ClangFormatStep.withVersion(version == null ? ClangFormatStep.defaultVersion() : version);
38+
39+
if (pathToExe != null) {
40+
clang = clang.withPathToExe(pathToExe);
41+
}
42+
43+
if (style != null) {
44+
clang = clang.withStyle(style);
45+
}
46+
47+
return clang.create();
48+
}
49+
50+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -40,6 +40,10 @@ public void addEclipseCdt(EclipseCdt eclipse) {
4040
addStepFactory(eclipse);
4141
}
4242

43+
public void addClangFormat(Clang clang) {
44+
addStepFactory(clang);
45+
}
46+
4347
@Override
4448
public String licenseHeaderDelimiter() {
4549
return CppDefaults.DELIMITER_EXPR;

plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -23,6 +23,7 @@
2323

2424
import com.diffplug.common.collect.ImmutableSet;
2525
import com.diffplug.spotless.maven.FormatterFactory;
26+
import com.diffplug.spotless.maven.cpp.Clang;
2627
import com.diffplug.spotless.maven.generic.LicenseHeader;
2728

2829
/**
@@ -49,4 +50,8 @@ public String licenseHeaderDelimiter() {
4950
public void addBuf(Buf buf) {
5051
addStepFactory(buf);
5152
}
53+
54+
public void addClang(Clang clang) {
55+
addStepFactory(clang);
56+
}
5257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024-2025 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.cpp;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
import com.diffplug.spotless.tag.ClangTest;
22+
23+
@ClangTest
24+
class ClangMavenIntegrationTest extends MavenIntegrationHarness {
25+
26+
@Test
27+
@ClangTest
28+
void csharp() throws Exception {
29+
writePomWithCppSteps("<includes>", "<include>", "src/**/*.cs", "</include>", "</includes>",
30+
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
31+
setFile("src/test.cs").toResource("clang/example.cs");
32+
mavenRunner().withArguments("spotless:apply").runNoError();
33+
assertFile("src/test.cs").sameAsResource("clang/example.cs.clean");
34+
}
35+
36+
@Test
37+
@ClangTest
38+
void proto() throws Exception {
39+
writePomWithCppSteps("<includes>", "<include>", "**/*.proto", "</include>", "</includes>",
40+
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
41+
setFile("buf.proto").toResource("protobuf/buf/buf.proto");
42+
mavenRunner().withArguments("spotless:apply").runNoError();
43+
assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
44+
}
45+
46+
}

0 commit comments

Comments
 (0)