Class SingleSpaceSeparatorCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class SingleSpaceSeparatorCheck extends AbstractCheck
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
validateCommentsto true.Setting
validateCommentsto 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.
To configure the check:
<module name="SingleSpaceSeparator"/>
Example:
int foo() { // violation, 3 whitespaces return 1; // violation, 2 whitespaces } int fun1() { // OK, 1 whitespace return 3; // OK, 1 whitespace } void fun2() {} // violation, 2 whitespacesTo configure the check so that it validates comments:
<module name="SingleSpaceSeparator"> <property name="validateComments" value="true"/> </module>
Example:
void fun1() {} // violation, 2 whitespaces before the comment starts void fun2() { return; } /* violation here, 2 whitespaces before the comment starts */ /* violation, 2 whitespaces after the comment ends */ int a; String s; /* OK, 1 whitespace */ /** * This is a Javadoc comment */ int b; // violation, 2 whitespaces after the javadoc comment ends float f1; // OK, 1 whitespace /** * OK, 1 white space after the doc comment ends */ float f2;Parent is
com.puppycrawl.tools.checkstyle.TreeWalkerViolation Message Keys:
-
single.space.separator
- Since:
- 6.19
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private booleanvalidateCommentsControl whether to validate whitespaces surrounding comments.
-
Constructor Summary
Constructors Constructor Description SingleSpaceSeparatorCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.int[]getAcceptableTokens()The configurable token set.int[]getDefaultTokens()Returns the default token a check is interested in.int[]getRequiredTokens()The tokens that this check must be registered for.private static booleanisBlockCommentEnd(int[] line, int columnNo)Checks if thelineatcolumnNois the end of a comment, '*/'.booleanisCommentNodesRequired()Whether comment nodes are required or not.private static booleanisFirstInLine(int[] line, int columnNo)Checks if thelineup to and includingcolumnNois all non-whitespace text encountered.private static booleanisSingleSpace(int[] line, int columnNo)Checks if thelineatcolumnNois a single space, and not preceded by another space.private static booleanisSpace(int[] line, int columnNo)Checks if thelineatcolumnNois a space.private booleanisTextSeparatedCorrectlyFromPrevious(int[] line, int columnNo)Checks if characters inlineat and aroundcolumnNohas the correct number of spaces.voidsetValidateComments(boolean validateComments)Setter to control whether to validate whitespaces surrounding comments.private voidvisitEachToken(DetailAST node)Examines every sibling and child ofnodefor violations.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, 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.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
validateComments
private boolean validateComments
Control whether to validate whitespaces surrounding comments.
-
-
Constructor Detail
-
SingleSpaceSeparatorCheck
public SingleSpaceSeparatorCheck()
-
-
Method Detail
-
setValidateComments
public void setValidateComments(boolean validateComments)
Setter to control whether to validate whitespaces surrounding comments.- Parameters:
validateComments-trueto validate surrounding whitespaces at comments.
-
getDefaultTokens
public int[] 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:
TokenTypes
-
getAcceptableTokens
public int[] 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:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
isCommentNodesRequired
public boolean isCommentNodesRequired()
Description copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
isCommentNodesRequiredin classAbstractCheck- Returns:
- false as a default value.
-
beginTree
public void beginTree(DetailAST rootAST)
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:
rootAST- the root of the tree
-
visitEachToken
private void visitEachToken(DetailAST node)
Examines every sibling and child ofnodefor violations.- Parameters:
node- The node to start examining.
-
isTextSeparatedCorrectlyFromPrevious
private boolean isTextSeparatedCorrectlyFromPrevious(int[] line, int columnNo)
Checks if characters inlineat and aroundcolumnNohas the correct number of spaces. to returntruethe following conditions must be met:- the character at
columnNois the first in the line. - the character at
columnNois not separated by whitespaces from the previous non-whitespace character. - the character at
columnNois separated by only one whitespace from the previous non-whitespace character. -
validateCommentsis 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 thelineto examine.- Returns:
trueif the text atcolumnNois separated correctly from the previous token.
- the character at
-
isSingleSpace
private static boolean isSingleSpace(int[] line, int columnNo)
Checks if thelineatcolumnNois 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 thelineto examine.- Returns:
trueif the character atcolumnNois a space, and not preceded by another space.
-
isSpace
private static boolean isSpace(int[] line, int columnNo)
Checks if thelineatcolumnNois a space.- Parameters:
line- Unicode code point array of line in the file to examine.columnNo- The column position in thelineto examine.- Returns:
trueif the character atcolumnNois a space.
-
isFirstInLine
private static boolean isFirstInLine(int[] line, int columnNo)
Checks if thelineup to and includingcolumnNois all non-whitespace text encountered.- Parameters:
line- Unicode code point array of line in the file to examine.columnNo- The column position in thelineto examine.- Returns:
trueif the column position is the first non-whitespace text on theline.
-
isBlockCommentEnd
private static boolean isBlockCommentEnd(int[] line, int columnNo)
Checks if thelineatcolumnNois the end of a comment, '*/'.- Parameters:
line- Unicode code point array of line in the file to examine.columnNo- The column position in thelineto examine.- Returns:
trueif the previous text is a end comment block.
-
-