3333import org .sonar .plugins .java .api .tree .CaseGroupTree ;
3434import org .sonar .plugins .java .api .tree .CaseLabelTree ;
3535import org .sonar .plugins .java .api .tree .ClassTree ;
36+ import org .sonar .plugins .java .api .tree .MethodInvocationTree ;
3637import org .sonar .plugins .java .api .tree .StatementTree ;
3738import org .sonar .plugins .java .api .tree .SyntaxToken ;
3839import 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 ;
0 commit comments