From bba4324babdfb6b5bc53a9902093f69ee310cae6 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Fri, 4 Mar 2016 13:48:54 +0300 Subject: [PATCH] Do not wrap javadoc description if every line is shorter than description (IDEA-152431) Current wrapping approach sadly merges all lines and then tries to reflow text. --- .../source/codeStyle/javadoc/JDComment.java | 2 +- .../source/codeStyle/javadoc/JDParser.java | 46 +++++++++++++++---- .../formatter/java/JavadocFormatterTest.java | 24 ++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDComment.java b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDComment.java index 4baf63c4a3e8..d79c018139d5 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDComment.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/javadoc/JDComment.java @@ -71,7 +71,7 @@ public class JDComment { if (!isNull(myDescription)) { sb.append(prefix); - sb.append(myFormatter.getParser().formatJDTagDescription(myDescription, prefix)); + sb.append(myFormatter.getParser().formatJDTagDescription(myDescription, prefix, false)); if (myFormatter.getSettings().JD_ADD_BLANK_AFTER_DESCRIPTION) { sb.append(prefix); 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 cf4a353ce049..7afafddf8101 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 @@ -18,7 +18,6 @@ package com.intellij.psi.impl.source.codeStyle.javadoc; import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.javadoc.PsiDocComment; @@ -280,6 +279,18 @@ public class JDParser { return removeWhiteSpacesFrom(token).toLowerCase().equals(SELF_CLOSED_P_TAG); } + private static boolean hasLineLongerThan(String str, int maxLength) { + if (str == null) return false; + + for (String s : str.split("\n")) { + if (s.length() > maxLength) { + return true; + } + } + + return false; + } + @NotNull private static String removeWhiteSpacesFrom(@NotNull final String token) { @@ -520,13 +531,15 @@ public class JDParser { }, }; - /** - * @see JDParser#formatJDTagDescription(String, CharSequence, boolean, int) - */ @NotNull protected StringBuilder formatJDTagDescription(@Nullable String s, @NotNull CharSequence prefix) { return formatJDTagDescription(s, prefix, false, 0); } + + @NotNull + protected StringBuilder formatJDTagDescription(@Nullable String s, @NotNull CharSequence prefix, boolean wrapLinesShorterRightMargin) { + return formatJDTagDescription(s, prefix, false, 0, wrapLinesShorterRightMargin); + } private static boolean lineHasUnclosedPreTag(@NotNull String line) { return StringUtil.getOccurrenceCount(line, PRE_TAG_START) > StringUtil.getOccurrenceCount(line, PRE_TAG_END); @@ -536,6 +549,14 @@ public class JDParser { return StringUtil.getOccurrenceCount(line, PRE_TAG_END) > StringUtil.getOccurrenceCount(line, PRE_TAG_START); } + @NotNull + protected StringBuilder formatJDTagDescription(@Nullable String str, + @NotNull CharSequence prefix, + boolean firstLineShorter, + int firstLinePrefixLength) { + return formatJDTagDescription(str, prefix, firstLineShorter, firstLinePrefixLength, true); + } + /** * Returns formatted JavaDoc tag description, according to selected configuration * @param str JavaDoc tag description @@ -548,15 +569,20 @@ public class JDParser { protected StringBuilder formatJDTagDescription(@Nullable String str, @NotNull CharSequence prefix, boolean firstLineShorter, - int firstLinePrefixLength) + int firstLinePrefixLength, + boolean isWrapLinesShorterRightMargin) { - int rightMargin = mySettings.getRightMargin(JavaLanguage.INSTANCE); + final int rightMargin = mySettings.getRightMargin(JavaLanguage.INSTANCE); + final int maxCommentLength = rightMargin - prefix.length(); + StringBuilder sb = new StringBuilder(); List list; - + + boolean canWrap = isWrapLinesShorterRightMargin || hasLineLongerThan(str, maxCommentLength); + //If wrap comments selected, comments should be wrapped by the right margin - if (mySettings.WRAP_COMMENTS) { - list = toArrayWrapping(str, rightMargin - prefix.length()); + if (mySettings.WRAP_COMMENTS && canWrap) { + list = toArrayWrapping(str, maxCommentLength); if (firstLineShorter && list != null && !list.isEmpty() @@ -575,7 +601,7 @@ public class JDParser { } //getting all another lines according to their prefix - List subList = toArrayWrapping(str, rightMargin - prefix.length()); + List subList = toArrayWrapping(str, maxCommentLength); //removing pre tag if (unclosedPreTag && subList != null && !subList.isEmpty()) { 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 ee8b628cd478..ae4d0e7c960f 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 @@ -527,6 +527,30 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest { doClassTest(before, after); } + public void testDoNotMergeCommentLines() { + getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true; + getSettings().getRootSettings().WRAP_COMMENTS = true; + + doClassTest( + "/**\n" + + " * Some comment\n" + + " * 2016\n" + + " * Date: Mar 03, 2016\n" + + " *\n" + + " */\n" + + " public class TestCase {\n" + + "}", + + "/**\n" + + " * Some comment\n" + + " * 2016\n" + + " * Date: Mar 03, 2016\n" + + " */\n" + + "public class TestCase {\n" + + "}" + ); + } + public void testSeeTagAlignment() throws Exception { getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true; getSettings().RIGHT_MARGIN = 80;