Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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,63 @@
package com.diffplug.spotless.groovy;

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

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

/**
* Removes all semicolons from the end of lines.
*
* @author Jose Luis Badano
*/
public final class RemoveSemiColonsStep {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rename these RenameSemiColons* to RenameSemicolons*

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense for me that name.
The idea of this step is to remove the semicolons at the end of the lines in the groovy files, since they are optional.
If change the name to RenameSemiColons it would be harder to understand the purpose of the step by reading only the name.

WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant rename all SemiColons to Semicolons, like RemoveSemiColonsStep to RemoveSemicolonsStep, cause semicolon is a complete word.

https://en.wikipedia.org/wiki/Semicolon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh!! Yeah, got it. Yep, let me do that change quickly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done -> 5781dea


private RemoveSemiColonsStep() {
// prevent instantiation
}

static final String NAME = "Remove not needed semi colons";

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 @@ -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,32 @@
/*
* 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.groovy.RemoveSemiColonsStep;


import com.diffplug.spotless.FormatterStep;
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,38 @@
package com.diffplug.spotless.maven.groovy;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

import org.junit.jupiter.api.Test;

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()
}
}