From 562b4afd1abf28a52755517ee79c08764bc77136 Mon Sep 17 00:00:00 2001 From: Artemiy Sartakov Date: Fri, 26 Jul 2019 15:17:46 +0700 Subject: [PATCH] CommentFoldingUtil: do not add ellipsis if header includes all javadoc content (IDEA-216380) GitOrigin-RevId: 677371c852f49a64d0f2ca008280152c953494bb --- .../folding/impl/CommentFoldingUtil.java | 69 +++++++++++++++---- .../codeInsight/folding/JavadocComments.java | 24 +++++-- 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/CommentFoldingUtil.java b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/CommentFoldingUtil.java index 920b95b79838..e59660815dee 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/CommentFoldingUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/CommentFoldingUtil.java @@ -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, diff --git a/java/java-tests/testData/codeInsight/folding/JavadocComments.java b/java/java-tests/testData/codeInsight/folding/JavadocComments.java index 0b4959c13259..2b72134bd76e 100644 --- a/java/java-tests/testData/codeInsight/folding/JavadocComments.java +++ b/java/java-tests/testData/codeInsight/folding/JavadocComments.java @@ -18,12 +18,12 @@ class Test { int j = i; } - /**ill-formed javadoc + /**ill-formed javadoc */ void bar(char c) { } - boolean b1 = true; /** javadoc with code on the same line */ boolean b2 = false; /** second javadoc with code on the same line */ + boolean b1 = true; /** javadoc with code on the same line */ boolean b2 = false; /** second javadoc with code on the same line */ /** first line * second line @@ -31,13 +31,29 @@ class Test { void illFormedJavaDocMultilines() { } - /** + /** first line + * + * + */ + void javaDocWithTextOnlyOnFirstLine() { + + } + + /** + * second line + * + */ + void javaDocWithTextOnlyOnSecondLine() { + + } + + /** */ void emptyJavadoc() { } - /***/ + /***/ void oneLineEmptyJavadoc() { }