Skip to content

Commit

Permalink
AutoFormat better handles type parameters (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
SofiaBrittoSchwartz authored Nov 19, 2020
1 parent decf842 commit 3913ffb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ public J.MethodDecl visitMethod(J.MethodDecl method) {

String prefix;
if(m.getAnnotations().isEmpty()) {
if(m.getModifiers().isEmpty()) {
prefix = (m.getReturnTypeExpr() == null ? m.getName() : m.getReturnTypeExpr()).getPrefix();
} else {
prefix = firstPrefix(m.getModifiers());
if(m.getTypeParameters() == null) {
if(m.getModifiers().isEmpty()) {
prefix = (m.getReturnTypeExpr() == null ? m.getName() : m.getReturnTypeExpr()).getPrefix();
} else {
prefix = firstPrefix(m.getModifiers());
}
}
else {
prefix = m.getTypeParameters().getPrefix();
}
} else {
prefix = firstPrefix(m.getAnnotations());
Expand Down
29 changes: 21 additions & 8 deletions rewrite-java/src/main/java/org/openrewrite/java/AutoFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,30 @@ private class FixNewlines extends JavaIsoRefactorVisitor {
*
* Right now comments aren't AST elements so FixIndentation.visitTree() is blind to them.
* This special treatment of comment indentation will be able to go away once we start treating
* comments an their own, independent AST element.
* comments as their own, independent AST element.
*/
public String indentLine(String prefix) {
if (!prefix.isEmpty() && stream(scope).anyMatch(s -> getCursor().isScopeInPath(s))) {
int indentMultiple = (int) getCursor().getPathAsStream().filter(J.Block.class::isInstance).count();
Formatter.Result wholeSourceIndent = formatter.wholeSourceIndent();
int nonWhiteSpaceIndex = StringUtils.indexOfNonWhitespace(prefix);
boolean insideJavaDocComment = false;

if(nonWhiteSpaceIndex == -1) {
nonWhiteSpaceIndex = prefix.length();
} else if(prefix.charAt(nonWhiteSpaceIndex) == '*')
{
} else if(prefix.charAt(nonWhiteSpaceIndex) == '*') {
insideJavaDocComment = true;
}

String indentation = prefix.substring(0, nonWhiteSpaceIndex);
String comment = prefix.substring(nonWhiteSpaceIndex);
String newIndentation = indentation.substring(0, indentation.lastIndexOf('\n') + 1) + range(0, indentMultiple * wholeSourceIndent.getIndentToUse())
.mapToObj(n -> wholeSourceIndent.isIndentedWithSpaces() ? " " : "\t")
.collect(Collectors.joining(""));

// +1 because for String.substring(start, end) start is inclusive, but end is exclusive
String newIndentation = indentation.substring(0, indentation.lastIndexOf('\n') + 1) +
range(0, indentMultiple * wholeSourceIndent.getIndentToUse())
.mapToObj(n -> wholeSourceIndent.isIndentedWithSpaces() ? " " : "\t")
.collect(Collectors.joining(""));

if(insideJavaDocComment) //noinspection DanglingJavadoc
{
// By convention Javadoc-style comments are indented such that lines beginning with '*'
Expand Down Expand Up @@ -169,9 +174,17 @@ public J.MethodDecl visitMethod(J.MethodDecl methodDecl) {
m = m.withModifiers(modifiers);
}
}
// typeParameters
else if(m.getTypeParameters() != null) {
if(!m.getTypeParameters().getPrefix().contains("\n")) {
m = m.withTypeParameters(m.getTypeParameters().withPrefix("\n"));
}
}
// returnTypeExpr
else if(m.getReturnTypeExpr() != null && !m.getReturnTypeExpr().getPrefix().contains("\n")) {
m = m.withReturnTypeExpr(m.getReturnTypeExpr().withPrefix("\n"));
else if(m.getReturnTypeExpr() != null) {
if(!m.getReturnTypeExpr().getPrefix().contains("\n")) {
m = m.withReturnTypeExpr(m.getReturnTypeExpr().withPrefix("\n"));
}
}
// name
else if(!m.getName().getPrefix().contains("\n")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.java

import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.openrewrite.RefactorVisitorTest
import org.openrewrite.java.tree.J
Expand Down Expand Up @@ -86,7 +85,6 @@ interface AddAnnotationTest : RefactorVisitorTest {
"""
)

@Disabled("https://github.com/openrewrite/rewrite/issues/64")
@Test
fun addAnnotationToMethod(jp: JavaParser) = assertRefactored(
jp,
Expand Down Expand Up @@ -118,6 +116,7 @@ interface AddAnnotationTest : RefactorVisitorTest {
import b.MyAnnotation;
public class UsersController {
@MyAnnotation
UsersController() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,30 @@ interface AutoFormatTest : RefactorVisitorTest {
"""
)

@Test
fun putMethodAnnotationAndTypeParametersOnSeparateLines(jp: JavaParser) = assertRefactored(
jp,
dependencies = dependencies,
visitorsMapped = listOf { a ->
AutoFormat(a.classes[0].methods[0])
},
before = """
public class D {
@A <T> T onInit() {
}
}
""",
after = """
public class D {
@A
<T> T onInit() {
}
}
"""
)

@Test
fun putMethodAnnotationAndReturnTypeOnSeparateLines(jp: JavaParser) = assertRefactored(
jp,
Expand Down

0 comments on commit 3913ffb

Please sign in to comment.