Skip to content

Commit

Permalink
SONARJAVA-1396 IndentationCheck: should handle lambdas having block a…
Browse files Browse the repository at this point in the history
…s body
  • Loading branch information
mpaladin committed Dec 24, 2015
1 parent 0207dd0 commit ec579af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.sonar.plugins.java.api.tree.CaseGroupTree;
import org.sonar.plugins.java.api.tree.CaseLabelTree;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.StatementTree;
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.Tree;
Expand Down Expand Up @@ -64,7 +65,8 @@ public class IndentationCheck extends SubscriptionBaseVisitor {
Kind.STATIC_INITIALIZER,
Kind.INITIALIZER,
Kind.SWITCH_STATEMENT,
Kind.CASE_GROUP
Kind.CASE_GROUP,
Kind.METHOD_INVOCATION
);

private static final int DEFAULT_INDENTATION_LEVEL = 2;
Expand Down Expand Up @@ -103,6 +105,9 @@ public void visitNode(Tree tree) {
if (!isInAnonymousClass.peek()) {
checkIndentation(Collections.singletonList(classTree));
}
} else if (tree.is(Kind.METHOD_INVOCATION)) {
adjustMethodInvocation((MethodInvocationTree) tree);
return;
}
expectedLevel += indentationLevel;
isBlockAlreadyReported = false;
Expand All @@ -125,6 +130,22 @@ public void visitNode(Tree tree) {
}
}

private void adjustMethodInvocation(MethodInvocationTree tree) {
int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line();
int parenthesisLine = tree.arguments().openParenToken().line();
if (startLine != parenthesisLine) {
expectedLevel += indentationLevel;
}
}

private void restoreMethodInvocation(MethodInvocationTree tree) {
int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line();
int parenthesisLine = tree.arguments().openParenToken().line();
if (startLine != parenthesisLine) {
expectedLevel -= indentationLevel;
}
}

private void checkClass(ClassTree classTree) {
// Exclude anonymous classes
if (classTree.simpleName() != null) {
Expand Down Expand Up @@ -160,16 +181,12 @@ private void checkCaseGroup(CaseGroupTree tree) {
private void adjustBlockForExceptionalParents(Tree parent) {
if (parent.is(Kind.CASE_GROUP)) {
expectedLevel -= indentationLevel;
} else if (parent.is(Kind.LAMBDA_EXPRESSION)) {
expectedLevel += indentationLevel;
}
}

private void restoreBlockForExceptionalParents(Tree parent) {
if (parent.is(Kind.CASE_GROUP)) {
expectedLevel += indentationLevel;
} else if (parent.is(Kind.LAMBDA_EXPRESSION)) {
expectedLevel -= indentationLevel;
}
}

Expand All @@ -190,7 +207,10 @@ private void checkIndentation(Tree tree, int expectedLevel) {

@Override
public void leaveNode(Tree tree) {
if (tree.is(Kind.BLOCK)) {
if (tree.is(Kind.METHOD_INVOCATION)) {
restoreMethodInvocation((MethodInvocationTree) tree);
return;
} else if (tree.is(Kind.BLOCK)) {
restoreBlockForExceptionalParents(tree.parent());
}
expectedLevel -= indentationLevel;
Expand Down
41 changes: 38 additions & 3 deletions java-checks/src/test/files/checks/IndentationCheck_default.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Foo {
void foo() {
IntStream
.range(1, 5)
.map(a -> {
return a + 1; // should be a valid position
});
.map((a -> {
return a + 1;
}));
IntStream
.range(1, 5)
.map(a -> {
Expand All @@ -53,6 +53,9 @@ void foo() {
a += 1; // Noncompliant {{Make this line start at column 9.}}
return a + 1;
});
IntStream.range(1, 5).map(a -> {
return a + 1;
});
}
}

Expand Down Expand Up @@ -157,3 +160,35 @@ public static class Inner {
public static final String FOO = "foo";
}
}

class IndentFoo {
public static boolean showJobDifferences(final String name, final JobsDifference.JobDifference diff, boolean onlyOkButton) {
FutureTask<Boolean> task = new FutureTask<>(() -> {
Optional<ButtonType> result = new DifferenceDialog(name, diff, onlyOkButton).showAndWait();
if (result.isPresent() && (result.get() == DifferenceDialog.ACTION_YES_ALL || result.get() == DifferenceDialog.ACTION_OK_ALL)) {
skipDialog = true;
}
return result.get() == DifferenceDialog.ACTION_YES || result.get() == DifferenceDialog.ACTION_YES_ALL;
});
}

@Override
protected void append(final ILoggingEvent event) {
synchronized (lock) {
Platform.runLater(() -> {
Text text = new Text();
text.setText(patternLayout.doLayout(event));
switch (event.getLevel().levelInt) {
case Level.DEBUG_INT:
text.setFill(Color.BLACK);
break;
default:
text.setFill(Color.BLACK);
break;
}
listView.getItems().add(text);
});
}
}

}

0 comments on commit ec579af

Please sign in to comment.