From 5f0e92caa1e939e753dc3caea5ca974808ed3425 Mon Sep 17 00:00:00 2001
From: Mathias
Date: Mon, 17 Nov 2025 14:25:30 +0100
Subject: [PATCH] IDEA-375516 javadoc: better handling of last line paragraph
breaks
#IDEA-375516 Fixed
GitOrigin-RevId: fcc5475be92335ed7cb4a0f7dc416eaffa27462d
---
.../JavaDocMarkdownFlavourDescriptor.java | 33 +++++++++++++++----
.../markdownInheritDocSingleLine.html | 1 +
.../markdownInheritDocSingleLine.java | 19 +++++++++++
.../javadocIG/markdownInlineCodeBlock.html | 2 +-
.../markdownParagraphSeparation.html | 1 +
.../markdownParagraphSeparation.java | 4 +++
.../javadocIG/markdownReferenceLink.html | 2 +-
.../javadoc/JavaDocInfoGeneratorTest.java | 6 ++++
8 files changed, 60 insertions(+), 8 deletions(-)
create mode 100644 java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.html
create mode 100644 java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.java
create mode 100644 java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.html
create mode 100644 java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.java
diff --git a/java/java-impl/src/com/intellij/codeInsight/javadoc/markdown/JavaDocMarkdownFlavourDescriptor.java b/java/java-impl/src/com/intellij/codeInsight/javadoc/markdown/JavaDocMarkdownFlavourDescriptor.java
index a33b36b49fe6..967601326b7c 100644
--- a/java/java-impl/src/com/intellij/codeInsight/javadoc/markdown/JavaDocMarkdownFlavourDescriptor.java
+++ b/java/java-impl/src/com/intellij/codeInsight/javadoc/markdown/JavaDocMarkdownFlavourDescriptor.java
@@ -89,15 +89,36 @@ public class JavaDocMarkdownFlavourDescriptor extends GFMFlavourDescriptor {
ASTNode parent = node.getParent();
if (parent != null && parent.getType() == MarkdownElementTypes.MARKDOWN_FILE) {
- // Check if the next thing is an EOL
- List siblings = parent.getChildren();
- ASTNode nextSibling = siblings.get(Math.min(siblings.size() - 1, siblings.indexOf(node) + 1));
- if (nextSibling != null && nextSibling.getType() == MarkdownTokenTypes.EOL) {
- return false;
- }
+ if (hasEolSiblings(node, 1, true)) return false;
+ if (hasEolSiblings(node, 2, false)) return false;
}
return true;
}
+
+ /// Returns whether we have eol siblings, skipping over whitespaces.
+ /// @param count The desired number of EOL siblings
+ /// @param forward Whether to search forward or backward
+ private static boolean hasEolSiblings(@NotNull ASTNode node, int count, boolean forward) throws IllegalArgumentException {
+ if (count <= 0) throw new IllegalArgumentException("count must be positive, was " + count);
+ ASTNode parent = node.getParent();
+ if (parent == null) return false;
+
+ List siblings = node.getParent().getChildren();
+ int nodeChildIndex = siblings.indexOf(node);
+ int direction = forward ? 1 : -1;
+
+ for (int i = nodeChildIndex + direction; i < siblings.size() && i >= 0; i += direction) {
+ if (siblings.get(i).getType() == MarkdownTokenTypes.EOL) {
+ count--;
+ if (count == 0) return true;
+ continue;
+ }
+ if (siblings.get(i).getType() == MarkdownTokenTypes.WHITE_SPACE) continue;
+
+ return false;
+ }
+ return false;
+ }
@Override
public void closeTag(@NotNull HtmlGenerator.HtmlGeneratingVisitor visitor, @NotNull String text, @NotNull ASTNode node) {
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.html b/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.html
new file mode 100644
index 000000000000..be6eabbd0382
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.html
@@ -0,0 +1 @@
+Markdown variant
I am legacy javadoc, I hope no one disturbs my _underlines_ and **astrerisks**
Single line markdown
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.java b/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.java
new file mode 100644
index 000000000000..93c39748c71d
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownInheritDocSingleLine.java
@@ -0,0 +1,19 @@
+import java.lang.Override
+
+class MarkdownInheritDoc {
+
+ /** I am legacy javadoc, I hope no one disturbs my _underlines_ and **astrerisks** */
+ void foo() {}
+}
+
+class MarkdownInheritedDoc extends MarkdownInheritDoc {
+
+ /// Markdown variant
+ /// {@inheritDoc}
+ ///
+ /// Single line markdown
+ @Override
+ void foo() {
+ super.foo();
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownInlineCodeBlock.html b/java/java-tests/testData/codeInsight/javadocIG/markdownInlineCodeBlock.html
index fb7253be6bec..535b954b71b6 100644
--- a/java/java-tests/testData/codeInsight/javadocIG/markdownInlineCodeBlock.html
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownInlineCodeBlock.html
@@ -3,4 +3,4 @@
{@link java.lang.String niceLink}
No markdown markup is interpreted inside them
_Hello_ <code>
Code span inside a link
my text with a code span!
This is a broken inline code span
- `Start of broken code span
end of broken code span`
\ No newline at end of file
+ `Start of broken code spanend of broken code span`
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.html b/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.html
new file mode 100644
index 000000000000..f09e65b8c5ce
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.html
@@ -0,0 +1 @@
+Paragraph one
Paragraph two (eof)
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.java b/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.java
new file mode 100644
index 000000000000..326112a61597
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownParagraphSeparation.java
@@ -0,0 +1,4 @@
+/// Paragraph one
+///
+/// Paragraph two (eof)
+class MarkdownJepExample {}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/javadocIG/markdownReferenceLink.html b/java/java-tests/testData/codeInsight/javadocIG/markdownReferenceLink.html
index cd682aa27bdb..ffec93af1346 100644
--- a/java/java-tests/testData/codeInsight/javadocIG/markdownReferenceLink.html
+++ b/java/java-tests/testData/codeInsight/javadocIG/markdownReferenceLink.html
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/javadoc/JavaDocInfoGeneratorTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/javadoc/JavaDocInfoGeneratorTest.java
index d8c7bae7c842..04dba31c97f9 100644
--- a/java/java-tests/testSrc/com/intellij/java/codeInsight/javadoc/JavaDocInfoGeneratorTest.java
+++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/javadoc/JavaDocInfoGeneratorTest.java
@@ -199,11 +199,17 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
PsiClass outerClass = ((PsiJavaFile) myFile).getClasses()[1];
verifyJavaDoc(outerClass.getMethods()[0]);
}
+ public void testMarkdownInheritDocSingleLine() {
+ configureByFile();
+ PsiClass outerClass = ((PsiJavaFile) myFile).getClasses()[1];
+ verifyJavaDoc(outerClass.getMethods()[0]);
+ }
public void testMarkdownInlineWithTags(){
configureByFile();
PsiClass outerClass = ((PsiJavaFile) myFile).getClasses()[0];
verifyJavaDoc(outerClass.getMethods()[0]);
}
+ public void testMarkdownParagraphSeparation() { doTestClass(); }
public void testMarkdownJepExample(){ doTestMethod(); }
public void testHtmlCodeInMarkdown() { doTestMethod(); }
public void testMarkdownInlineCodeBlock() { doTestClass(); }