Skip to content

Commit ec579af

Browse files
committed
SONARJAVA-1396 IndentationCheck: should handle lambdas having block as body
1 parent 0207dd0 commit ec579af

2 files changed

Lines changed: 64 additions & 9 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/IndentationCheck.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.sonar.plugins.java.api.tree.CaseGroupTree;
3434
import org.sonar.plugins.java.api.tree.CaseLabelTree;
3535
import org.sonar.plugins.java.api.tree.ClassTree;
36+
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
3637
import org.sonar.plugins.java.api.tree.StatementTree;
3738
import org.sonar.plugins.java.api.tree.SyntaxToken;
3839
import org.sonar.plugins.java.api.tree.Tree;
@@ -64,7 +65,8 @@ public class IndentationCheck extends SubscriptionBaseVisitor {
6465
Kind.STATIC_INITIALIZER,
6566
Kind.INITIALIZER,
6667
Kind.SWITCH_STATEMENT,
67-
Kind.CASE_GROUP
68+
Kind.CASE_GROUP,
69+
Kind.METHOD_INVOCATION
6870
);
6971

7072
private static final int DEFAULT_INDENTATION_LEVEL = 2;
@@ -103,6 +105,9 @@ public void visitNode(Tree tree) {
103105
if (!isInAnonymousClass.peek()) {
104106
checkIndentation(Collections.singletonList(classTree));
105107
}
108+
} else if (tree.is(Kind.METHOD_INVOCATION)) {
109+
adjustMethodInvocation((MethodInvocationTree) tree);
110+
return;
106111
}
107112
expectedLevel += indentationLevel;
108113
isBlockAlreadyReported = false;
@@ -125,6 +130,22 @@ public void visitNode(Tree tree) {
125130
}
126131
}
127132

133+
private void adjustMethodInvocation(MethodInvocationTree tree) {
134+
int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line();
135+
int parenthesisLine = tree.arguments().openParenToken().line();
136+
if (startLine != parenthesisLine) {
137+
expectedLevel += indentationLevel;
138+
}
139+
}
140+
141+
private void restoreMethodInvocation(MethodInvocationTree tree) {
142+
int startLine = FirstSyntaxTokenFinder.firstSyntaxToken(tree).line();
143+
int parenthesisLine = tree.arguments().openParenToken().line();
144+
if (startLine != parenthesisLine) {
145+
expectedLevel -= indentationLevel;
146+
}
147+
}
148+
128149
private void checkClass(ClassTree classTree) {
129150
// Exclude anonymous classes
130151
if (classTree.simpleName() != null) {
@@ -160,16 +181,12 @@ private void checkCaseGroup(CaseGroupTree tree) {
160181
private void adjustBlockForExceptionalParents(Tree parent) {
161182
if (parent.is(Kind.CASE_GROUP)) {
162183
expectedLevel -= indentationLevel;
163-
} else if (parent.is(Kind.LAMBDA_EXPRESSION)) {
164-
expectedLevel += indentationLevel;
165184
}
166185
}
167186

168187
private void restoreBlockForExceptionalParents(Tree parent) {
169188
if (parent.is(Kind.CASE_GROUP)) {
170189
expectedLevel += indentationLevel;
171-
} else if (parent.is(Kind.LAMBDA_EXPRESSION)) {
172-
expectedLevel -= indentationLevel;
173190
}
174191
}
175192

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

191208
@Override
192209
public void leaveNode(Tree tree) {
193-
if (tree.is(Kind.BLOCK)) {
210+
if (tree.is(Kind.METHOD_INVOCATION)) {
211+
restoreMethodInvocation((MethodInvocationTree) tree);
212+
return;
213+
} else if (tree.is(Kind.BLOCK)) {
194214
restoreBlockForExceptionalParents(tree.parent());
195215
}
196216
expectedLevel -= indentationLevel;

java-checks/src/test/files/checks/IndentationCheck_default.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class Foo {
4141
void foo() {
4242
IntStream
4343
.range(1, 5)
44-
.map(a -> {
45-
return a + 1; // should be a valid position
46-
});
44+
.map((a -> {
45+
return a + 1;
46+
}));
4747
IntStream
4848
.range(1, 5)
4949
.map(a -> {
@@ -53,6 +53,9 @@ void foo() {
5353
a += 1; // Noncompliant {{Make this line start at column 9.}}
5454
return a + 1;
5555
});
56+
IntStream.range(1, 5).map(a -> {
57+
return a + 1;
58+
});
5659
}
5760
}
5861

@@ -157,3 +160,35 @@ public static class Inner {
157160
public static final String FOO = "foo";
158161
}
159162
}
163+
164+
class IndentFoo {
165+
public static boolean showJobDifferences(final String name, final JobsDifference.JobDifference diff, boolean onlyOkButton) {
166+
FutureTask<Boolean> task = new FutureTask<>(() -> {
167+
Optional<ButtonType> result = new DifferenceDialog(name, diff, onlyOkButton).showAndWait();
168+
if (result.isPresent() && (result.get() == DifferenceDialog.ACTION_YES_ALL || result.get() == DifferenceDialog.ACTION_OK_ALL)) {
169+
skipDialog = true;
170+
}
171+
return result.get() == DifferenceDialog.ACTION_YES || result.get() == DifferenceDialog.ACTION_YES_ALL;
172+
});
173+
}
174+
175+
@Override
176+
protected void append(final ILoggingEvent event) {
177+
synchronized (lock) {
178+
Platform.runLater(() -> {
179+
Text text = new Text();
180+
text.setText(patternLayout.doLayout(event));
181+
switch (event.getLevel().levelInt) {
182+
case Level.DEBUG_INT:
183+
text.setFill(Color.BLACK);
184+
break;
185+
default:
186+
text.setFill(Color.BLACK);
187+
break;
188+
}
189+
listView.getItems().add(text);
190+
});
191+
}
192+
}
193+
194+
}

0 commit comments

Comments
 (0)