-
Notifications
You must be signed in to change notification settings - Fork 484
Added step to remove the semicolons in groovy files #1881
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfe12e3
Added step to remove the semicolons in groovy files
jlbadano 38b74e2
Updated readme
jlbadano 1f45cf0
Update lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemiColon…
jlbadano 0df8980
Added gradle support
jlbadano 5781dea
Renamed SemiColons to Semicolons
jlbadano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemiColonsStep.java
This file contains hidden or 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,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 { | ||
|
||
private RemoveSemiColonsStep() { | ||
// prevent instantiation | ||
} | ||
|
||
static final String NAME = "Remove not needed semi colons"; | ||
jlbadano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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
32 changes: 32 additions & 0 deletions
32
plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/RemoveSemiColons.java
This file contains hidden or 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,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(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
plugin-maven/src/test/java/com/diffplug/spotless/maven/groovy/RemoveSemiColonsTest.java
This file contains hidden or 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,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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
testlib/src/main/resources/groovy/removesemicolons/GroovyCodeWithSemiColons.test
This file contains hidden or 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 hidden or 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() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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*
toRenameSemicolons*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
toSemicolons
, likeRemoveSemiColonsStep
toRemoveSemicolonsStep
, causesemicolon
is a complete word.https://en.wikipedia.org/wiki/Semicolon
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done -> 5781dea