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
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); }
validateComments - Control whether to validate whitespaces
surrounding comments.
Type is boolean.
Default value is false.
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 whitespaces
To 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.TreeWalker
Violation Message Keys:
single.space.separator
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
validateComments
Control whether to validate whitespaces surrounding comments.
|
| Constructor and Description |
|---|
SingleSpaceSeparatorCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(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 boolean |
isBlockCommentEnd(java.lang.String line,
int columnNo)
Checks if the
line at columnNo is the end of a comment,
'*/'. |
boolean |
isCommentNodesRequired()
Whether comment nodes are required or not.
|
private static boolean |
isFirstInLine(java.lang.String line,
int columnNo)
Checks if the
line up to and including columnNo is all
non-whitespace text encountered. |
private static boolean |
isSingleSpace(java.lang.String line,
int columnNo)
Checks if the
line at columnNo is a single space, and not
preceded by another space. |
private static boolean |
isSpace(java.lang.String line,
int columnNo)
Checks if the
line at columnNo is a space. |
private boolean |
isTextSeparatedCorrectlyFromPrevious(java.lang.String line,
int columnNo)
Checks if characters in
line at and around columnNo has
the correct number of spaces. |
private static boolean |
isWhitespace(java.lang.String line,
int columnNo)
Checks if the
line at columnNo is a whitespace character. |
void |
setValidateComments(boolean validateComments)
Setter to control whether to validate whitespaces surrounding comments.
|
private void |
visitEachToken(DetailAST node)
Examines every sibling and child of
node for violations. |
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitTokenfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private boolean validateComments
public SingleSpaceSeparatorCheck()
public void setValidateComments(boolean validateComments)
validateComments - true to validate surrounding whitespaces at comments.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic boolean isCommentNodesRequired()
AbstractCheckisCommentNodesRequired in class AbstractCheckpublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treeprivate void visitEachToken(DetailAST node)
node for violations.node - The node to start examining.private boolean isTextSeparatedCorrectlyFromPrevious(java.lang.String line, int columnNo)
line at and around columnNo has
the correct number of spaces. to return true the following
conditions must be met:
columnNo is the first in the line. columnNo is not separated by whitespaces from
the previous non-whitespace character. 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. line - The line in the file to examine.columnNo - The column position in the line to examine.true if the text at columnNo is separated
correctly from the previous token.private static boolean isSingleSpace(java.lang.String line, int columnNo)
line at columnNo is a single space, and not
preceded by another space.line - The line in the file to examine.columnNo - The column position in the line to examine.true if the character at columnNo is a space, and
not preceded by another space.private static boolean isSpace(java.lang.String line, int columnNo)
line at columnNo is a space.line - The line in the file to examine.columnNo - The column position in the line to examine.true if the character at columnNo is a space.private static boolean isWhitespace(java.lang.String line, int columnNo)
line at columnNo is a whitespace character.line - The line in the file to examine.columnNo - The column position in the line to examine.true if the character at columnNo is a
whitespace.private static boolean isFirstInLine(java.lang.String line, int columnNo)
line up to and including columnNo is all
non-whitespace text encountered.line - The line in the file to examine.columnNo - The column position in the line to examine.true if the column position is the first non-whitespace
text on the line.private static boolean isBlockCommentEnd(java.lang.String line, int columnNo)
line at columnNo is the end of a comment,
'*/'.line - The line in the file to examine.columnNo - The column position in the line to examine.true if the previous text is a end comment block.Copyright © 2001-2022. All Rights Reserved.