public class IndentationCheck extends AbstractCheck
The basic idea behind this is that while pretty printers are sometimes convenient for reformatting of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules are followed.
Implementation -- Basically, this check requests visitation for all handled token types (those tokens registered in the HandlerFactory). When visitToken is called, a new ExpressionHandler is created for the AST and pushed onto the handlers stack. The new handler then checks the indentation for the currently visiting AST. When leaveToken is called, the ExpressionHandler is popped from the stack.
While on the stack the ExpressionHandler can be queried for the indentation level it suggests for children as well as for other values.
While an ExpressionHandler checks the indentation level of its own AST, it typically also checks surrounding ASTs. For instance, a while loop handler checks the while loop as well as the braces and immediate children.
- handler class -to-> ID mapping kept in Map - parent passed in during construction - suggest child indent level - allows for some tokens to be on same line (ie inner classes OBJBLOCK) and not increase indentation level - looked at using double dispatch for getSuggestedChildIndent(), but it doesn't seem worthwhile, at least now - both tabs and spaces are considered whitespace in front of the line... tabs are converted to spaces - block parents with parens -- for, while, if, etc... -- are checked that they match the level of the parent
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_CHILD_ERROR
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_CHILD_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_ERROR
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
IndentationCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST ast)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int |
getArrayInitIndent()
Get the line-wrapping indentation level.
|
int |
getBasicOffset()
Get the basic offset.
|
int |
getBraceAdjustment()
Get the brace adjustment amount.
|
int |
getCaseIndent()
Get the case indentation level.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
HandlerFactory |
getHandlerFactory()
Accessor for the handler factory.
|
int |
getIndentationTabWidth()
Get the width of a tab.
|
LineWrappingHandler |
getLineWrappingHandler()
Accessor for the line wrapping handler.
|
int |
getLineWrappingIndentation()
Get the array line-wrapping indentation level.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
int |
getThrowsIndent()
Get the throws indentation level.
|
void |
indentationLog(int line,
String key,
Object... args)
Log an error message.
|
boolean |
isForceStrictCondition()
Get forcing strict condition.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
setArrayInitIndent(int arrayInitIndent)
Set the array initialisation indentation level.
|
void |
setBasicOffset(int basicOffset)
Set the basic offset.
|
void |
setBraceAdjustment(int adjustmentAmount)
Adjusts brace indentation (positive offset).
|
void |
setCaseIndent(int amount)
Set the case indentation level.
|
void |
setForceStrictCondition(boolean value)
Set forcing strict condition.
|
void |
setLineWrappingIndentation(int lineWrappingIndentation)
Set the line-wrapping indentation level.
|
void |
setThrowsIndent(int throwsIndent)
Set the throws indentation level.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_ERROR
public static final String MSG_ERROR_MULTI
public static final String MSG_CHILD_ERROR
public static final String MSG_CHILD_ERROR_MULTI
public IndentationCheck()
public boolean isForceStrictCondition()
public void setForceStrictCondition(boolean value)
value
- user's value of forceStrictCondition.public void setBasicOffset(int basicOffset)
basicOffset
- the number of tabs or spaces to indentpublic int getBasicOffset()
public void setBraceAdjustment(int adjustmentAmount)
adjustmentAmount
- the brace offsetpublic int getBraceAdjustment()
public void setCaseIndent(int amount)
amount
- the case indentation levelpublic int getCaseIndent()
public void setThrowsIndent(int throwsIndent)
throwsIndent
- the throws indentation levelpublic int getThrowsIndent()
public void setArrayInitIndent(int arrayInitIndent)
arrayInitIndent
- the array initialisation indentation levelpublic int getArrayInitIndent()
public int getLineWrappingIndentation()
public void setLineWrappingIndentation(int lineWrappingIndentation)
lineWrappingIndentation
- the line-wrapping indentation levelpublic void indentationLog(int line, String key, Object... args)
line
- the line number where the error was foundkey
- the message that describes the errorargs
- the details of the messageMessageFormat
public int getIndentationTabWidth()
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST ast)
AbstractCheck
beginTree
in class AbstractCheck
ast
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processpublic void leaveToken(DetailAST ast)
AbstractCheck
leaveToken
in class AbstractCheck
ast
- the token leavingpublic LineWrappingHandler getLineWrappingHandler()
public final HandlerFactory getHandlerFactory()
Copyright © 2001–2018. All rights reserved.