Class IndentationCheck
- All Implemented Interfaces:
Configurable,Contextualizable
The idea behind this is that while pretty printers are sometimes convenient for bulk reformats 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 is followed.
Basic offset indentation is used for indentation inside code blocks. For any lines that span more than 1, line wrapping indentation is used for those lines after the first. Brace adjustment, case, and throws indentations are all used only if those specific identifiers start the line. If, for example, a brace is used in the middle of the line, its indentation will not take effect. All indentations have an accumulative/recursive effect when they are triggered. If during a line wrapping, another code block is found and it doesn't end on that same line, then the subsequent lines afterwards, in that new code block, are increased on top of the line wrap and any indentations above it.
Example:
if ((condition1 && condition2)
|| (condition3 && condition4) // line wrap with bigger indentation
||!(condition5 && condition6)) { // line wrap with bigger indentation
field.doSomething() // basic offset
.doSomething() // line wrap
.doSomething( c -> { // line wrap
return c.doSome(); // basic offset
});
}
- Since:
- 3.1
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intSpecify how far an array initialization should be indented when on next line.private intSpecify how far new indentation level should be indented when on the next line.private intSpecify how far a braces should be indented when on the next line.private intSpecify how far a case label should be indented when on next line.private IndentationContextContext handed to handlers for this file; rebuilt inbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST).private static final intDefault indentation amount - based on Sun.private booleanForce strict indent level in line wrapping case.private final HandlerFactoryFactory from which handlers are distributed.private final Deque<AbstractExpressionHandler> Handlers currently in use.Lines logged as having incorrect indentation.private intSpecify how far continuation line should be indented when line-wrapping is present.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.private intSpecify how far a throws clause should be indented when on next line. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private voidClears internal state for memory management between files.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private voidlogIndentation(DetailAST ast, String key, Object... args) Log a violation, deduplicating by line number so each line only produces one indentation error.voidsetArrayInitIndent(int arrayInitIndent) Setter to specify how far an array initialization should be indented when on next line.voidsetBasicOffset(int basicOffset) Setter to specify how far new indentation level should be indented when on the next line.voidsetBraceAdjustment(int adjustmentAmount) Setter to specify how far a braces should be indented when on the next line.voidsetCaseIndent(int amount) Setter to specify how far a case label should be indented when on next line.voidsetForceStrictCondition(boolean value) Setter to force strict indent level in line wrapping case.voidsetLineWrappingIndentation(int lineWrappingIndentation) Setter to specify how far continuation line should be indented when line-wrapping is present.voidsetThrowsIndent(int throwsIndent) Setter to specify how far a throws clause should be indented when on next line.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_ERROR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_CHILD_ERROR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_CHILD_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
DEFAULT_INDENTATION
Default indentation amount - based on Sun.- See Also:
-
handlers
Handlers currently in use. -
handlerFactory
Factory from which handlers are distributed. -
incorrectIndentationLines
Lines logged as having incorrect indentation. -
context
Context handed to handlers for this file; rebuilt inbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST). -
basicOffset
Specify how far new indentation level should be indented when on the next line. -
caseIndent
Specify how far a case label should be indented when on next line. -
braceAdjustment
Specify how far a braces should be indented when on the next line. -
throwsIndent
Specify how far a throws clause should be indented when on next line. -
arrayInitIndent
Specify how far an array initialization should be indented when on next line. -
lineWrappingIndentation
Specify how far continuation line should be indented when line-wrapping is present. -
forceStrictCondition
Force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like.
-
-
Constructor Details
-
IndentationCheck
public IndentationCheck()Creates a newIndentationCheckinstance.
-
-
Method Details
-
setForceStrictCondition
Setter to force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like.- Parameters:
value- user's value of forceStrictCondition.- Since:
- 6.3
-
setBasicOffset
Setter to specify how far new indentation level should be indented when on the next line.- Parameters:
basicOffset- the number of tabs or spaces to indent- Since:
- 3.1
-
setBraceAdjustment
Setter to specify how far a braces should be indented when on the next line.- Parameters:
adjustmentAmount- the brace offset- Since:
- 3.1
-
setCaseIndent
Setter to specify how far a case label should be indented when on next line.- Parameters:
amount- the case indentation level- Since:
- 3.1
-
setThrowsIndent
Setter to specify how far a throws clause should be indented when on next line.- Parameters:
throwsIndent- the throws indentation level- Since:
- 5.7
-
setArrayInitIndent
Setter to specify how far an array initialization should be indented when on next line.- Parameters:
arrayInitIndent- the array initialization indentation level- Since:
- 5.8
-
setLineWrappingIndentation
Setter to specify how far continuation line should be indented when line-wrapping is present.- Parameters:
lineWrappingIndentation- the line-wrapping indentation level- Since:
- 5.9
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
ast- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
logIndentation
Log a violation, deduplicating by line number so each line only produces one indentation error. Invoked by handlers throughIndentationLogger.- Parameters:
ast- the AST for which the error is loggedkey- the message keyargs- message arguments
-
clearState
Clears internal state for memory management between files.
-