Skip to content

Commit

Permalink
Issue checkstyle#10924: Checks that use getLine() should check code p…
Browse files Browse the repository at this point in the history
…oints for spaces
  • Loading branch information
MUzairS15 committed Nov 26, 2021
1 parent 66b7b00 commit 378388f
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ public final String getLine(int index) {
return context.get().fileContents.getLine(index);
}

/**
* Returns the line associated with the tree.
*
* @param line Unicode code points of required line
* @return the array of Unicode code points
*/
public static final int[] getCodePoints(String line) {
return line.codePoints().toArray();
}

/**
* The actual context holder.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,28 +447,66 @@ private void verifyBrace(final DetailAST brace,
// Check for being told to ignore, or have '{}' which is a special case
if (braceLine.length() <= brace.getColumnNo() + 1
|| braceLine.charAt(brace.getColumnNo() + 1) != '}') {
final int[] codePoints = getCodePoints(braceLine);
if (option == LeftCurlyOption.NL) {
if (!CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
if (!hasWhitespaceBefore(codePoints, brace.getColumnNo())) {
log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
}
}
else if (option == LeftCurlyOption.EOL) {
validateEol(brace, braceLine);
validateEol(codePoints, brace);
}
else if (!TokenUtil.areOnSameLine(startToken, brace)) {
validateNewLinePosition(brace, startToken, braceLine);
validateNewLinePosition(codePoints, brace, startToken);
}
}
}

/**
* Returns whether the specified string contains only whitespace up to the specified index.
*
* @param codePoints
* array of Unicode code point
* @param index
* index to check up to
* @return whether there is only whitespace
*/
public static boolean hasWhitespaceBefore(int[] codePoints, int index) {
boolean result = true;
for (int i = 0; i < index; i++) {
if (!isWhitespace(codePoints, i)) {
result = false;
break;
}
}
return result;
}

/**
* Converts the Unicode code point at index {@code index} to it's UTF-16
* representation, then checks if the character is whitespace. Note that the given
* index {@code index} should correspond to the location of the character
* to check in the string, not in code points.
*
* @param codePoints the array of Unicode code points
* @param index the index of the character to check
* @return true if character at {@code index} is whitespace
*/
private static boolean isWhitespace(int[] codePoints, int index) {
// We only need to check the first member of a surrogate pair to verify that
// it is not whitespace.
final char character = Character.toChars(codePoints[index])[0];
return Character.isWhitespace(character);
}

/**
* Validate EOL case.
*
* @param brace brace AST
* @param braceLine line content
* @param codePoints the array of Unicode code points
*/
private void validateEol(DetailAST brace, String braceLine) {
if (CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
private void validateEol(int[] codePoints, DetailAST brace) {
if (hasWhitespaceBefore(codePoints, brace.getColumnNo())) {
log(brace, MSG_KEY_LINE_PREVIOUS, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
}
if (!hasLineBreakAfter(brace)) {
Expand All @@ -481,19 +519,19 @@ private void validateEol(DetailAST brace, String braceLine) {
*
* @param brace brace AST
* @param startToken start Token
* @param braceLine content of line with Brace
* @param codePoints the array of Unicode code points
*/
private void validateNewLinePosition(DetailAST brace, DetailAST startToken, String braceLine) {
private void validateNewLinePosition(int[] codePoints, DetailAST brace, DetailAST startToken) {
// not on the same line
if (startToken.getLineNo() + 1 == brace.getLineNo()) {
if (CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
if (hasWhitespaceBefore(codePoints, brace.getColumnNo())) {
log(brace, MSG_KEY_LINE_PREVIOUS, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
}
else {
log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
}
}
else if (!CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
else if (!hasWhitespaceBefore(codePoints, brace.getColumnNo())) {
log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,47 @@ private static boolean shouldBeAloneOnLineWithNotAloneOption(Details details,
private static boolean isAloneOnLine(Details details, String targetSrcLine) {
final DetailAST rcurly = details.rcurly;
final DetailAST nextToken = details.nextToken;
final int[] codePoints = getCodePoints(targetSrcLine);
return (nextToken == null || !TokenUtil.areOnSameLine(rcurly, nextToken)
|| skipDoubleBraceInstInit(details))
&& CommonUtil.hasWhitespaceBefore(details.rcurly.getColumnNo(),
targetSrcLine);
&& hasWhitespaceBefore(codePoints, details.rcurly.getColumnNo());
}

/**
* Converts the Unicode code point at index {@code index} to it's UTF-16
* representation, then checks if the character is whitespace. Note that the given
* index {@code index} should correspond to the location of the character
* to check in the string, not in code points.
*
* @param codePoints the array of Unicode code points
* @param index the index of the character to check
* @return true if character at {@code index} is whitespace
*/
private static boolean isWhitespace(int[] codePoints, int index) {
// We only need to check the first member of a surrogate pair to verify that
// it is not whitespace.
final char character = Character.toChars(codePoints[index])[0];
return Character.isWhitespace(character);
}

/**
* Returns whether the specified string contains only whitespace up to the specified index.
*
* @param codePoints
* array of Unicode code point
* @param index
* index to check up to
* @return whether there is only whitespace
*/
public static boolean hasWhitespaceBefore(int[] codePoints, int index) {
boolean result = true;
for (int i = 0; i < index; i++) {
if (!isWhitespace(codePoints, i)) {
result = false;
break;
}
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,46 @@ private boolean isTrailingComment(DetailAST comment) {
*/
private boolean isTrailingSingleLineComment(DetailAST singleLineComment) {
final String targetSourceLine = getLine(singleLineComment.getLineNo() - 1);
final int[] codePoints = getCodePoints(targetSourceLine);
final int commentColumnNo = singleLineComment.getColumnNo();
return !CommonUtil.hasWhitespaceBefore(commentColumnNo, targetSourceLine);
return !hasWhitespaceBefore(codePoints, commentColumnNo);
}

/**
* Converts the Unicode code point at index {@code index} to it's UTF-16
* representation, then checks if the character is whitespace. Note that the given
* index {@code index} should correspond to the location of the character
* to check in the string, not in code points.
*
* @param codePoints the array of Unicode code points
* @param index the index of the character to check
* @return true if character at {@code index} is whitespace
*/
private static boolean isWhitespace(int[] codePoints, int index) {
// We only need to check the first member of a surrogate pair to verify that
// it is not whitespace.
final char character = Character.toChars(codePoints[index])[0];
return Character.isWhitespace(character);
}

/**
* Returns whether the specified string contains only whitespace up to the specified index.
*
* @param codePoints
* array of Unicode code point
* @param index
* index to check up to
* @return whether there is only whitespace
*/
public static boolean hasWhitespaceBefore(int[] codePoints, int index) {
boolean result = true;
for (int i = 0; i < index; i++) {
if (!isWhitespace(codePoints, i)) {
result = false;
break;
}
}
return result;
}

/**
Expand All @@ -1164,9 +1202,10 @@ private boolean isTrailingSingleLineComment(DetailAST singleLineComment) {
*/
private boolean isTrailingBlockComment(DetailAST blockComment) {
final String commentLine = getLine(blockComment.getLineNo() - 1);
final int[] codePoints = getCodePoints(commentLine);
final int commentColumnNo = blockComment.getColumnNo();
final DetailAST nextSibling = blockComment.getNextSibling();
return !CommonUtil.hasWhitespaceBefore(commentColumnNo, commentLine)
return !hasWhitespaceBefore(codePoints, commentColumnNo)
|| nextSibling != null && TokenUtil.areOnSameLine(nextSibling, blockComment);
}

Expand Down

0 comments on commit 378388f

Please sign in to comment.