From bcbd31a5ecc3aa9b5f0677acb17c441142e57c6f Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Sat, 23 Jul 2005 09:18:36 +0000 Subject: [PATCH] some additional refactoring (to remove code duplication) --- .../indentation/BlockParentHandler.java | 50 +------------ .../checks/indentation/ExpressionHandler.java | 45 +++++++++++ .../checks/indentation/MethodCallHandler.java | 74 ++++++------------- .../checks/indentation/NewHandler.java | 53 ++----------- 4 files changed, 73 insertions(+), 149 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/BlockParentHandler.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/BlockParentHandler.java index ba5026a0a..de9f9c27f 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/BlockParentHandler.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/BlockParentHandler.java @@ -278,52 +278,6 @@ public class BlockParentHandler extends ExpressionHandler return getMainAst().findFirstToken(TokenTypes.LPAREN); } - /** - * Check the indentation of the right parenthesis. - */ - protected void checkRParen() - { - final DetailAST rparen = getRParen(); - - // no paren - no check :) - if (rparen == null) { - return; - } - - // the rcurly can either be at the correct indentation, - // or not first on the line ... - final int rparenLevel = expandedTabsColumnNo(rparen); - if (getLevel().accept(rparenLevel) || !startsLine(rparen)) { - return; - } - - // or has + 1 indentation - final DetailAST lparen = getLParen(); - final int lparenLevel = expandedTabsColumnNo(lparen); - if (rparenLevel == (lparenLevel + 1)) { - return; - } - - logError(rparen, "rparen", expandedTabsColumnNo(rparen)); - } - - /** - * Check the indentation of the left parenthesis. - */ - protected void checkLParen() - { - // the rcurly can either be at the correct indentation, or on the - // same line as the lcurly - DetailAST lparen = getLParen(); - if (lparen == null - || getLevel().accept(expandedTabsColumnNo(lparen)) - || !startsLine(lparen)) - { - return; - } - logError(lparen, "lparen", expandedTabsColumnNo(lparen)); - } - /** * Check the indentation of the expression we are handling. */ @@ -331,8 +285,8 @@ public class BlockParentHandler extends ExpressionHandler { checkToplevelToken(); // seperate to allow for eventual configuration - checkLParen(); - checkRParen(); + checkLParen(getLParen()); + checkRParen(getLParen(), getRParen()); if (hasCurlys()) { checkLCurly(); checkRCurly(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/ExpressionHandler.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/ExpressionHandler.java index 1eb2aac3c..401e93686 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/ExpressionHandler.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/ExpressionHandler.java @@ -606,4 +606,49 @@ public abstract class ExpressionHandler { return getIndentCheck().getBraceAdjustement(); } + + /** + * Check the indentation of the right parenthesis. + * @param aRparen parenthesis to check + * @param aLparen left parenthesis associated with aRparen + */ + protected final void checkRParen(DetailAST aLparen, DetailAST aRparen) + { + // no paren - no check :) + if (aRparen == null) { + return; + } + + // the rcurly can either be at the correct indentation, + // or not first on the line ... + final int rparenLevel = expandedTabsColumnNo(aRparen); + if (getLevel().accept(rparenLevel) || !startsLine(aRparen)) { + return; + } + + // or has + 1 indentation + final int lparenLevel = expandedTabsColumnNo(aLparen); + if (rparenLevel == (lparenLevel + 1)) { + return; + } + + logError(aRparen, "rparen", rparenLevel); + } + + /** + * Check the indentation of the left parenthesis. + * @param aLparen parenthesis to check + */ + protected final void checkLParen(final DetailAST aLparen) + { + // the rcurly can either be at the correct indentation, or on the + // same line as the lcurly + if (aLparen == null + || getLevel().accept(expandedTabsColumnNo(aLparen)) + || !startsLine(aLparen)) + { + return; + } + logError(aLparen, "lparen", expandedTabsColumnNo(aLparen)); + } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java index 4fd031a01..40c174057 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java @@ -46,39 +46,6 @@ public class MethodCallHandler extends ExpressionHandler aParent); } - /** - * Check the indentation of the left parenthesis. - */ - private void checkLParen() - { - final DetailAST lparen = getMainAst(); - final int columnNo = expandedTabsColumnNo(lparen); - - if (getLevel().accept(columnNo) || !startsLine(lparen)) { - return; - } - - logError(lparen, "lparen", columnNo); - } - - /** - * Check the indentation of the right parenthesis. - */ - private void checkRParen() - { - // the rparen can either be at the correct indentation, or on - // the same line as the lparen - final DetailAST rparen = - getMainAst().findFirstToken(TokenTypes.RPAREN); - final int columnNo = expandedTabsColumnNo(rparen); - - if (getLevel().accept(columnNo) || !startsLine(rparen)) { - return; - } - - logError(rparen, "rparen", columnNo); - } - /** * Compute the indentation amount for this handler. * @@ -199,29 +166,30 @@ public class MethodCallHandler extends ExpressionHandler DetailAST methodName = (DetailAST) getMainAst().getFirstChild(); checkExpressionSubtree(methodName, getLevel(), false, false); - checkLParen(); - DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); DetailAST lparen = getMainAst(); + DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); + checkLParen(lparen); - if (rparen.getLineNo() != lparen.getLineNo()) { - - // if this method name is on the same line as a containing - // method, don't indent, this allows expressions like: - // method("my str" + method2( - // "my str2")); - // as well as - // method("my str" + - // method2( - // "my str2")); - // - - checkExpressionSubtree( - getMainAst().findFirstToken(TokenTypes.ELIST), - new IndentLevel(getLevel(), getBasicOffset()), - false, true); - - checkRParen(); + if (rparen.getLineNo() == lparen.getLineNo()) { + return; } + + // if this method name is on the same line as a containing + // method, don't indent, this allows expressions like: + // method("my str" + method2( + // "my str2")); + // as well as + // method("my str" + + // method2( + // "my str2")); + // + + checkExpressionSubtree( + getMainAst().findFirstToken(TokenTypes.ELIST), + new IndentLevel(getLevel(), getBasicOffset()), + false, true); + + checkRParen(lparen, rparen); } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/NewHandler.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/NewHandler.java index 23cd933a8..ada762021 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/NewHandler.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/NewHandler.java @@ -49,9 +49,9 @@ public class NewHandler extends ExpressionHandler DetailAST type = (DetailAST) getMainAst().getFirstChild(); checkExpressionSubtree(type, getLevel(), false, false); - checkLParen(); - DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); DetailAST lparen = getMainAst().findFirstToken(TokenTypes.LPAREN); + DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); + checkLParen(lparen); if (rparen == null || lparen == null || rparen.getLineNo() == lparen.getLineNo()) @@ -74,7 +74,7 @@ public class NewHandler extends ExpressionHandler new IndentLevel(getLevel(), getBasicOffset()), false, true); - checkRParen(); + checkRParen(lparen, rparen); } /** {@inheritDoc} */ @@ -82,55 +82,12 @@ public class NewHandler extends ExpressionHandler { // if our expression isn't first on the line, just use the start // of the line - int lineStart = getLineStart(getMainAst()); - if (lineStart != getMainAst().getColumnNo()) { - return new IndentLevel(lineStart); + if (getLineStart(getMainAst()) != getMainAst().getColumnNo()) { + return new IndentLevel(getLineStart(getMainAst())); } return super.getLevelImpl(); } - /** - * Check the indentation of the left parenthesis. - */ - private void checkLParen() - { - final DetailAST lparen = - getMainAst().findFirstToken(TokenTypes.LPAREN); - - if (lparen == null) { - return; - } - - final int columnNo = expandedTabsColumnNo(lparen); - - if (getLevel().accept(columnNo) || !startsLine(lparen)) { - return; - } - - logError(lparen, "lparen", columnNo); - } - - /** - * Check the indentation of the right parenthesis. - */ - private void checkRParen() - { - // the rparen can either be at the correct indentation, or on - // the same line as the lparen - final DetailAST rparen = - getMainAst().findFirstToken(TokenTypes.RPAREN); - if (rparen == null) { - return; - } - final int columnNo = expandedTabsColumnNo(rparen); - - if (getLevel().accept(columnNo) || !startsLine(rparen)) { - return; - } - - logError(rparen, "rparen", columnNo); - } - /** {@inheritDoc} */ protected boolean shouldIncreaseIndent() {