Skip to content

Commit

Permalink
improved Autodetect interpretation of annotations for continuationInd…
Browse files Browse the repository at this point in the history
…ent.

fixes #3568
  • Loading branch information
nmck257 committed Sep 27, 2023
1 parent b863972 commit ac6d3f2
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.openrewrite.java.style;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.style.GeneralFormatStyle;
Expand All @@ -25,7 +27,7 @@
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored"})
@SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored", "PointlessBooleanExpression"})
class AutodetectTest implements RewriteTest {

private static JavaParser jp() {
Expand Down Expand Up @@ -1045,4 +1047,122 @@ public void cont() {
assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent()).isEqualTo(12);
}

@Nested
class ContinuationIndentForAnnotations {
@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3568")
void ignoreSpaceBetweenAnnotations() {
var cus = jp().parse(
"""
class Test {
@SafeVarargs
@Deprecated
@SuppressWarnings({"mistakes"})
boolean count(String... strings) {
return strings.length;
}
}
"""
);

var detector = Autodetect.detector();
cus.forEach(detector::sample);
var styles = detector.build();
var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));

assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent())
.as("With no actual continuation indents to go off of, assume IntelliJ IDEA default of 2x the normal indent")
.isEqualTo(8);
}

@Test
void includeAnnotationAsAnnotationArg() {
var cus = jp().parse(
"""
@interface Foo{}
@interface Foos{
Foo[] value();
}
class Test {
@Foos(
@Foo)
boolean count(String... strings) {
return strings.length;
}
}
"""
);

var detector = Autodetect.detector();
cus.forEach(detector::sample);
var styles = detector.build();
var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));

assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent())
.isEqualTo(3);
}

@Test
void includeAnnotationArgArray() {
var cus = jp().parse(
"""
@interface Foo{}
@interface Foos{
Foo[] value();
}
class Test {
@Foos(
{@Foo})
boolean count(String... strings) {
return strings.length;
}
}
"""
);

var detector = Autodetect.detector();
cus.forEach(detector::sample);
var styles = detector.build();
var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));

assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent())
.isEqualTo(3);
}

@Test
@ExpectedToFail("existing visitor does not super-visit newArray trees")
void includeAnnotationArgArrayElements() {
var cus = jp().parse(
"""
@interface Foo{}
@interface Foos{
Foo[] value();
}
class Test {
@Foos({
@Foo})
boolean count(String... strings) {
return strings.length;
}
}
"""
);

var detector = Autodetect.detector();
cus.forEach(detector::sample);
var styles = detector.build();
var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));

assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent())
.isEqualTo(3);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,13 @@ public Expression visitExpression(Expression expression, IndentStatistics stats)
if (statementExpressions.contains(expression)) {
return expression;
}
countIndents(expression.getPrefix().getWhitespace(), true, stats);
// (newline-separated) annotations on some common target are not continuations
boolean isContinuation = !(expression instanceof J.Annotation && !(
// ...but annotations which are *arguments* to other annotations can be continuations
getCursor().getParentTreeCursor().getValue() instanceof J.Annotation
|| getCursor().getParentTreeCursor().getValue() instanceof J.NewArray
));
countIndents(expression.getPrefix().getWhitespace(), isContinuation, stats);

return expression;
}
Expand Down

0 comments on commit ac6d3f2

Please sign in to comment.