From f111cc06fb4744981f79173db4659b8a8f12d5aa Mon Sep 17 00:00:00 2001 From: rnveach Date: Sun, 24 Apr 2016 08:35:37 -0400 Subject: [PATCH] Issue #2950: rewrote LineWrappingHandler for easier use (#2951) --- .../indentation/LineWrappingHandler.java | 60 +++++++++---------- .../indentation/IndentationCheckTest.java | 10 ++++ .../InputTwoStatementsPerLine.java | 13 ++++ 3 files changed, 52 insertions(+), 31 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputTwoStatementsPerLine.java 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 4ceed2a2f..e9c3958f7 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 @@ -84,7 +84,7 @@ public class LineWrappingHandler { // First node should be removed because it was already checked before. firstNodesOnLines.remove(firstNodesOnLines.firstKey()); - final int firstNodeIndent = getFirstNodeIndent(firstLineNode); + final int firstNodeIndent = getLineStart(firstLineNode); final int currentIndent = firstNodeIndent + indentLevel; for (DetailAST node : firstNodesOnLines.values()) { @@ -99,35 +99,6 @@ public class LineWrappingHandler { } } - /** - * Calculates indentation of first node. - * - * @param node - * first node. - * @return indentation of first node. - */ - private int getFirstNodeIndent(DetailAST node) { - final int result; - - if (node.getType() == TokenTypes.LITERAL_IF - && node.getParent().getType() == TokenTypes.LITERAL_ELSE) { - final DetailAST lcurly = node.getParent().getPreviousSibling(); - final DetailAST rcurly = lcurly.getLastChild(); - - if (lcurly.getType() == TokenTypes.SLIST - && rcurly.getLineNo() == node.getLineNo()) { - result = expandedTabsColumnNo(rcurly); - } - else { - result = expandedTabsColumnNo(node.getParent()); - } - } - else { - result = expandedTabsColumnNo(node); - } - return result; - } - /** * Finds first nodes on line and puts them into Map. * @@ -190,7 +161,7 @@ public class LineWrappingHandler { */ private void checkAnnotationIndentation(DetailAST atNode, NavigableMap firstNodesOnLines, int indentLevel) { - final int firstNodeIndent = expandedTabsColumnNo(atNode); + final int firstNodeIndent = getLineStart(atNode); final int currentIndent = firstNodeIndent + indentLevel; final Collection values = firstNodesOnLines.values(); final DetailAST lastAnnotationNode = getLastAnnotationNode(atNode); @@ -237,6 +208,33 @@ public class LineWrappingHandler { indentCheck.getIndentationTabWidth()); } + /** + * Get the start of the line for the given expression. + * + * @param ast the expression to find the start of the line for + * + * @return the start of the line for the given expression + */ + private int getLineStart(DetailAST ast) { + final String line = indentCheck.getLine(ast.getLineNo() - 1); + return getLineStart(line); + } + + /** + * Get the start of the specified line. + * + * @param line the specified line number + * + * @return the start of the specified line + */ + private int getLineStart(String line) { + int index = 0; + while (Character.isWhitespace(line.charAt(index))) { + index++; + } + return CommonUtils.lengthExpandedTabs(line, index, indentCheck.getIndentationTabWidth()); + } + /** * Finds and returns last annotation node. * @param atNode first at-clause node. 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 2a2bd8b1e..fd91c0842 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 @@ -1600,6 +1600,16 @@ public class IndentationCheckTest extends BaseCheckTestSupport { verify(checkConfig, fileName, expected); } + @Test + public void testTwoStatementsPerLine() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + checkConfig.addAttribute("basicOffset", "4"); + final String fileName = getPath("InputTwoStatementsPerLine.java"); + final String[] expected = CommonUtils.EMPTY_STRING_ARRAY; + verifyWarns(checkConfig, fileName, expected); + } + private static final class IndentAudit implements AuditListener { private final IndentComment[] comments; private int position; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputTwoStatementsPerLine.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputTwoStatementsPerLine.java new file mode 100644 index 000000000..50f659d82 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputTwoStatementsPerLine.java @@ -0,0 +1,13 @@ +package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:0 exp:0 + +public class InputTwoStatementsPerLine {//indent:0 exp:0 + int var6 = 5; int var7 = 6, //indent:4 exp:4 + var8 = 5; //indent:8 exp:8 + + public void method() { //indent:4 exp:4 + long_lined_label: if (true //indent:8 exp:8 + && true) {} //indent:12 exp:12 + } //indent:4 exp:4 + /* package-private */ static final void //indent:4 exp:4 + method2() {} //indent:8 exp:8 +}//indent:0 exp:0