ASTNode (int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.FOR_KEYWORD: return findChildByType(FOR_KEYWORD); case ChildRole.LPARENTH: return findChildByType(LPARENTH); case ChildRole.FOR_INITIALIZATION: final ASTNode initialization = PsiImplUtil.findStatementChild(this); // should be inside parens ASTNode paren = findChildByRole(ChildRole.LPARENTH); for(ASTNode child = paren; child != null; child = child.getTreeNext()){ if (child == initialization) return initialization; if (child.getElementType() == RPARENTH) return null; } return null; case ChildRole.CONDITION: return findChildByType(EXPRESSION_BIT_SET); case ChildRole.FOR_SEMICOLON: return findChildByType(SEMICOLON); case ChildRole.FOR_UPDATE: ASTNode semicolon = findChildByRole(ChildRole.FOR_SEMICOLON); for(ASTNode child = semicolon; child != null; child = child.getTreeNext()){ if (child.getPsi() instanceof PsiStatement) { return child; } if (child.getElementType() == RPARENTH) break; } return null; case ChildRole.RPARENTH: return findChildByType(RPARENTH); case ChildRole.LOOP_BODY: ASTNode rparenth = findChildByRole(ChildRole.RPARENTH); for(ASTNode child = rparenth; child != null; child = child.getTreeNext()){ if (child.getPsi() instanceof PsiStatement) { return child; } } return null; } }