From f6685bca1e8a6853e6192e2a3f71f1f365496eee Mon Sep 17 00:00:00 2001 From: rnveach Date: Mon, 1 Feb 2016 17:06:46 -0500 Subject: [PATCH] Issue #2795: fixed handling of tabs in LineWrapping --- .../indentation/LineWrappingHandler.java | 44 ++++++++++++++----- .../indentation/InputValidMethodIndent.java | 6 +++ 2 files changed, 38 insertions(+), 12 deletions(-) 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 d967ae619..bc3cb6766 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 @@ -26,6 +26,7 @@ import java.util.TreeMap; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** * This class checks line-wrapping into definitions and expressions. The @@ -127,8 +128,8 @@ public class LineWrappingHandler { * first node. * @return indentation of first node. */ - private static int getFirstNodeIndent(DetailAST node) { - int indentLevel = node.getColumnNo(); + private int getFirstNodeIndent(DetailAST node) { + final int result; if (node.getType() == TokenTypes.LITERAL_IF && node.getParent().getType() == TokenTypes.LITERAL_ELSE) { @@ -137,13 +138,16 @@ public class LineWrappingHandler { if (lcurly.getType() == TokenTypes.SLIST && rcurly.getLineNo() == node.getLineNo()) { - indentLevel = rcurly.getColumnNo(); + result = expandedTabsColumnNo(rcurly); } else { - indentLevel = node.getParent().getColumnNo(); + result = expandedTabsColumnNo(node.getParent()); } } - return indentLevel; + else { + result = expandedTabsColumnNo(node); + } + return result; } /** @@ -169,7 +173,7 @@ public class LineWrappingHandler { final DetailAST firstTokenOnLine = result.get(curNode.getLineNo()); if (firstTokenOnLine == null - || firstTokenOnLine.getColumnNo() >= curNode.getColumnNo()) { + || expandedTabsColumnNo(firstTokenOnLine) >= expandedTabsColumnNo(curNode)) { result.put(curNode.getLineNo(), curNode); } curNode = getNextCurNode(curNode); @@ -206,8 +210,8 @@ public class LineWrappingHandler { */ private void checkAnnotationIndentation(DetailAST atNode, NavigableMap firstNodesOnLines) { - final int currentIndent = atNode.getColumnNo() + indentLevel; - final int firstNodeIndent = atNode.getColumnNo(); + final int firstNodeIndent = expandedTabsColumnNo(atNode); + final int currentIndent = firstNodeIndent + indentLevel; final Collection values = firstNodesOnLines.values(); final DetailAST lastAnnotationNode = getLastAnnotationNode(atNode); final int lastAnnotationLine = lastAnnotationNode.getLineNo(); @@ -237,6 +241,22 @@ public class LineWrappingHandler { } } + /** + * Get the column number for the start of a given expression, expanding + * tabs out into spaces in the process. + * + * @param ast the expression to find the start of + * + * @return the column number for the start of the expression + */ + private int expandedTabsColumnNo(DetailAST ast) { + final String line = + indentCheck.getLine(ast.getLineNo() - 1); + + return CommonUtils.lengthExpandedTabs(line, ast.getColumnNo(), + indentCheck.getIndentationTabWidth()); + } + /** * Finds and returns last annotation node. * @param atNode first at-clause node. @@ -261,17 +281,17 @@ public class LineWrappingHandler { */ private void logWarningMessage(DetailAST currentNode, int currentIndent) { if (forceStrictCondition) { - if (currentNode.getColumnNo() != currentIndent) { + if (expandedTabsColumnNo(currentNode) != currentIndent) { indentCheck.indentationLog(currentNode.getLineNo(), IndentationCheck.MSG_ERROR, currentNode.getText(), - currentNode.getColumnNo(), currentIndent); + expandedTabsColumnNo(currentNode), currentIndent); } } else { - if (currentNode.getColumnNo() < currentIndent) { + if (expandedTabsColumnNo(currentNode) < currentIndent) { indentCheck.indentationLog(currentNode.getLineNo(), IndentationCheck.MSG_ERROR, currentNode.getText(), - currentNode.getColumnNo(), currentIndent); + expandedTabsColumnNo(currentNode), currentIndent); } } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputValidMethodIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputValidMethodIndent.java index 316299bc5..4bec1edf4 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputValidMethodIndent.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputValidMethodIndent.java @@ -194,4 +194,10 @@ public class InputValidMethodIndent extends java.awt.event.MouseAdapter implemen getArray()[0] = 2; //indent:8 exp:8 } //indent:4 exp:4 + // the following lines have tabs //indent:4 exp:4 + @SuppressWarnings( //indent:4 exp:4 + value="" //indent:8 exp:8 + ) //indent:4 exp:4 + public void testStartOfSequence() { //indent:4 exp:4 + } //indent:4 exp:4 } //indent:0 exp:0