-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a step to remove semicolons in Groovy files (#1900)
- Loading branch information
Showing
13 changed files
with
223 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/RemoveSemicolons.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
plugin-maven/src/test/java/com/diffplug/spotless/maven/groovy/RemoveSemicolonsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
testlib/src/main/resources/groovy/removeSemicolons/GroovyCodeWithSemicolons.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
testlib/src/main/resources/groovy/removeSemicolons/GroovyCodeWithSemicolonsFormatted.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |