Skip to content

Commit 671cc98

Browse files
jamezpctubbsii
andauthored
Add a parameter to remove trailing whitespace (#521)
This fixes #520 Co-authored-by: Christopher Tubbs <[email protected]>
1 parent 9dbdf65 commit 671cc98

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/net/revelc/code/formatter/FormatterMojo.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Properties;
34+
import java.util.regex.Pattern;
3435
import java.util.stream.Collectors;
3536

3637
import org.apache.maven.plugin.AbstractMojo;
@@ -89,6 +90,8 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
8990
private static final String[] DEFAULT_INCLUDES = new String[] { "**/*.css", "**/*.json", "**/*.html", "**/*.java",
9091
"**/*.js", "**/*.xml" };
9192

93+
private static final Pattern REMOVE_TRAILING_PATTERN = Pattern.compile("\\p{Blank}+$", Pattern.MULTILINE);
94+
9295
/**
9396
* ResourceManager for retrieving the configFile resource.
9497
*/
@@ -328,6 +331,13 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
328331
@Parameter(property = "formatter.java.exclusion_pattern")
329332
private String javaExclusionPattern;
330333

334+
/**
335+
* When set to true, remove trailing whitespace on all lines after the formatter has finished.
336+
*
337+
* @since 2.17.0
338+
*/
339+
@Parameter(defaultValue = "false", property = "formatter.removeTrailingWhitespace")
340+
private boolean removeTrailingWhitespace;
331341
private JavaFormatter javaFormatter = new JavaFormatter();
332342

333343
private JavascriptFormatter jsFormatter = new JavascriptFormatter();
@@ -652,6 +662,11 @@ protected void doFormatFile(File file, ResultCollector rc, Properties hashCache,
652662
return;
653663
}
654664

665+
// Process the source one more time and remove any trailing whitespace found
666+
if (removeTrailingWhitespace && formattedCode != null) {
667+
formattedCode = REMOVE_TRAILING_PATTERN.matcher(formattedCode).replaceAll("");
668+
}
669+
655670
// Write the cache
656671
String formattedHash;
657672
if (Result.SKIPPED.equals(result)) {

0 commit comments

Comments
 (0)