CommentFoldingUtil: do not add ellipsis if header includes all javadoc content (IDEA-216380)

GitOrigin-RevId: 677371c852f49a64d0f2ca008280152c953494bb
This commit is contained in:
Artemiy Sartakov
2019-07-26 12:05:43 +03:00
committed by intellij-monorepo-bot
parent 6a6455816e
commit 562b4afd1a
2 changed files with 74 additions and 19 deletions
@@ -180,6 +180,8 @@ public final class CommentFoldingUtil {
if (prefix == null || suffix == null || linePrefix == null) return null;
final String header = getCommentHeader(document, suffix, prefix, linePrefix, commentRange);
final String fullText = getCommentText(document, suffix, prefix, linePrefix, commentRange);
if (StringUtil.equalsIgnoreWhitespaces(header, fullText)) replacement = "";
return getCommentPlaceholder(prefix, suffix, header, replacement);
}
@@ -246,24 +248,40 @@ public final class CommentFoldingUtil {
@NotNull String linePrefix,
@NotNull TextRange commentRange) {
final int nFirstCommentLine = document.getLineNumber(commentRange.getStartOffset());
TextRange lineRange = getLineRange(document, nFirstCommentLine);
String line = getCommentLine(document, lineRange, commentRange, commentPrefix, commentSuffix);
if (line.chars().anyMatch(c -> !StringUtil.isWhiteSpace((char)c))) return line;
final int nSecondCommentLine = nFirstCommentLine + 1;
if (nSecondCommentLine >= document.getLineCount()) return "";
lineRange = getLineRange(document, nSecondCommentLine);
if (lineRange.getEndOffset() > commentRange.getEndOffset()) return "";
line = getCommentLine(document, lineRange, commentRange, linePrefix, commentSuffix);
if (line.chars().anyMatch(c -> !StringUtil.isWhiteSpace((char)c))) return line;
for (int i = 0; i <= 1; i++) {
final String line = getCommentLine(i, nFirstCommentLine, document, commentSuffix, commentPrefix, linePrefix, commentRange);
if (line == null) return "";
if (line.chars().anyMatch(c -> !StringUtil.isWhiteSpace((char)c))) return line;
}
return "";
}
/**
* Get comment text excluding prefixes and suffixes.
* If line contains whitespaces they will be included as well.
*
* @param document document with comment
* @param commentSuffix doc comment suffix
* @param commentPrefix doc comment prefix
* @param linePrefix prefix for doc comment line
* @param commentRange comment text range in document
*/
@NotNull
public static String getCommentText(@NotNull Document document,
@NotNull String commentSuffix,
@NotNull String commentPrefix,
@NotNull String linePrefix,
@NotNull TextRange commentRange) {
final StringBuilder sb = new StringBuilder();
final int nFirstCommentLine = document.getLineNumber(commentRange.getStartOffset());
for (int i = 0; ; i++) {
final String line = getCommentLine(i, nFirstCommentLine, document, commentSuffix, commentPrefix, linePrefix, commentRange);
if (line == null) break;
sb.append(line);
}
return sb.toString();
}
@NotNull
@Contract("_, _ -> new")
private static TextRange getLineRange(@NotNull Document document, int nLine) {
@@ -272,6 +290,27 @@ public final class CommentFoldingUtil {
return new TextRange(startOffset, endOffset);
}
@Nullable
private static String getCommentLine(int lineOffset,
int nFirstCommentLine,
@NotNull Document document,
@NotNull String commentSuffix,
@NotNull String commentPrefix,
@NotNull String linePrefix,
@NotNull TextRange commentRange) {
if (lineOffset == 0) {
final TextRange lineRange = getLineRange(document, nFirstCommentLine);
return getCommentLine(document, lineRange, commentRange, commentPrefix, commentSuffix);
}
final int nCommentLine = nFirstCommentLine + lineOffset;
if (nCommentLine >= document.getLineCount()) return null;
final TextRange lineRange = getLineRange(document, nCommentLine);
if (lineRange.getEndOffset() > commentRange.getEndOffset()) return null;
return getCommentLine(document, lineRange, commentRange, linePrefix, commentSuffix);
}
@NotNull
private static String getCommentLine(@NotNull Document document,
@NotNull TextRange lineRange,
@@ -18,12 +18,12 @@ class Test {
int j = i;
}</fold>
<fold text='/**ill-formed javadoc ...*/'>/**ill-formed javadoc
<fold text='/**ill-formed javadoc */'>/**ill-formed javadoc
*/</fold>
void bar(char c) <fold text='{}'>{
}</fold>
boolean b1 = true; <fold text='/** javadoc with code on the same line ...*/'>/** javadoc with code on the same line */</fold> boolean b2 = false; <fold text='/** second javadoc with code on the same line ...*/'>/** second javadoc with code on the same line */</fold>
boolean b1 = true; <fold text='/** javadoc with code on the same line */'>/** javadoc with code on the same line */</fold> boolean b2 = false; <fold text='/** second javadoc with code on the same line */'>/** second javadoc with code on the same line */</fold>
<fold text='/** first line ...*/'>/** first line
* second line
@@ -31,13 +31,29 @@ class Test {
void illFormedJavaDocMultilines() <fold text='{}'>{
}</fold>
<fold text='/**...*/'>/**
<fold text='/** first line */'>/** first line
*
*
*/</fold>
void javaDocWithTextOnlyOnFirstLine() <fold text='{}'>{
}</fold>
<fold text='/** second line */'>/**
* second line
*
*/</fold>
void javaDocWithTextOnlyOnSecondLine() <fold text='{}'>{
}</fold>
<fold text='/***/'>/**
*/</fold>
void emptyJavadoc() <fold text='{}'>{
}</fold>
<fold text='/**...*/'>/***/</fold>
<fold text='/***/'>/***/</fold>
void oneLineEmptyJavadoc() <fold text='{}'>{
}</fold>