Class SingleSpaceSeparatorCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
Checks that non-whitespace characters are separated by no more than one
whitespace. Separating characters by tabs or multiple spaces will be
reported. Currently, the check doesn't permit horizontal alignment. To inspect
whitespaces before and after comments, set the property
validateComments
to true.
Setting validateComments
to false will ignore cases like:
int i; // Multiple whitespaces before comment tokens will be ignored. private void foo(int /* whitespaces before and after block-comments will be ignored */ i) {
Sometimes, users like to space similar items on different lines to the same column position for easier reading. This feature isn't supported by this check, so both braces in the following case will be reported as violations.
public long toNanos(long d) { return d; } // 2 violations public long toMicros(long d) { return d / (C1 / C0); }
-
Property
validateComments
- Control whether to validate whitespaces surrounding comments. Type isboolean
. Default value isfalse
.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
single.space.separator
- Since:
- 6.19
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
A key is pointing to the warning message text in "messages.properties" file.private boolean
Control whether to validate whitespaces surrounding comments. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Called before the starting to process a tree.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.private static boolean
isBlockCommentEnd
(int[] line, int columnNo) Checks if theline
atcolumnNo
is the end of a comment, '*/'.boolean
Whether comment nodes are required or not.private static boolean
isFirstInLine
(int[] line, int columnNo) Checks if theline
up to and includingcolumnNo
is all non-whitespace text encountered.private static boolean
isSingleSpace
(int[] line, int columnNo) Checks if theline
atcolumnNo
is a single space, and not preceded by another space.private static boolean
isSpace
(int[] line, int columnNo) Checks if theline
atcolumnNo
is a space.private boolean
isTextSeparatedCorrectlyFromPrevious
(int[] line, int columnNo) Checks if characters inline
at and aroundcolumnNo
has the correct number of spaces. to returntrue
the following conditions must be met: the character atcolumnNo
is the first in the line.void
setValidateComments
(boolean validateComments) Setter to control whether to validate whitespaces surrounding comments.private void
visitEachToken
(DetailAST node) Examines every sibling and child ofnode
for violations.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitToken
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
validateComments
Control whether to validate whitespaces surrounding comments.
-
-
Constructor Details
-
SingleSpaceSeparatorCheck
public SingleSpaceSeparatorCheck()
-
-
Method Details
-
setValidateComments
Setter to control whether to validate whitespaces surrounding comments.- Parameters:
validateComments
-true
to validate surrounding whitespaces at comments.- Since:
- 6.19
-
getDefaultTokens
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
isCommentNodesRequired
Description copied from class:AbstractCheck
Whether comment nodes are required or not.- Overrides:
isCommentNodesRequired
in classAbstractCheck
- Returns:
- false as a default value.
-
beginTree
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitEachToken
Examines every sibling and child ofnode
for violations.- Parameters:
node
- The node to start examining.
-
isTextSeparatedCorrectlyFromPrevious
Checks if characters inline
at and aroundcolumnNo
has the correct number of spaces. to returntrue
the following conditions must be met:- the character at
columnNo
is the first in the line. - the character at
columnNo
is not separated by whitespaces from the previous non-whitespace character. - the character at
columnNo
is separated by only one whitespace from the previous non-whitespace character. -
validateComments
is disabled and the previous text is the end of a block comment.
- Parameters:
line
- Unicode code point array of line in the file to examine.columnNo
- The column position in theline
to examine.- Returns:
true
if the text atcolumnNo
is separated correctly from the previous token.
- the character at
-
isSingleSpace
Checks if theline
atcolumnNo
is a single space, and not preceded by another space.- Parameters:
line
- Unicode code point array of line in the file to examine.columnNo
- The column position in theline
to examine.- Returns:
true
if the character atcolumnNo
is a space, and not preceded by another space.
-
isSpace
Checks if theline
atcolumnNo
is a space.- Parameters:
line
- Unicode code point array of line in the file to examine.columnNo
- The column position in theline
to examine.- Returns:
true
if the character atcolumnNo
is a space.
-
isFirstInLine
Checks if theline
up to and includingcolumnNo
is all non-whitespace text encountered.- Parameters:
line
- Unicode code point array of line in the file to examine.columnNo
- The column position in theline
to examine.- Returns:
true
if the column position is the first non-whitespace text on theline
.
-
isBlockCommentEnd
Checks if theline
atcolumnNo
is the end of a comment, '*/'.- Parameters:
line
- Unicode code point array of line in the file to examine.columnNo
- The column position in theline
to examine.- Returns:
true
if the previous text is an end comment block.
-