minor: refactored LineWrappingHandler

This commit is contained in:
rnveach 2016-02-20 15:23:26 -05:00 committed by Roman Ivanov
parent 2482a07872
commit a0fed4c0e9
10 changed files with 58 additions and 68 deletions

View File

@ -344,7 +344,6 @@ public abstract class AbstractExpressionHandler {
* @param indentLevel the indentation level
* @param mustMatch whether or not the indentation level must match
*/
private void checkLineIndent(int lineNum, int colNum,
IndentLevel indentLevel, boolean mustMatch) {
final String line = indentCheck.getLine(lineNum - 1);
@ -359,6 +358,17 @@ public abstract class AbstractExpressionHandler {
}
}
/**
* Checks indentation on wrapped lines between and including
* {@code firstNode} and {@code lastNode}.
*
* @param firstNode First node to start examining.
* @param lastNode Last node to examine inclusively.
*/
protected void checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode) {
indentCheck.getLineWrappingHandler().checkIndentation(firstNode, lastNode);
}
/**
* Check the indent level of the children of the specified parent
* expression.

View File

@ -80,9 +80,7 @@ public class ClassDefHandler extends BlockParentHandler {
checkModifiers();
}
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(), getMainAst().getLastChild());
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(), getMainAst().getLastChild());
super.checkIndentation();
}

View File

@ -72,10 +72,7 @@ public class ForHandler extends BlockParentHandler {
public void checkIndentation() {
checkForParams();
super.checkIndentation();
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getForLoopRightParen(getMainAst()));
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(), getForLoopRightParen(getMainAst()));
}
/**

View File

@ -94,10 +94,7 @@ public class IfHandler extends BlockParentHandler {
public void checkIndentation() {
super.checkIndentation();
checkCondExpr();
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getIfStatementRightParen(getMainAst()));
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(), getIfStatementRightParen(getMainAst()));
}
/**

View File

@ -112,6 +112,9 @@ public class IndentationCheck extends AbstractCheck {
/** Handlers currently in use. */
private final Deque<AbstractExpressionHandler> handlers = new ArrayDeque<>();
/** Instance of line wrapping handler to use. */
private final LineWrappingHandler lineWrappingHandler = new LineWrappingHandler(this);
/** Factory from which handlers are distributed. */
private final HandlerFactory handlerFactory = new HandlerFactory();
@ -330,6 +333,15 @@ public class IndentationCheck extends AbstractCheck {
handlers.pop();
}
/**
* Accessor for the line wrapping handler.
*
* @return the line wrapping handler
*/
public LineWrappingHandler getLineWrappingHandler() {
return lineWrappingHandler;
}
/**
* Accessor for the handler factory.
*

View File

@ -45,61 +45,41 @@ public class LineWrappingHandler {
*/
private final IndentationCheck indentCheck;
/**
* Root node for current expression.
*/
private final DetailAST firstNode;
/**
* Last node for current expression.
*/
private final DetailAST lastNode;
/**
* User's value of line wrapping indentation.
*/
private final int indentLevel;
/**
* Force strict condition in line wrapping case.
*/
private final boolean forceStrictCondition;
/**
* Sets values of class field, finds last node and calculates indentation level.
*
* @param instance
* instance of IndentationCheck.
* @param firstNode
* root node for current expression.
* @param lastNode
* last node for current expression.
*/
public LineWrappingHandler(IndentationCheck instance, DetailAST firstNode, DetailAST lastNode) {
public LineWrappingHandler(IndentationCheck instance) {
indentCheck = instance;
this.firstNode = firstNode;
this.lastNode = lastNode;
indentLevel = indentCheck.getLineWrappingIndentation();
forceStrictCondition = indentCheck.isForceStrictCondition();
}
/**
* Getter for lastNode field.
* @return lastNode field
* Checks line wrapping into expressions and definitions using property
* 'lineWrappingIndentation'.
*
* @param firstNode First node to start examining.
* @param lastNode Last node to examine inclusively.
*/
protected final DetailAST getLastNode() {
return lastNode;
public void checkIndentation(DetailAST firstNode, DetailAST lastNode) {
checkIndentation(firstNode, lastNode, indentCheck.getLineWrappingIndentation());
}
/**
* Checks line wrapping into expressions and definitions.
*
* @param firstNode First node to start examining.
* @param lastNode Last node to examine inclusively.
* @param indentLevel Indentation all wrapped lines should use.
*/
public void checkIndentation() {
final NavigableMap<Integer, DetailAST> firstNodesOnLines = collectFirstNodes();
public void checkIndentation(DetailAST firstNode, DetailAST lastNode, int indentLevel) {
final NavigableMap<Integer, DetailAST> firstNodesOnLines = collectFirstNodes(firstNode,
lastNode);
final DetailAST firstLineNode = firstNodesOnLines.get(firstNodesOnLines.firstKey());
if (firstLineNode.getType() == TokenTypes.AT) {
checkAnnotationIndentation(firstLineNode, firstNodesOnLines);
checkAnnotationIndentation(firstLineNode, firstNodesOnLines, indentLevel);
}
// First node should be removed because it was already checked before.
@ -153,10 +133,13 @@ public class LineWrappingHandler {
/**
* Finds first nodes on line and puts them into Map.
*
* @param firstNode First node to start examining.
* @param lastNode Last node to examine inclusively.
* @return NavigableMap which contains lines numbers as a key and first
* nodes on lines as a values.
*/
private NavigableMap<Integer, DetailAST> collectFirstNodes() {
private NavigableMap<Integer, DetailAST> collectFirstNodes(DetailAST firstNode,
DetailAST lastNode) {
final NavigableMap<Integer, DetailAST> result = new TreeMap<>();
result.put(firstNode.getLineNo(), firstNode);
@ -205,9 +188,10 @@ public class LineWrappingHandler {
* @param atNode at-clause node.
* @param firstNodesOnLines map which contains
* first nodes as values and line numbers as keys.
* @param indentLevel line wrapping indentation.
*/
private void checkAnnotationIndentation(DetailAST atNode,
NavigableMap<Integer, DetailAST> firstNodesOnLines) {
NavigableMap<Integer, DetailAST> firstNodesOnLines, int indentLevel) {
final int firstNodeIndent = expandedTabsColumnNo(atNode);
final int currentIndent = firstNodeIndent + indentLevel;
final Collection<DetailAST> values = firstNodesOnLines.values();
@ -278,7 +262,7 @@ public class LineWrappingHandler {
* correct indentation.
*/
private void logWarningMessage(DetailAST currentNode, int currentIndent) {
if (forceStrictCondition) {
if (indentCheck.isForceStrictCondition()) {
if (expandedTabsColumnNo(currentNode) != currentIndent) {
indentCheck.indentationLog(currentNode.getLineNo(),
IndentationCheck.MSG_ERROR, currentNode.getText(),

View File

@ -50,11 +50,11 @@ public class MemberDefHandler extends AbstractExpressionHandler {
else {
checkModifiers();
}
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getVarDefStatementSemicolon(getMainAst()));
if (lineWrap.getLastNode() != null && !isArrayDeclaration(getMainAst())) {
lineWrap.checkIndentation();
final DetailAST firstNode = getMainAst();
final DetailAST lastNode = getVarDefStatementSemicolon(firstNode);
if (lastNode != null && !isArrayDeclaration(firstNode)) {
checkWrappingIndentation(firstNode, lastNode);
}
}

View File

@ -168,10 +168,7 @@ public class MethodCallHandler extends AbstractExpressionHandler {
false, true);
checkRParen(lparen, rparen);
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getMethodCallLastNode(getMainAst()));
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(), getMethodCallLastNode(getMainAst()));
}
@Override

View File

@ -61,10 +61,7 @@ public class MethodDefHandler extends BlockParentHandler {
public void checkIndentation() {
checkModifiers();
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getMethodDefParamRightParen(getMainAst()));
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(), getMethodDefParamRightParen(getMainAst()));
if (getLCurly() == null) {
// abstract method def -- no body
return;

View File

@ -53,10 +53,8 @@ public class SynchronizedHandler extends BlockParentHandler {
if (!methodModifier) {
super.checkIndentation();
checkSynchronizedExpr();
final LineWrappingHandler lineWrap =
new LineWrappingHandler(getIndentCheck(), getMainAst(),
getSynchronizedStatementRightParen(getMainAst()));
lineWrap.checkIndentation();
checkWrappingIndentation(getMainAst(),
getSynchronizedStatementRightParen(getMainAst()));
}
}