Skip to content

Commit

Permalink
feat: port clang-format to maven plugin (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Feb 12, 2025
2 parents 9a44f53 + 2c76560 commit 1ac85b7
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
* Support for`clang-format` on maven-plugin ([#2406](https://github.com/diffplug/spotless/pull/2406))

## [3.0.2] - 2025-01-14
### Fixed
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.44.2] - 2025-01-14
* Eclipse-based tasks can now handle parallel configuration ([#2389](https://github.com/diffplug/spotless/issues/2389))
Expand Down
30 changes: 21 additions & 9 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ user@machine repo % mvn spotless:check
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
- [Scala](#scala) ([scalafmt](#scalafmt))
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt), [clang-format](#clang-format))
- [Python](#python) ([black](#black))
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
- [Sql](#sql) ([dbeaver](#dbeaver))
Expand All @@ -56,7 +56,7 @@ user@machine repo % mvn spotless:check
- [Gherkin](#gherkin)
- [Go](#go)
- [RDF](#RDF)
- [Protobuf](#protobuf) ([buf](#buf))
- [Protobuf](#protobuf) ([buf](#buf), [clang-format](#clang))
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
- [eclipse web tools platform](#eclipse-web-tools-platform)
Expand Down Expand Up @@ -558,6 +558,18 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
</eclipseCdt>
```

### clang-format

[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.

```xml
<clangFormat>
<version>14.0.0-1ubuntu1.1</version> <!-- optional version of clang-format -->
<pathToExe>/path/to/buf</pathToExe> <!-- optional: if clang-format isn't in your path -->
<style>LLVM</style> <!-- optional: can be LLVM, Google, Chromium, Mozilla, WebKit -->
</clangFormat>
```

## Python

[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).
Expand Down Expand Up @@ -1218,17 +1230,17 @@ RDF parsing is done via [Apache Jena](https://jena.apache.org/) in the version t
[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).
```xml
<configuration>
<includes> <!-- optiona: default is **/*.proto -->
<include>proto/*.proto<include>
<includes>
<includes> <!-- optional: default is **/*.proto -->
<include>proto/*.proto</include>
</includes>

<excludes> <!-- optiona: if you want to ignore auto generated protos -->
<include>target/**/<include>
<excludes>
<excludes> <!-- optional: if you want to ignore auto generated protos -->
<exclude>target/**/<exclude>
</excludes>

<protobuf>
<buf /> <!-- has its own section below -->
</css>
</protobuf>
</configuration>
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.cpp;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.cpp.ClangFormatStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Clang implements FormatterStepFactory {
@Parameter
private String version;

@Parameter
private String pathToExe;

@Parameter
private String style;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
ClangFormatStep clang = ClangFormatStep.withVersion(version == null ? ClangFormatStep.defaultVersion() : version);

if (pathToExe != null) {
clang = clang.withPathToExe(pathToExe);
}

if (style != null) {
clang = clang.withStyle(style);
}

return clang.create();
}

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

public void addClangFormat(Clang clang) {
addStepFactory(clang);
}

@Override
public String licenseHeaderDelimiter() {
return CppDefaults.DELIMITER_EXPR;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 DiffPlug
* Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.cpp.Clang;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
Expand All @@ -49,4 +50,8 @@ public String licenseHeaderDelimiter() {
public void addBuf(Buf buf) {
addStepFactory(buf);
}

public void addClang(Clang clang) {
addStepFactory(clang);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.cpp;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;
import com.diffplug.spotless.tag.ClangTest;

@ClangTest
class ClangMavenIntegrationTest extends MavenIntegrationHarness {

@Test
@ClangTest
void csharp() throws Exception {
writePomWithCppSteps("<includes>", "<include>", "src/**/*.cs", "</include>", "</includes>",
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
setFile("src/test.cs").toResource("clang/example.cs");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("src/test.cs").sameAsResource("clang/example.cs.clean");
}

@Test
@ClangTest
void proto() throws Exception {
writePomWithCppSteps("<includes>", "<include>", "**/*.proto", "</include>", "</includes>",
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
setFile("buf.proto").toResource("protobuf/buf/buf.proto");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
}

}

0 comments on commit 1ac85b7

Please sign in to comment.