Issue #2950: rewrote LineWrappingHandler for easier use (#2951)

This commit is contained in:
rnveach 2016-04-24 08:35:37 -04:00 committed by Roman Ivanov
parent 585738d956
commit f111cc06fb
3 changed files with 52 additions and 31 deletions

View File

@ -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<Integer, DetailAST> firstNodesOnLines, int indentLevel) {
final int firstNodeIndent = expandedTabsColumnNo(atNode);
final int firstNodeIndent = getLineStart(atNode);
final int currentIndent = firstNodeIndent + indentLevel;
final Collection<DetailAST> 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.

View File

@ -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;

View File

@ -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