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.
This commit is contained in:
Yaroslav Lepenkin
2016-03-04 15:33:34 +03:00
parent d02b9b6ce6
commit bba4324bab
3 changed files with 61 additions and 11 deletions
@@ -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);
@@ -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<String> 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<String> subList = toArrayWrapping(str, rightMargin - prefix.length());
List<String> subList = toArrayWrapping(str, maxCommentLength);
//removing pre tag
if (unclosedPreTag && subList != null && !subList.isEmpty()) {
@@ -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;