Skip to content

Commit 7d1fe1f

Browse files
committed
port clang-format to maven plugin
1 parent ecd1f16 commit 7d1fe1f

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed
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+
}

Diff for: 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;

Diff for: 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)