Replace empty lines with <p> instead of <p/> (IDEA-145373), however preserve existing <p/>.

According to documentation "If you have more than one paragraph in the doc comment, separate the paragraphs with a <p> paragraph tag, as shown." and it has not changed since 2010, also javadoc tool from jdk8 fails with error "error: self-closing element not allowed", that is why we now generate <p>
This commit is contained in:
Yaroslav Lepenkin
2015-10-02 16:53:51 +03:00
parent 2f73226bfe
commit 8736db12bc
4 changed files with 48 additions and 39 deletions
@@ -49,7 +49,7 @@ public class CommentFormatter {
public CommentFormatter(@NotNull Project project) {
mySettings = CodeStyleSettingsManager.getSettings(project);
myParser = new JDParser(mySettings, LanguageLevelProjectExtension.getInstance(project).getLanguageLevel());
myParser = new JDParser(mySettings);
myProject = project;
}
@@ -45,11 +45,9 @@ public class JDParser {
private static final char lineSeparator = '\n';
private final CodeStyleSettings mySettings;
private final LanguageLevel myLanguageLevel;
public JDParser(@NotNull CodeStyleSettings settings, @NotNull LanguageLevel languageLevel) {
public JDParser(@NotNull CodeStyleSettings settings) {
mySettings = settings;
myLanguageLevel = languageLevel;
}
public void formatCommentText(@NotNull PsiElement element, @NotNull CommentFormatter formatter) {
@@ -253,7 +251,7 @@ public class JDParser {
first = true;
if (p2nl) {
if (isParaTag(token) && s.indexOf(P_END_TAG, curPos) < 0) {
list.add("");
list.add(isSelfClosedPTag(token) ? SELF_CLOSED_P_TAG : "");
markers.add(Boolean.valueOf(preCount > 0));
continue;
}
@@ -277,6 +275,11 @@ public class JDParser {
String withoutWS = removeWhiteSpacesFrom(token).toLowerCase();
return withoutWS.equals(SELF_CLOSED_P_TAG) || withoutWS.equals(P_START_TAG);
}
private static boolean isSelfClosedPTag(@NotNull final String token) {
return removeWhiteSpacesFrom(token).toLowerCase().equals(SELF_CLOSED_P_TAG);
}
@NotNull
private static String removeWhiteSpacesFrom(@NotNull final String token) {
@@ -379,12 +382,12 @@ public class JDParser {
result.add(Pair.create(s1, marks[i]));
}
else {
if (s1.isEmpty()) {
if (s1.isEmpty() || s1.equals(SELF_CLOSED_P_TAG)) {
if (sb.length() != 0) {
result.add(new Pair<String, Boolean>(sb.toString(), false));
sb.setLength(0);
}
result.add(Pair.create("", marks[i]));
result.add(Pair.create(s1, marks[i]));
}
else if (mySettings.JD_PRESERVE_LINE_FEEDS) {
result.add(Pair.create(s1, marks[i]));
@@ -596,13 +599,7 @@ public class JDParser {
if (line.isEmpty() && !mySettings.JD_KEEP_EMPTY_LINES) continue;
if (i != 0) sb.append(prefix);
if (line.isEmpty() && mySettings.JD_P_AT_EMPTY_LINES && !insidePreTag) {
if (myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
//Self-closing elements are not allowed for javadoc tool from JDK8
sb.append(P_START_TAG);
}
else {
sb.append(SELF_CLOSED_P_TAG);
}
sb.append(P_START_TAG);
}
else {
sb.append(line);
@@ -227,9 +227,11 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
getSettings().getRootSettings().WRAP_COMMENTS = true;
getSettings().RIGHT_MARGIN = 20;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
doTextTest("/**\n" + " * <p />\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}",
doTextTest("/**\n" +
" * <p />\n" +
" * Another paragraph of the description placed after blank line.\n" +
" */\n" +
"class A{}",
"/**\n" +
" * <p/>\n" +
" * Another paragraph\n" +
@@ -240,26 +242,36 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"class A {\n" +
"}");
}
public void testSCR2632_JDK8_LanguageLevel() throws Exception {
public void test_PreserveExistingSelfClosingTags_AndGenerateOnlyPTag() {
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
getSettings().getRootSettings().WRAP_COMMENTS = true;
getSettings().RIGHT_MARGIN = 20;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
doTextTest("/**\n" + " * <p />\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}",
"/**\n" +
" * <p>\n" +
" * Another paragraph\n" +
" * of the description\n" +
" * placed after\n" +
" * blank line.\n" +
" */\n" +
"class A {\n" +
"}");
LanguageLevel before = LanguageLevelProjectExtension.getInstance(getProject()).getLanguageLevel();
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
try {
doTextTest(
"/**\n" +
" * My test comment\n" +
" * <p/>\n" +
" * \n" +
" * With empty line\n" +
" */\n" +
"class T {\n" +
"}",
"/**\n" +
" * My test comment\n" +
" * <p/>\n" +
" * <p>\n" +
" * With empty line\n" +
" */\n" +
"class T {\n" +
"}"
);
}
finally {
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(before);
}
}
public void testParagraphTagGeneration() {
// Inspired by IDEA-61811
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
@@ -280,7 +292,7 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"}",
"/**\n" +
" * line 1\n" +
" * <p/>\n" +
" * <p>\n" +
" * line 2\n" +
" * <pre>\n" +
" * line 3\n" +
@@ -793,7 +805,7 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
doClassTest(before, after);
}
public void testGenerateSelfClosingPTagIfLanguageLevelNotJava8() throws Exception {
public void testPTagIfLanguageLevelNotJava8() throws Exception {
getSettings().getRootSettings().JD_P_AT_EMPTY_LINES = true;
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
@@ -806,7 +818,7 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"}\n";
String after = "/**\n" +
" * Super method\n" +
" * <p/>\n" +
" * <p>\n" +
" * Super multiple times\n" +
" */\n" +
"public void voo() {\n" +
@@ -483,7 +483,7 @@ checkbox.keep.empty.throws.tags=Keep empty @throws tags
checkbox.enable.leading.asterisks=Enable leading asterisks
checkbox.use.throws.rather.than.exception=Use @throws rather than @exception
checkbox.wrap.at.right.margin=Wrap at right margin
checkbox.generate.p.on.empty.lines=Generate "<p/>" on empty lines
checkbox.generate.p.on.empty.lines=Generate "<p>" on empty lines
checkbox.keep.empty.lines=Keep empty lines
checkbox.do.not.wrap.one.line.comments=Do not wrap one line comments
checkbox.preserve.line.feeds=Preserve line feeds