From 6a2c34b5d8d465c99c829df1e6f13fe69cf86f64 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Sat, 23 Dec 2017 13:24:07 +0300 Subject: [PATCH] Fixed IDEA-143120 Javadoc HTML proper formatting merged HTML lines with "wrapped at right margin" option --- .../source/codeStyle/javadoc/JDParser.java | 28 ++++++--- .../formatter/java/JavadocFormatterTest.kt | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) 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 bc7ea5efc9fe..09774f141807 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 @@ -422,14 +422,8 @@ public class JDParser { result.add(Pair.create(s1, marks[i])); } else { - 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(s1, marks[i])); - } - else if (mySettings.JD_PRESERVE_LINE_FEEDS) { + if (s1.isEmpty() || s1.equals(SELF_CLOSED_P_TAG) || isKeepLineFeedsIn(s1)) { + endParagraph(result, sb); result.add(Pair.create(s1, marks[i])); } else { @@ -444,6 +438,24 @@ public class JDParser { return result; } + private boolean isKeepLineFeedsIn(@NotNull String line) { + return mySettings.JD_PRESERVE_LINE_FEEDS || startsWithTag(line); + } + + private static boolean startsWithTag(@NotNull String line) { + if (line.trim().startsWith("<")) { + return line.matches("\\s*.*"); + } + return false; + } + + private static void endParagraph(@NotNull List> result, @NotNull StringBuilder sb) { + if (sb.length() > 0) { + result.add(new Pair<>(sb.toString(), false)); + sb.setLength(0); + } + } + private interface TagParser { boolean parse(String tag, String line, JDComment c); } diff --git a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavadocFormatterTest.kt b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavadocFormatterTest.kt index 1f748f78c6d3..3d9568c233b1 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavadocFormatterTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavadocFormatterTest.kt @@ -1023,4 +1023,63 @@ module M { module M { }""") } + + fun testRichHtml() { + getSettings().WRAP_COMMENTS = true + getSettings().RIGHT_MARGIN = 50 + getJavaSettings().JD_ADD_BLANK_AFTER_DESCRIPTION = false + doTextTest( + """public class Test { + /** + *

A description containing HTML tags

+ *

+ * There might be lists in descriptions like this one: + *

+ * which should be left as is, without any tags merged. + *

+ * @param a Parameter descriptions can also be long but tag + * content should be left intact: + *
    + *
  1. Another item one
  2. + *
  3. Item two
  4. + *
+ */ + void test(int a) { + } +} +""", + + """public class Test { + /** + *

A description containing HTML + * tags

+ *

+ * There might be lists in descriptions like + * this one: + *

+ * which should be left as is, without any + * tags merged. + *

+ * @param a Parameter descriptions can also be + * long but tag content should be + * left intact: + *
    + *
  1. Another item one
  2. + *
  3. Item two
  4. + *
+ */ + void test(int a) { + } +} +""" + ) + } } \ No newline at end of file