1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.puppycrawl.tools.checkstyle.checks.indentation;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24
25
26
27
28
29 public class MethodCallHandler extends ExpressionHandler
30 {
31
32
33
34
35
36
37
38
39 public MethodCallHandler(IndentationCheck indentCheck,
40 DetailAST ast, ExpressionHandler parent)
41 {
42 super(indentCheck,
43 ast.getType() == TokenTypes.METHOD_CALL
44 ? "method call" : "ctor call",
45 ast,
46 parent);
47 }
48
49 @Override
50 protected IndentLevel getLevelImpl()
51 {
52
53
54 if (getParent() instanceof MethodCallHandler) {
55 final MethodCallHandler container =
56 (MethodCallHandler) getParent();
57 if (container != null) {
58 if (areOnSameLine(container.getMainAst(), getMainAst())) {
59 return container.getLevel();
60 }
61
62
63
64 final DetailAST main = getMainAst();
65 final DetailAST dot = main.getFirstChild();
66 final DetailAST target = dot.getFirstChild();
67
68 if (dot.getType() == TokenTypes.DOT
69 && target.getType() == TokenTypes.METHOD_CALL)
70 {
71 final DetailAST dot1 = target.getFirstChild();
72 final DetailAST target1 = dot1.getFirstChild();
73
74 if (dot1.getType() == TokenTypes.DOT
75 && target1.getType() == TokenTypes.METHOD_CALL)
76 {
77 return container.getLevel();
78 }
79 }
80 return new IndentLevel(container.getLevel(), getBasicOffset());
81 }
82
83
84
85
86
87 ExpressionHandler p = getParent();
88 while (p instanceof MethodCallHandler) {
89 p = p.getParent();
90 }
91 return p.suggestedChildLevel(this);
92 }
93
94
95
96 final LineSet lines = new LineSet();
97 findSubtreeLines(lines, getMainAst().getFirstChild(), true);
98 final int firstCol = lines.firstLineCol();
99 final int lineStart = getLineStart(getFirstAst(getMainAst()));
100 if (lineStart != firstCol) {
101 return new IndentLevel(lineStart);
102 }
103 return super.getLevelImpl();
104 }
105
106
107
108
109
110
111
112
113
114 private DetailAST getFirstAst(DetailAST ast)
115 {
116
117
118
119 DetailAST astNode = ast.getFirstChild();
120 while (astNode != null && astNode.getType() == TokenTypes.DOT) {
121 astNode = astNode.getFirstChild();
122 }
123
124 if (astNode == null) {
125 astNode = ast;
126 }
127
128 return astNode;
129 }
130
131 @Override
132 public IndentLevel suggestedChildLevel(ExpressionHandler child)
133 {
134
135
136
137
138
139
140 final DetailAST first = getMainAst().getFirstChild();
141 int indentLevel = getLineStart(first);
142 if (!areOnSameLine(child.getMainAst().getFirstChild(),
143 getMainAst().getFirstChild()))
144 {
145 indentLevel += getBasicOffset();
146 }
147 return new IndentLevel(indentLevel);
148 }
149
150 @Override
151 public void checkIndentation()
152 {
153 final DetailAST exprNode = getMainAst().getParent();
154 if (exprNode.getParent().getType() != TokenTypes.LCURLY
155 && exprNode.getParent().getType() != TokenTypes.SLIST)
156 {
157 return;
158 }
159 final DetailAST methodName = getMainAst().getFirstChild();
160 checkExpressionSubtree(methodName, getLevel(), false, false);
161
162 final DetailAST lparen = getMainAst();
163 final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN);
164 checkLParen(lparen);
165
166 if (rparen.getLineNo() == lparen.getLineNo()) {
167 return;
168 }
169
170 checkExpressionSubtree(
171 getMainAst().findFirstToken(TokenTypes.ELIST),
172 new IndentLevel(getLevel(), getBasicOffset()),
173 false, true);
174
175 checkRParen(lparen, rparen);
176 final LineWrappingHandler lineWrap =
177 new LineWrappingHandler(getIndentCheck(), getMainAst(),
178 getMethodCallLastNode(getMainAst()));
179 lineWrap.checkIndentation();
180 }
181
182 @Override
183 protected boolean shouldIncreaseIndent()
184 {
185 return false;
186 }
187
188
189
190
191
192
193
194
195 private static DetailAST getMethodCallLastNode(DetailAST firstNode)
196 {
197 DetailAST lastNode;
198 if (firstNode.getNextSibling() == null) {
199 lastNode = firstNode.getLastChild();
200 }
201 else {
202 lastNode = firstNode.getNextSibling();
203 }
204 return lastNode;
205 }
206 }