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

Added step to remove the semicolons in groovy files #1881

Closed
wants to merge 5 commits into from
Closed
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 @@ -14,6 +14,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 Step to remove semicolons from Groovy code. ([#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,77 @@
/*
* 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 RemoveSemicolonsStep() {
// prevent instantiation
}

static final String NAME = "Remove unnecessary semicolons";

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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
import com.diffplug.spotless.generic.LicenseHeaderStep;
import com.diffplug.spotless.groovy.RemoveSemicolonsStep;
import com.diffplug.spotless.java.ImportOrderStep;

public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang {
Expand Down Expand Up @@ -69,6 +70,10 @@ public void importOrder(String... importOrder) {
addStep(ImportOrderStep.forGroovy().createFrom(importOrder));
}

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

public void importOrderFile(Object importOrderFile) {
Objects.requireNonNull(importOrderFile);
addStep(ImportOrderStep.forGroovy().createFrom(getProject().file(importOrderFile)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* 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.
Expand All @@ -20,6 +20,7 @@
import javax.inject.Inject;

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

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

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

public GroovyExtension.GrEclipseConfig greclipse() {
return new GroovyExtension.GrEclipseConfig(GrEclipseFormatterStep.defaultVersion(), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ 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
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,53 @@
/*
* 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