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