Skip to content

Commit

Permalink
Remove empty lines after rewrite (#300)
Browse files Browse the repository at this point in the history
* Add test case

* Fix removal of empty JavaDoc

* Remove resulting empty JavaDoc in RemoveEmptyJavaDocParameters

* Slight speedup of RemoveJavaDocAuthorTag

* Add trim()

* Fix trim()

* Add test for Windows EOL

* Apply formatter

* Restore single line no space tests

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
koppor and timtebeek committed Jun 7, 2024
1 parent f6fe18b commit 667ee47
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,48 @@ public Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext ctx)
}

if (useNewBody) {
trim(newBody);
if (newBody.isEmpty()) {
return null;
}
javadoc = javadoc.withBody(newBody);
}
// No need to call super visitor, already covered all cases by adding an empty first element when needed.
return javadoc;
}

/**
* Removes all empty lines from body
*/
private void trim(List<Javadoc> body) {
Javadoc currentDoc;
Javadoc.LineBreak firstLineBreak = null;
while (!body.isEmpty()) {
currentDoc = body.get(body.size() - 1);
boolean isLineBreak = currentDoc instanceof Javadoc.LineBreak;
if (isLineBreak && firstLineBreak == null) {
firstLineBreak = (Javadoc.LineBreak) currentDoc;
}
boolean isEmptyText = false;
if (currentDoc instanceof Javadoc.Text) {
String currentText = ((Javadoc.Text) currentDoc).getText().trim();
isEmptyText = currentText.isEmpty();
}
if (!isLineBreak && !isEmptyText) {
break;
}
body.remove(body.size() - 1);
}
if (!body.isEmpty() && firstLineBreak != null) {
// ensure proper "ending" of JavaDoc including OS-specific newlines
String margin = firstLineBreak.getMargin();
if (margin.endsWith("*")) {
firstLineBreak = firstLineBreak.withMargin(margin.substring(0, margin.length() - 1));
}
body.add(firstLineBreak);
}
}

public boolean isEmptyParameter(Javadoc.Parameter parameter) {
return parameter.getDescription().stream().allMatch(Javadoc.LineBreak.class::isInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext ctx)
}

if (isChanged) {
Collections.reverse(newBody);
if (isBlank(getCursor(), newBody)) {
return null;
}
Collections.reverse(newBody);
dc = dc.withBody(newBody);
}
return dc;
Expand All @@ -87,7 +87,8 @@ static boolean isBlank(Cursor cursor, List<Javadoc> newBody) {
return newBody.stream().allMatch(jd -> {
PrintOutputCapture<Object> p = new PrintOutputCapture<>(null);
jd.printer(cursor).visit(jd, p);
return StringUtils.isBlank(p.getOut());
String currentLine = p.getOut().trim();
return StringUtils.isBlank(currentLine) || "*".equals(currentLine);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ void method(int arg0) {
""",
"""
class Test {
/**
*/
void method(int arg0) {
}
}
Expand All @@ -77,8 +75,6 @@ int method() {
""",
"""
class Test {
/**
*/
int method() {
}
}
Expand All @@ -103,8 +99,6 @@ void method() throws IllegalStateException {
""",
"""
class Test {
/**
*/
void method() throws IllegalStateException {
}
}
Expand Down Expand Up @@ -174,6 +168,69 @@ void method(int arg0, int arg1) {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/98")
void removeTrailingEmptyLines() {
rewriteRun(
//language=java
java(
"""
class Test {
/**
* Text text text
*
* @param arg0
* @param arg1
*
*/
void method(int arg0, int arg1) {
}
}
""",
"""
class Test {
/**
* Text text text
*/
void method(int arg0, int arg1) {
}
}
"""
)
);
}

@Test
void removeTrailingEmptyLinesWindowsEOL() {
rewriteRun(
//language=java
java(
"""
class Test {
/**
* Text text text
*
* @param arg0
* @param arg1
*
*/
void method(int arg0, int arg1) {
}
}
""".replace("\n", "\r\n"),
"""
class Test {
/**
* Text text text
*/
void method(int arg0, int arg1) {
}
}
""".replace("\n", "\r\n")
)
);
}

@Nested
class NoSpace {
@Test
Expand All @@ -192,8 +249,6 @@ void method(int arg0) {
""",
"""
class Test {
/**
*/
void method(int arg0) {
}
}
Expand All @@ -218,8 +273,6 @@ int method() {
""",
"""
class Test {
/**
*/
int method() {
}
}
Expand All @@ -244,8 +297,6 @@ void method() throws IllegalStateException {
""",
"""
class Test {
/**
*/
void method() throws IllegalStateException {
}
}
Expand All @@ -272,7 +323,6 @@ void method(int arg0) {
""",
"""
class Test {
/***/
void method(int arg0) {
}
}
Expand All @@ -295,7 +345,6 @@ int method() {
""",
"""
class Test {
/***/
int method() {
}
}
Expand All @@ -318,7 +367,6 @@ void method() throws IllegalStateException {
""",
"""
class Test {
/***/
void method() throws IllegalStateException {
}
}
Expand All @@ -341,7 +389,6 @@ void method(int arg0) {
""",
"""
class Test {
/***/
void method(int arg0) {
}
}
Expand All @@ -364,7 +411,6 @@ int method() {
""",
"""
class Test {
/***/
int method() {
}
}
Expand All @@ -387,7 +433,6 @@ void method() throws IllegalStateException {
""",
"""
class Test {
/***/
void method() throws IllegalStateException {
}
}
Expand All @@ -414,8 +459,6 @@ void method(int arg0) {
""",
"""
class Test {
/**
*/
void method(int arg0) {
}
}
Expand All @@ -439,8 +482,6 @@ int method() {
""",
"""
class Test {
/**
*/
int method() {
}
}
Expand All @@ -464,8 +505,6 @@ int method() throws IllegalStateException {
""",
"""
class Test {
/**
*/
int method() throws IllegalStateException {
}
}
Expand All @@ -489,8 +528,6 @@ void method(int arg0) {
""",
"""
class Test {
/**
*/
void method(int arg0) {
}
}
Expand All @@ -514,8 +551,6 @@ int method() {
""",
"""
class Test {
/**
*/
int method() {
}
}
Expand All @@ -539,8 +574,6 @@ int method() throws IllegalStateException {
""",
"""
class Test {
/**
*/
int method() throws IllegalStateException {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Test {}
)
);
}
@Test

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/119")
@Test
void tagOnSecondLine() {
rewriteRun(
//language=java
Expand All @@ -65,6 +66,27 @@ class Test {}
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/119")
@Test
void tagOnSecondLineSourroundedByEmptyLines() {
rewriteRun(
//language=java
java(
"""
/**
*
* @author foo.bar
*
*/
class Test {}
""",
"""
class Test {}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1640")
@Test
@DocumentExample
Expand Down

0 comments on commit 667ee47

Please sign in to comment.