IDEA-109631 Javadocs formatting depends on current method indentation

This commit is contained in:
Denis.Zhdanov
2013-07-11 16:05:21 +04:00
parent 764617d741
commit 29d17b55c3
4 changed files with 247 additions and 72 deletions
@@ -1,9 +1,10 @@
class A {
/**
* The <code>String</code> class represents
* character strings. All string literals in Java
* programs, such as <code>"abc"</code>, are
* implemented as instances of this class.
* character strings. All string literals in
* Java programs, such as <code>"abc"</code>,
* are implemented as instances of this
* class.
* Some text after empty line
*
* @author Lee Boynton
@@ -39,6 +39,7 @@ import com.intellij.util.IncorrectOperationException;
import com.intellij.util.text.LineReader;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayInputStream;
import java.io.File;
@@ -129,35 +130,47 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
doTextTest(Action.REFORMAT, text, textAfter);
}
public void doTextTest(final Action action, final String text, String textAfter) throws IncorrectOperationException {
public void doTextTest(@NotNull final Action action, @NotNull final String text, @NotNull String textAfter) throws IncorrectOperationException {
final PsiFile file = createFile("A.java", text);
if (myLineRange != null) {
final DocumentImpl document = new DocumentImpl(text);
myTextRange =
new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
}
/*
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
performFormatting(file);
}
});
}
}, null, null);
assertEquals(prepareText(textAfter), prepareText(file.getText()));
*/
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
if (document == null) {
fail("Document is null");
return;
}
replaceAndProcessDocument(action, text, file, document);
assertEquals(textAfter, document.getText());
manager.commitDocument(document);
assertEquals(textAfter, file.getText());
}
public void formatEveryoneAndCheckIfResultEqual(@NotNull final String...before) {
assert before.length > 1;
final PsiFile file = createFile("A.java", "");
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
String afterFirst = replaceAndProcessDocument(Action.REFORMAT, before[0], file, document);
for (String nextBefore: before) {
assertEquals(afterFirst, replaceAndProcessDocument(Action.REFORMAT, nextBefore, file, document));
}
}
@NotNull
private String replaceAndProcessDocument(@NotNull final Action action,
@NotNull final String text,
@NotNull final PsiFile file,
@Nullable final Document document) throws IncorrectOperationException
{
if (document == null) {
fail("Don't expect the document to be null");
return null;
}
if (myLineRange != null) {
final DocumentImpl doc = new DocumentImpl(text);
myTextRange =
new TextRange(doc.getLineStartOffset(myLineRange.getStartOffset()), doc.getLineEndOffset(myLineRange.getEndOffset()));
}
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
@@ -181,15 +194,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
}
}, action == Action.REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "", "");
if (document == null) {
fail("Don't expect the document to be null");
return;
}
assertEquals(textAfter, document.getText());
manager.commitDocument(document);
assertEquals(textAfter, file.getText());
return document.getText();
}
public void doMethodTest(@NonNls final String before, @NonNls final String after) throws Exception {
@@ -352,4 +352,168 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
doClassTest(before, after);
}
public void testJavadocFormattingIndependentOfMethodIndentation() {
getCurrentCodeStyleSettings().RIGHT_MARGIN = 50;
getCurrentCodeStyleSettings().ENABLE_JAVADOC_FORMATTING = true;
getCurrentCodeStyleSettings().WRAP_COMMENTS = true;
getCurrentCodeStyleSettings().JD_LEADING_ASTERISKS_ARE_ENABLED = true;
getCurrentCodeStyleSettings().JD_P_AT_EMPTY_LINES = false;
getCurrentCodeStyleSettings().JD_KEEP_EMPTY_LINES = false;
getCurrentCodeStyleSettings().JD_ADD_BLANK_AFTER_DESCRIPTION = false;
String before1 = "class A {\n" +
" /**\n" +
" * Some really great independent test approach purpose live fish\n" +
" * banana split string be accurate when writing tests and code\n" +
" * read write buffer.\n" +
" *\n" +
" * Some text after empty line\n" +
" *\n" +
" */\n" +
"void foo() {\n" +
"\n" +
"}\n" +
"}";
String before2 = "class A {\n" +
" /**\n" +
" * Some really great independent test approach purpose live fish\n" +
" * banana split string be accurate when writing tests and code\n" +
" * read write buffer.\n" +
" *\n" +
" * Some text after empty line\n" +
" *\n" +
" */\n" +
" void foo() {\n" +
"\n" +
" }\n" +
"}";
formatEveryoneAndCheckIfResultEqual(before1, before2);
}
public void testJavadocAlignmentForInnerClasses() {
getCurrentCodeStyleSettings().RIGHT_MARGIN = 40;
getCurrentCodeStyleSettings().ENABLE_JAVADOC_FORMATTING = true;
getCurrentCodeStyleSettings().WRAP_COMMENTS = true;
getCurrentCodeStyleSettings().JD_LEADING_ASTERISKS_ARE_ENABLED = true;
String code = "public class Outer {\n" +
" class Inner {\n" +
" /**\n" +
" * Password from wild forest big house\n" +
" */\n" +
" public int getMagic() {\n" +
" return 312;\n" +
" }\n" +
"\n" +
"class InnerInner {\n" +
"/**\n" +
" * Special magic needs special rules\n" +
" */\n" +
"public int innerMagic() {\n" +
" return 1;\n" +
"}\n" +
"}\n" +
" }\n" +
"}";
String result = "public class Outer {\n" +
" class Inner {\n" +
" /**\n" +
" * Password from wild forest big\n" +
" * house\n" +
" */\n" +
" public int getMagic() {\n" +
" return 312;\n" +
" }\n" +
"\n" +
" class InnerInner {\n" +
" /**\n" +
" * Special magic needs\n" +
" * special rules\n" +
" */\n" +
" public int innerMagic() {\n" +
" return 1;\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
doTextTest(code, result);
}
public void testAlignmentWithNoTopClassMembersIndentation() {
getCurrentCodeStyleSettings().RIGHT_MARGIN = 40;
getCurrentCodeStyleSettings().WRAP_COMMENTS = true;
getCurrentCodeStyleSettings().JD_LEADING_ASTERISKS_ARE_ENABLED = true;
getCurrentCodeStyleSettings().getCommonSettings(JavaLanguage.INSTANCE).DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = true;
String before = "public class Outer {\n" +
"class Inner {\n" +
"/**\n" +
" * Password from wild forest big\n" +
" * house\n" +
" */\n" +
"public int getMagic() {\n" +
" return 312;\n" +
"}\n" +
"\n" +
"class InnerInner {\n" +
"/**\n" +
" * Special magic needs special rules\n" +
" */\n" +
"public int innerMagic() {\n" +
" return 1;\n" +
"}\n" +
"\n" +
"class InnerInnerInner {\n" +
"int iii;\n" +
"class TripleInner {\n" +
"int ti;\n" +
"}\n" +
"}\n" +
"}\n" +
"}\n" +
" public static void main(String[] args) {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"}";
String after = "public class Outer {\n" +
"class Inner {\n" +
" /**\n" +
" * Password from wild forest big\n" +
" * house\n" +
" */\n" +
" public int getMagic() {\n" +
" return 312;\n" +
" }\n" +
"\n" +
" class InnerInner {\n" +
" /**\n" +
" * Special magic needs special\n" +
" * rules\n" +
" */\n" +
" public int innerMagic() {\n" +
" return 1;\n" +
" }\n" +
"\n" +
" class InnerInnerInner {\n" +
" int iii;\n" +
"\n" +
" class TripleInner {\n" +
" int ti;\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n" +
"\n" +
"public static void main(String[] args) {\n" +
" System.out.println(\"AAA!\");\n" +
"}\n" +
"}";
doTextTest(before, after);
}
}