From 53c887f92d8b3c40d2bf83cc7ab47c5b47b9ba36 Mon Sep 17 00:00:00 2001 From: rnveach Date: Sun, 24 Apr 2016 12:59:21 -0400 Subject: [PATCH] Issue #2618: refactored indentation import and package logic --- .../AbstractExpressionHandler.java | 41 +------------------ .../checks/indentation/ImportHandler.java | 12 +++--- .../indentation/LineWrappingHandler.java | 3 +- .../checks/indentation/PackageDefHandler.java | 9 ++-- .../indentation/IndentationCheckTest.java | 21 +++++++++- .../indentation/InputPackageDeclaration2.java | 4 ++ .../indentation/InputInvalidImportIndent.java | 5 ++- .../indentation/InputPackageDeclaration3.java | 3 ++ 8 files changed, 47 insertions(+), 51 deletions(-) create mode 100644 src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration2.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration3.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java index 1a5fe911ead..67fc54d5015 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java @@ -236,9 +236,9 @@ protected final int getLineStart(int lineNo) { * * @return the start of the specified line */ - protected final int getLineStart(String line) { + private int getLineStart(String line) { int index = 0; - while (index < line.length() && Character.isWhitespace(line.charAt(index))) { + while (Character.isWhitespace(line.charAt(index))) { index++; } return CommonUtils.lengthExpandedTabs( @@ -255,27 +255,6 @@ protected boolean shouldIncreaseIndent() { return true; } - /** - * Check the indentation of consecutive lines for the expression we are - * handling. - * - * @param startLine the first line to check - * @param endLine the last line to check - * @param indentLevel the required indent level - */ - protected final void checkLinesIndent(int startLine, int endLine, - IndentLevel indentLevel) { - // check first line - checkLineIndent(startLine, indentLevel); - - // check following lines - final IndentLevel offsetLevel = - new IndentLevel(indentLevel, getBasicOffset()); - for (int i = startLine + 1; i <= endLine; i++) { - checkLineIndent(i, offsetLevel); - } - } - /** * Check the indentation for a set of lines. * @@ -328,22 +307,6 @@ private void checkLinesIndent(LineSet lines, } } - /** - * Check the indent level for a single line. - * - * @param lineNum the line number to check - * @param indentLevel the required indent level - */ - private void checkLineIndent(int lineNum, IndentLevel indentLevel) { - final String line = indentCheck.getLine(lineNum - 1); - if (!line.isEmpty()) { - final int start = getLineStart(line); - if (indentLevel.isGreaterThan(start)) { - logChildError(lineNum, start, indentLevel); - } - } - } - /** * Check the indentation for a single line. * diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ImportHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ImportHandler.java index d1a93c34379..54fc104f71e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ImportHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ImportHandler.java @@ -43,12 +43,14 @@ public ImportHandler(IndentationCheck indentCheck, @Override public void checkIndentation() { - final int lineStart = getMainAst().getLineNo(); - final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI); - final int lineEnd = semi.getLineNo(); + final int columnNo = expandedTabsColumnNo(getMainAst()); - if (getMainAst().getLineNo() != lineEnd) { - checkLinesIndent(lineStart, lineEnd, getIndent()); + if (!getIndent().isAcceptable(columnNo) && isOnStartOfLine(getMainAst())) { + logError(getMainAst(), "", columnNo); } + + final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI); + + checkWrappingIndentation(getMainAst(), semi); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java index 7e1ff94cfc9..2127ba60af9 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java @@ -201,7 +201,8 @@ private void checkAnnotationIndentation(DetailAST atNode, && node.equals(lastAnnotationNode); if (isCurrentNodeCloseAnnotationAloneInLine || node.getType() == TokenTypes.AT - && parentNode.getParent().getType() == TokenTypes.MODIFIERS) { + && (parentNode.getParent().getType() == TokenTypes.MODIFIERS + || parentNode.getParent().getType() == TokenTypes.ANNOTATIONS)) { logWarningMessage(node, firstNodeIndent); } else { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/PackageDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/PackageDefHandler.java index 6ab177dcea0..d1136573cb7 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/PackageDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/PackageDefHandler.java @@ -44,12 +44,13 @@ public PackageDefHandler(IndentationCheck indentCheck, @Override public void checkIndentation() { final int columnNo = expandedTabsColumnNo(getMainAst()); - if (!getIndent().isAcceptable(columnNo)) { + + if (!getIndent().isAcceptable(columnNo) && isOnStartOfLine(getMainAst())) { logError(getMainAst(), "", columnNo); } - checkLinesIndent(getMainAst().getLineNo(), - getMainAst().findFirstToken(TokenTypes.SEMI).getLineNo(), - getIndent()); + final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI); + + checkWrappingIndentation(getMainAst(), semi); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index c4f49692607..6db11601c85 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -1467,7 +1467,8 @@ public void testInvalidImportIndent() throws Exception { checkConfig.addAttribute("basicOffset", "8"); checkConfig.addAttribute("tabWidth", "4"); final String[] expected = { - "4: " + getCheckMessage(MSG_CHILD_ERROR, "import", 2, 8), + "4: " + getCheckMessage(MSG_ERROR, ".", 2, 4), + "5: " + getCheckMessage(MSG_ERROR, "import", 1, 0), }; verifyWarns(checkConfig, getPath("InputInvalidImportIndent.java"), expected); } @@ -1607,6 +1608,24 @@ public void testPackageDeclaration() throws Exception { verifyWarns(checkConfig, getPath("InputPackageDeclaration.java"), expected); } + @Test + public void testPackageDeclaration2() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + final String[] expected = { + "2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0), + }; + verifyWarns(checkConfig, getNonCompilablePath("InputPackageDeclaration2.java"), expected); + } + + @Test + public void testPackageDeclaration3() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + final String[] expected = CommonUtils.EMPTY_STRING_ARRAY; + verifyWarns(checkConfig, getPath("InputPackageDeclaration3.java"), expected); + } + @Test public void testLambda1() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration2.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration2.java new file mode 100644 index 00000000000..ac88911989f --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration2.java @@ -0,0 +1,4 @@ + @Deprecated //indent:1 exp:1 + package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:1 exp:0 warn + +public class InputPackageDeclaration2 {}//indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidImportIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidImportIndent.java index 2a3830b4c1a..f08a9170d98 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidImportIndent.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidImportIndent.java @@ -1,7 +1,10 @@ package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0 import java.util //indent:0 exp:0 - .RandomAccess; //indent:2 exp:8 warn + .RandomAccess; import java.util.RandomAccess; //indent:2 exp:4 warn + import java.util.RandomAccess; //indent:1 exp:0 warn +import java.util //indent:0 exp:0 + .RandomAccess; //indent:19 exp:>=8 /** //indent:0 exp:0 * This test-input is intended to be checked using following configuration: //indent:1 exp:1 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration3.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration3.java new file mode 100644 index 00000000000..6a7e108977e --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputPackageDeclaration3.java @@ -0,0 +1,3 @@ +/** Comment */ package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:0 exp:>=0 + +public class InputPackageDeclaration3 {}//indent:0 exp:0