Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #11166: remove getFileContents from EmptyLineSeparator #14819

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/suppressions.xml
Expand Up @@ -62,6 +62,8 @@
<suppress checks="MethodCount" files="[\\/]UnusedLocalVariableCheck.java"/>
<!-- Utility class is combination of a lot of different methods . -->
<suppress checks="MethodCount" files="[\\/]SiteUtil.java"/>
<!-- a lot of small methods for a better readability. -->
<suppress checks="MethodCount" files="[\\/]EmptyLineSeparatorCheck.java"/>
<!-- parse method needs catching Exceptions to print context of execution -->
<suppress checks="IllegalCatch" files="[\\/]src[\\/]test[\\/].*[\\/]InlineConfigParser\.java"/>
<!-- exception maybe thrown while executing the static block -->
Expand Down
Expand Up @@ -622,19 +622,16 @@ private boolean isPrePreviousLineEmpty(DetailAST token) {
* @return true if token have empty line after.
*/
private boolean hasEmptyLineAfter(DetailAST token) {
DetailAST lastToken = token.getLastChild().getLastChild();
if (lastToken == null) {
lastToken = token.getLastChild();
DetailAST currentEnd = token.getLastChild().getLastChild();
if (currentEnd == null) {
currentEnd = token.getLastChild();
}
DetailAST nextToken = token.getNextSibling();
if (TokenUtil.isCommentType(nextToken.getType())) {
nextToken = nextToken.getNextSibling();
DetailAST nextBegin = token.getNextSibling();
if (TokenUtil.isCommentType(nextBegin.getType())) {
nextBegin = nextBegin.getNextSibling();
}
// Start of the next token
final int nextBegin = nextToken.getLineNo();
// End of current token.
final int currentEnd = lastToken.getLineNo();
return hasEmptyLine(currentEnd + 1, nextBegin - 1);

return hasEmptyLine(currentEnd, nextBegin);
}

/**
Expand All @@ -652,28 +649,62 @@ private static Optional<DetailAST> findCommentUnder(DetailAST packageDef) {
}

/**
* Checks, whether there are empty lines within the specified line range. Line numbering is
* started from 1 for parameter values
* Checks, whether there are empty lines between two tokens.
*
* @param startLine number of the first line in the range
* @param endLine number of the second line in the range
* @return {@code true} if found any blank line within the range, {@code false}
* @param currentEnd the last token of the current subtree
* @param nextBegin the first token of the next subtree
* @return {@code true} if found any blank line, {@code false}
* otherwise
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean hasEmptyLine(int startLine, int endLine) {
// Initial value is false - blank line not found
boolean result = false;
final FileContents fileContents = getFileContents();
for (int line = startLine; line <= endLine; line++) {
// Check, if the line is blank. Lines are numbered from 0, so subtract 1
if (fileContents.lineIsBlank(line - 1)) {
result = true;
break;
private boolean hasEmptyLine(DetailAST currentEnd, DetailAST nextBegin) {
final int currentEndLineNo = currentEnd.getLineNo();
final int nextBeginLineNo = nextBegin.getLineNo();
final int linesBetween = nextBeginLineNo - currentEndLineNo - 1;

return getCommentsLineCount(nextBegin, currentEnd) < linesBetween;
}

/**
* Get the number of commented lines between the given token and the previous token.
*
* @param currentToken the current token
* @param previousToken the previous token
* @return the number of comment lines between the current token and the previous token
*/
private int getCommentsLineCount(DetailAST currentToken, DetailAST previousToken) {
int commentsLineCount = 0;
DetailAST commentAst = findFirstCommentToken(currentToken);

while (commentAst != null && TokenUtil.isCommentType(commentAst.getType())) {
if (TokenUtil.areOnSameLine(commentAst, previousToken)
|| TokenUtil.areOnSameLine(commentAst, currentToken)) {
commentAst = commentAst.getNextSibling();
continue;
}
final DetailAST commentEnd = commentAst.getLastChild();
commentsLineCount += commentEnd.getLineNo() - commentAst.getLineNo() + 1;
commentAst = commentAst.getNextSibling();
}
return result;
return commentsLineCount;
}

/**
* Finds the first comment token in the subtree of the given token.
*
* @param ast token to start the search
* @return the first comment token, or {@code null} if not found
*/
private DetailAST findFirstCommentToken(DetailAST ast) {
final DetailAST modifersAst = ast.findFirstToken(TokenTypes.MODIFIERS);
final DetailAST commentAst;

if (modifersAst != null) {
commentAst = modifersAst.getFirstChild();
}
else {
commentAst = ast.getFirstChild();
}
return commentAst;
Comment on lines +697 to +707
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will work more on this method. I believe that there are more cases that need to be handled to get the first comment token

}

/**
Expand Down
Expand Up @@ -43,7 +43,7 @@ class InputEmptyLineSeparator // violation ''CLASS_DEF' should be separated from
// one blank line - ok
{
//empty instance initializer
}
} /* . */
// no blank line - fail
/**
*
Expand Down