1 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 41 public class BlockParentHandler extends ExpressionHandler 42 { 43 46 private static final int[] CHECKED_CHILDREN = new int[] { 47 TokenTypes.VARIABLE_DEF, 48 TokenTypes.EXPR, 49 TokenTypes.OBJBLOCK, 50 TokenTypes.LITERAL_BREAK, 51 TokenTypes.LITERAL_RETURN, 52 TokenTypes.LITERAL_THROW, 53 TokenTypes.LITERAL_CONTINUE, 54 }; 55 56 60 protected int[] getCheckedChildren() 61 { 62 return CHECKED_CHILDREN; 63 } 64 65 74 public BlockParentHandler(IndentationCheck aIndentCheck, 75 String aName, DetailAST aAst, ExpressionHandler aParent) 76 { 77 super(aIndentCheck, aName, aAst, aParent); 78 } 79 80 85 protected DetailAST getToplevelAST() 86 { 87 return getMainAst(); 88 } 89 90 93 protected void checkToplevelToken() 94 { 95 final DetailAST toplevel = getToplevelAST(); 96 97 if ((toplevel == null) 98 || getLevel().accept(expandedTabsColumnNo(toplevel))) 99 { 100 return; 101 } 102 if (!toplevelMustStartLine() && !startsLine(toplevel)) { 103 return; 104 } 105 logError(toplevel, "", expandedTabsColumnNo(toplevel)); 106 } 107 108 113 protected boolean toplevelMustStartLine() 114 { 115 return true; 116 } 117 118 123 protected boolean hasCurlys() 124 { 125 return (getLCurly() != null) && (getRCurly() != null); 126 } 127 128 133 protected DetailAST getLCurly() 134 { 135 return getMainAst().findFirstToken(TokenTypes.SLIST); 136 } 137 138 143 protected DetailAST getRCurly() 144 { 145 final DetailAST slist = getMainAst().findFirstToken(TokenTypes.SLIST); 146 if (slist == null) { 147 return null; 148 } 149 150 return slist.findFirstToken(TokenTypes.RCURLY); 151 } 152 153 156 protected void checkLCurly() 157 { 158 final DetailAST lcurly = getLCurly(); 161 final int lcurlyPos = expandedTabsColumnNo(lcurly); 162 163 if ((lcurly == null) 164 || curlyLevel().accept(lcurlyPos) 165 || !startsLine(lcurly)) 166 { 167 return; 168 } 169 170 logError(lcurly, "lcurly", lcurlyPos); 171 } 172 173 178 private IndentLevel curlyLevel() 179 { 180 return new IndentLevel(getLevel(), getBraceAdjustement()); 181 } 182 183 188 protected boolean rcurlyMustStart() 189 { 190 return true; 191 } 192 193 198 protected boolean childrenMayNest() 199 { 200 return false; 201 } 202 203 206 protected void checkRCurly() 207 { 208 final DetailAST lcurly = getLCurly(); 211 final DetailAST rcurly = getRCurly(); 212 final int rcurlyPos = expandedTabsColumnNo(rcurly); 213 214 if ((rcurly == null) 215 || curlyLevel().accept(rcurlyPos) 216 || (!rcurlyMustStart() && !startsLine(rcurly)) 217 || areOnSameLine(rcurly, lcurly)) 218 { 219 return; 220 } 221 logError(rcurly, "rcurly", rcurlyPos, curlyLevel()); 222 } 223 224 229 protected DetailAST getNonlistChild() 230 { 231 return (DetailAST) getMainAst().findFirstToken( 232 TokenTypes.RPAREN).getNextSibling(); 233 } 234 235 238 private void checkNonlistChild() 239 { 240 final DetailAST nonlist = getNonlistChild(); 242 if (nonlist == null) { 243 return; 244 } 245 246 final IndentLevel expected = 247 new IndentLevel(getLevel(), getBasicOffset()); 248 checkExpressionSubtree(nonlist, expected, false, false); 249 } 250 251 256 protected DetailAST getListChild() 257 { 258 return getMainAst().findFirstToken(TokenTypes.SLIST); 259 } 260 261 266 protected DetailAST getRParen() 267 { 268 return getMainAst().findFirstToken(TokenTypes.RPAREN); 269 } 270 271 276 protected DetailAST getLParen() 277 { 278 return getMainAst().findFirstToken(TokenTypes.LPAREN); 279 } 280 281 284 public void checkIndentation() 285 { 286 checkToplevelToken(); 287 checkLParen(getLParen()); 289 checkRParen(getLParen(), getRParen()); 290 if (hasCurlys()) { 291 checkLCurly(); 292 checkRCurly(); 293 } 294 final DetailAST listChild = getListChild(); 295 if (listChild != null) { 296 if (!hasCurlys() || !areOnSameLine(getLCurly(), getRCurly())) { 298 checkChildren(listChild, 299 getCheckedChildren(), 300 getChildrenExpectedLevel(), 301 true, 302 childrenMayNest()); 303 } 304 } 305 else { 306 checkNonlistChild(); 307 } 308 } 309 310 313 protected IndentLevel getChildrenExpectedLevel() 314 { 315 if (getLevel().isMultiLevel() && hasCurlys() 319 && !areOnSameLine(getLCurly(), getRCurly())) 320 { 321 if (startsLine(getLCurly())) { 322 return new IndentLevel(expandedTabsColumnNo(getLCurly()) 323 + getBasicOffset()); 324 } 325 else if (startsLine(getRCurly())) { 326 return new IndentLevel(expandedTabsColumnNo(getRCurly()) 327 + getBasicOffset()); 328 } 329 } 330 return new IndentLevel(getLevel(), getBasicOffset()); 331 } 332 333 334 public IndentLevel suggestedChildLevel(ExpressionHandler aChild) 335 { 336 return getChildrenExpectedLevel(); 337 } 338 339 } 340 | Popular Tags |