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