From 8736db12bc1153f53f630f80b6cf52fcb73ee98f Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Fri, 2 Oct 2015 16:29:03 +0300 Subject: [PATCH] Replace empty lines with

instead of

(IDEA-145373), however preserve existing

. According to documentation "If you have more than one paragraph in the doc comment, separate the paragraphs with a

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

--- .../codeStyle/javadoc/CommentFormatter.java | 2 +- .../source/codeStyle/javadoc/JDParser.java | 23 ++++--- .../formatter/java/JavadocFormatterTest.java | 60 +++++++++++-------- .../src/messages/ApplicationBundle.properties | 2 +- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/CommentFormatter.java b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/CommentFormatter.java index 8e3b75b6fab7..3845b2bb99c0 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/CommentFormatter.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/CommentFormatter.java @@ -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; } diff --git a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDParser.java b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDParser.java index 997c155e5aa3..cf4a353ce049 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDParser.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDParser.java @@ -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(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); diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavadocFormatterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavadocFormatterTest.java index 6ba198548f53..ee8b628cd478 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavadocFormatterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavadocFormatterTest.java @@ -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" + " *

\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}", + doTextTest("/**\n" + + " *

\n" + + " * Another paragraph of the description placed after blank line.\n" + + " */\n" + + "class A{}", "/**\n" + " *

\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" + " *

\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}", - "/**\n" + - " *

\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" + + " *

\n" + + " * \n" + + " * With empty line\n" + + " */\n" + + "class T {\n" + + "}", + "/**\n" + + " * My test comment\n" + + " *

\n" + + " *

\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" + - " *

\n" + + " *

\n" + " * line 2\n" + " *

\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" +
-                   " * 

\n" + + " *

\n" + " * Super multiple times\n" + " */\n" + "public void voo() {\n" + diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index f2d869de7904..9132547c25ee 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -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 "

" on empty lines +checkbox.generate.p.on.empty.lines=Generate "

" 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