1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class BlockParentHandler extends AbstractExpressionHandler {
45
46
47
48
49 private static final int[] CHECKED_CHILDREN = {
50 TokenTypes.VARIABLE_DEF,
51 TokenTypes.EXPR,
52 TokenTypes.ANNOTATION,
53 TokenTypes.OBJBLOCK,
54 TokenTypes.LITERAL_BREAK,
55 TokenTypes.LITERAL_RETURN,
56 TokenTypes.LITERAL_THROW,
57 TokenTypes.LITERAL_CONTINUE,
58 TokenTypes.CTOR_CALL,
59 TokenTypes.SUPER_CTOR_CALL,
60 };
61
62
63
64
65
66
67
68
69
70
71
72
73 public BlockParentHandler(IndentationCheck indentCheck,
74 String name, DetailAST ast, AbstractExpressionHandler parent) {
75 super(indentCheck, name, ast, parent);
76 }
77
78
79
80
81
82
83 protected int[] getCheckedChildren() {
84 return CHECKED_CHILDREN.clone();
85 }
86
87
88
89
90
91
92 protected DetailAST getTopLevelAst() {
93 return getMainAst();
94 }
95
96
97
98
99 protected void checkTopLevelToken() {
100 final DetailAST topLevel = getTopLevelAst();
101
102 if (topLevel != null
103 && !getIndent().isAcceptable(expandedTabsColumnNo(topLevel))
104 && isOnStartOfLine(topLevel)) {
105 logError(topLevel, "", expandedTabsColumnNo(topLevel));
106 }
107 }
108
109
110
111
112
113
114 private boolean hasCurlies() {
115 return getLeftCurly() != null && getRightCurly() != null;
116 }
117
118
119
120
121
122
123 protected DetailAST getLeftCurly() {
124 return getMainAst().findFirstToken(TokenTypes.SLIST);
125 }
126
127
128
129
130
131
132 protected DetailAST getRightCurly() {
133 final DetailAST slist = getMainAst().findFirstToken(TokenTypes.SLIST);
134 return slist.findFirstToken(TokenTypes.RCURLY);
135 }
136
137
138
139
140 private void checkLeftCurly() {
141
142
143 final DetailAST lcurly = getLeftCurly();
144 final int lcurlyPos = expandedTabsColumnNo(lcurly);
145
146 if (!curlyIndent().isAcceptable(lcurlyPos) && isOnStartOfLine(lcurly)) {
147 logError(lcurly, "lcurly", lcurlyPos, curlyIndent());
148 }
149 }
150
151
152
153
154
155
156 protected IndentLevel curlyIndent() {
157 final DetailAST lcurly = getLeftCurly();
158 IndentLevel expIndentLevel = new IndentLevel(getIndent(), getBraceAdjustment());
159 if (!isOnStartOfLine(lcurly)
160 || lcurly.getParent().getType() == TokenTypes.INSTANCE_INIT) {
161 expIndentLevel = new IndentLevel(getIndent(), 0);
162 }
163
164 return expIndentLevel;
165 }
166
167
168
169
170
171
172 protected boolean canChildrenBeNested() {
173 return false;
174 }
175
176
177
178
179 private void checkRightCurly() {
180 final DetailAST rcurly = getRightCurly();
181 final int rcurlyPos = expandedTabsColumnNo(rcurly);
182
183 if (!curlyIndent().isAcceptable(rcurlyPos)
184 && isOnStartOfLine(rcurly)) {
185 logError(rcurly, "rcurly", rcurlyPos, curlyIndent());
186 }
187 }
188
189
190
191
192
193
194 protected DetailAST getNonListChild() {
195 return getMainAst().findFirstToken(TokenTypes.RPAREN).getNextSibling();
196 }
197
198
199
200
201 private void checkNonListChild() {
202 final DetailAST nonList = getNonListChild();
203 if (nonList != null) {
204 final IndentLevel expected = new IndentLevel(getIndent(), getBasicOffset());
205 checkExpressionSubtree(nonList, expected, false, false);
206
207 final DetailAST nonListStartAst = getFirstAstNode(nonList);
208 if (nonList != nonListStartAst) {
209 checkExpressionSubtree(nonListStartAst, expected, false, false);
210 }
211 }
212 }
213
214
215
216
217
218
219 protected DetailAST getListChild() {
220 return getMainAst().findFirstToken(TokenTypes.SLIST);
221 }
222
223
224
225
226
227
228 private DetailAST getRightParen() {
229 return getMainAst().findFirstToken(TokenTypes.RPAREN);
230 }
231
232
233
234
235
236
237 private DetailAST getLeftParen() {
238 return getMainAst().findFirstToken(TokenTypes.LPAREN);
239 }
240
241 @Override
242 public void checkIndentation() {
243 checkTopLevelToken();
244
245 checkLeftParen(getLeftParen());
246 checkRightParen(getLeftParen(), getRightParen());
247 if (hasCurlies()) {
248 checkLeftCurly();
249 checkRightCurly();
250 }
251 final DetailAST listChild = getListChild();
252 if (listChild == null) {
253 checkNonListChild();
254 }
255 else {
256
257 if (!hasCurlies() || !TokenUtil.areOnSameLine(getLeftCurly(), getRightCurly())) {
258 checkChildren(listChild,
259 getCheckedChildren(),
260 getChildrenExpectedIndent(),
261 true,
262 canChildrenBeNested());
263 }
264 }
265 }
266
267
268
269
270
271
272 protected IndentLevel getChildrenExpectedIndent() {
273 IndentLevel indentLevel = new IndentLevel(getIndent(), getBasicOffset());
274
275
276
277 if (getIndent().isMultiLevel() && hasCurlies()) {
278 if (isOnStartOfLine(getLeftCurly())) {
279 indentLevel = new IndentLevel(expandedTabsColumnNo(getLeftCurly())
280 + getBasicOffset());
281 }
282 else if (isOnStartOfLine(getRightCurly())) {
283 final IndentLevel level = new IndentLevel(curlyIndent(), getBasicOffset());
284 indentLevel = IndentLevel.addAcceptable(level, level.getFirstIndentLevel()
285 + getLineWrappingIndent());
286 }
287 }
288 if (hasCurlies() && isOnStartOfLine(getLeftCurly())) {
289 indentLevel = IndentLevel.addAcceptable(indentLevel,
290 curlyIndent().getFirstIndentLevel() + getBasicOffset());
291 }
292 return indentLevel;
293 }
294
295 @Override
296 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
297 return getChildrenExpectedIndent();
298 }
299
300
301
302
303
304
305
306 private int getLineWrappingIndent() {
307 return getIndentCheck().getLineWrappingIndentation();
308 }
309
310 }