Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a step to remove semicolons in Groovy files #1900

Merged
merged 2 commits into from
Nov 27, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638))
### Changes
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
* Add a Step to remove semicolons from Groovy files. ([#1881](https://github.com/diffplug/spotless/pull/1881))

## [2.42.0] - 2023-09-28
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2023 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.groovy;

import java.io.BufferedReader;
import java.io.Serializable;
import java.io.StringReader;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;

/**
* Removes all semicolons from the end of lines.
*
* @author Jose Luis Badano
*/
public final class RemoveSemicolonsStep {
private static final String NAME = "Remove unnecessary semicolons";

private RemoveSemicolonsStep() {
// do not instantiate
}

public static FormatterStep create() {
return FormatterStep.createLazy(NAME,
State::new,
RemoveSemicolonsStep.State::toFormatter);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

FormatterFunc toFormatter() {
return raw -> {
try (BufferedReader reader = new BufferedReader(new StringReader(raw))) {
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(removeSemicolon(line));
result.append(System.lineSeparator());
}
return result.toString();
}
};
}

/**
* Removes the last semicolon in a line if it exists.
*
* @param line the line to remove the semicolon from
* @return the line without the last semicolon
*/
private String removeSemicolon(String line) {
// find last semicolon in a string a remove it
int lastSemicolon = line.lastIndexOf(";");
if (lastSemicolon != -1 && lastSemicolon == line.length() - 1) {
return line.substring(0, lastSemicolon);
} else {
return line;
}
}
}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Check if EditorConfig file exist for Ktlint in KotlinGradleExtension. ([#1889](https://github.com/diffplug/spotless/pull/1889))
### Changes
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
* Add a Step to remove semicolons from Groovy files. ([#1881](https://github.com/diffplug/spotless/pull/1881))
### Fixed
* Fix `GoogleJavaFormatConfig.reorderImports` not working. ([#1872](https://github.com/diffplug/spotless/issues/1872))

Expand Down
11 changes: 8 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,18 @@ spotless {
// optional: instead of specifying import groups directly you can specify a config file
// export config file: https://github.com/diffplug/spotless/blob/main/ECLIPSE_SCREENSHOTS.md#creating-spotlessimportorder
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse

excludeJava() // excludes all Java sources within the Groovy source dirs from formatting
// removes semicolons at the end of lines
removeSemicolons()
// the Groovy Eclipse formatter extends the Java Eclipse formatter,
// so it formats Java files by default (unless `excludeJava` is used).
greclipse() // has its own section below

licenseHeader('/* (C) $YEAR */') // or licenseHeaderFile

licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
//---- Below is for `groovy` only ----

// excludes all Java sources within the Groovy source dirs from formatting
excludeJava()
}
groovyGradle {
target '*.gradle' // default target of groovyGradle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.diffplug.spotless.extra.EquoBasedStepBuilder;
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
import com.diffplug.spotless.groovy.RemoveSemicolonsStep;
import com.diffplug.spotless.java.ImportOrderStep;

abstract class BaseGroovyExtension extends FormatExtension {
Expand All @@ -40,6 +41,10 @@ public void importOrderFile(Object importOrderFile) {
addStep(ImportOrderStep.forGroovy().createFrom(getProject().file(importOrderFile)));
}

public void removeSemicolons() {
addStep(RemoveSemicolonsStep.create());
}

public GrEclipseConfig greclipse() {
return greclipse(GrEclipseFormatterStep.defaultVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ void excludeJavaWithCustomTarget() throws IOException {
assertThat(error).hasMessageContaining("'excludeJava' is not supported");
}

@Test
void removeSemicolons() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"apply plugin: 'groovy'",
"",
"spotless {",
" groovy {",
" removeSemicolons()",
" }",
"}");

String withSemicolons = getTestResource("groovy/removeSemicolons/GroovyCodeWithSemicolons.test");
String withoutSemicolons = getTestResource("groovy/removeSemicolons/GroovyCodeWithSemicolonsFormatted.test");
setFile("src/main/groovy/test.groovy").toContent(withSemicolons);
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/groovy/test.groovy").hasContent(withoutSemicolons);
}

@Test
void groovyPluginMissingCheck() throws IOException {
setFile("build.gradle").toLines(
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638))
### Changes
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
* Add a Step to remove semicolons from Groovy files. ([#1881](https://github.com/diffplug/spotless/pull/1881))

## [2.40.0] - 2023-09-28
### Added
Expand Down
8 changes: 5 additions & 3 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,16 @@ These mechanisms already exist for the Gradle plugin.
<include>src/test/groovy/**/*.groovy</include>
</includes>

<importOrder /> <!-- standard import order -->
<importOrder/> <!-- standard import order -->
<importOrder> <!-- or a custom ordering -->
<order>java|javax,org,com,com.diffplug,,\#com.diffplug,\#</order> <!-- or use <file>${project.basedir}/eclipse.importorder</file> -->
<!-- you can use an empty string for all the imports you didn't specify explicitly, '|' to join group without blank line, and '\#` prefix for static imports. -->
</importOrder>

<removeSemicolons/> <!-- removes semicolons at the end of lines -->
<greclipse/> <!-- has its own section below -->

<greclipse /> <!-- has its own section below -->

<excludeJava/>
<licenseHeader>
<content>/* (C)$YEAR */</content> <!-- or <file>${project.basedir}/license-header</file> -->
</licenseHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public void addGreclipse(GrEclipse greclipse) {
public void addImportOrder(ImportOrder importOrder) {
addStepFactory(importOrder);
}

public void addRemoveSemicolons(RemoveSemicolons removeSemicolons) {
addStepFactory(removeSemicolons);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2016-2023 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.groovy;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.groovy.RemoveSemicolonsStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class RemoveSemicolons implements FormatterStepFactory {

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return RemoveSemicolonsStep.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023 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.groovy;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

class RemoveSemicolonsTest extends MavenIntegrationHarness {

@Test
void testRemoveSemicolonsString() throws Exception {
writePomWithGroovySteps("<removeSemicolons/>");
runTest("Hello World;", "Hello World");
}

@Test
void testNotRemoveSemicolonsString() throws Exception {
writePomWithGroovySteps("<removeSemicolons/>");
runTest("Hello;World", "Hello;World");
}

@Test
void testRemoveSemicolons() throws Exception {
writePomWithGroovySteps("<removeSemicolons/>");

String path = "src/main/groovy/test.groovy";
setFile(path).toResource("groovy/removeSemicolons/GroovyCodeWithSemicolons.test");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).sameAsResource("groovy/removeSemicolons/GroovyCodeWithSemicolonsFormatted.test");
}

private void runTest(String sourceContent, String targetContent) throws Exception {
String path = "src/main/groovy/test.groovy";
setFile(path).toContent(sourceContent);
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).hasContent(targetContent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mylib.Unused;
import mylib.UsedB;
import mylib.UsedA;

public class SomeClass {
System.out.println("hello");
UsedB.someMethod();
UsedA.someMethod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mylib.Unused
import mylib.UsedB
import mylib.UsedA

public class SomeClass {
System.out.println("hello")
UsedB.someMethod()
UsedA.someMethod()
}
}
Loading