IDEA-66583 "blank line after anonymous class header" does not work with comment

This commit is contained in:
Denis Zhdanov
2011-03-28 19:48:04 +04:00
parent b9c689ca74
commit 6daecc2141
2 changed files with 37 additions and 0 deletions
@@ -23,6 +23,7 @@ import com.intellij.lexer.Lexer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettings;
@@ -238,6 +239,19 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
else if (myRole2 == ChildRole.RBRACE && aClass.isEnum()) {
createParenthSpace(true, false);
}
else if (aClass instanceof PsiAnonymousClass && ElementType.JAVA_PLAIN_COMMENT_BIT_SET.contains(myChild1.getElementType())) {
ASTNode prev = myChild1.getTreePrev();
if (prev.getElementType() == JavaTokenType.WHITE_SPACE && !StringUtil.containsLineBreak(prev.getChars())) {
prev = prev.getTreePrev();
}
if (prev.getElementType() == JavaTokenType.LBRACE) {
myResult = Spacing.createSpacing(0, 0, mySettings.BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER + 1,
mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_DECLARATIONS);
}
else {
processClassBody();
}
}
else processClassBody();
}
@@ -330,6 +330,29 @@ public class JavaFormatterBlankLinesTest extends AbstractJavaFormatterTest {
);
}
public void testAfterAnonymousClassWhereCodeBlockStartsWithComment() throws Exception {
// Inspired by IDEA-66583
getSettings().BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER = 0;
String textWithWhiteSpaceBetweenCommentAndLbrace =
"Object object = new Object() { // comment breaks \"blank line after anonymous class header\"\n" +
" @Override\n" +
" public String toString() {\n" +
" return super.toString();\n" +
" }\n" +
"};";
doMethodTest(textWithWhiteSpaceBetweenCommentAndLbrace, textWithWhiteSpaceBetweenCommentAndLbrace);
String textWithoutWhiteSpaceBetweenCommentAndLbrace =
"Object object = new Object() {// comment breaks \"blank line after anonymous class header\"\n" +
" @Override\n" +
" public String toString() {\n" +
" return super.toString();\n" +
" }\n" +
"};";
doMethodTest(textWithoutWhiteSpaceBetweenCommentAndLbrace, textWithoutWhiteSpaceBetweenCommentAndLbrace);
}
public void testBeforeMethodBody() {
// Inspired by IDEA-54747
getSettings().BLANK_LINES_BEFORE_METHOD_BODY = 3;