fix(javadoc-gen): @code tag rendering full line

Turns out there was another edge case that the ones from the Youtrack issues.

GitOrigin-RevId: 6bccd3d0aa4fc121059558b847ba90731d9c8962
This commit is contained in:
Mathias Boulay
2024-09-17 16:28:17 +00:00
committed by intellij-monorepo-bot
parent e05667345c
commit 68242cd5b9
4 changed files with 32 additions and 5 deletions
@@ -2242,7 +2242,7 @@ public class JavaDocInfoGenerator {
private static boolean isCodeBlock(PsiInlineDocTag tag) {
if (!CODE_TAG.equals(tag.getName())) return false;
return isInPre(tag);
return isInPre(tag, true);
}
private void generateCodeValue(PsiInlineDocTag tag, StringBuilder buffer) {
@@ -2278,7 +2278,7 @@ public class JavaDocInfoGenerator {
}
appendPlainText(tmpBuffer, doEscaping ? StringUtil.escapeXmlEntities(elementText) : elementText);
}
if ((mySdkVersion == null || mySdkVersion.isAtLeast(JavaSdkVersion.JDK_1_8)) && isInPre(tag)) {
if ((mySdkVersion == null || mySdkVersion.isAtLeast(JavaSdkVersion.JDK_1_8)) && isInPre(tag, false)) {
buffer.append(tmpBuffer);
}
else {
@@ -2286,21 +2286,26 @@ public class JavaDocInfoGenerator {
}
}
private static boolean isInPre(PsiElement element) {
/// @param strict If `true`, the method expects the `<pre>` tag to be the only text right before the `element`
private static boolean isInPre(@NotNull PsiElement element, boolean strict) {
PsiElement sibling = element.getPrevSibling();
while (sibling != null) {
if (sibling instanceof PsiDocToken) {
if (sibling instanceof PsiDocToken && sibling.getNode().getElementType() != JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) {
String text = StringUtil.toLowerCase(sibling.getText());
int pos = text.lastIndexOf("pre>");
if (pos > 0) {
switch (text.charAt(pos - 1)) {
case '<' -> {
return true;
if(!strict || text.endsWith("pre>")){
return true;
}
}
case '/' -> {
return false;
}
}
} else if(strict && !text.trim().isEmpty()) {
return false;
}
}
sibling = sibling.getPrevSibling();
@@ -0,0 +1,6 @@
<html><head><base href="placeholder"></head><body><div class='definition'><pre><span style="color:#000080;font-weight:bold;">class</span> <span style="color:#000000;">Test</span></pre></div><div class='content'>
<pre>
Some text with inline <code><span style="">code</span></code>
</pre>
<pre><code><span style="">String&#32;fullLine&#32;=&#32;</span><span style="color:#008000;font-weight:bold;">""</span><span style="">;</span></code></pre> </pre>
</div><table class='sections'><p></table>
@@ -0,0 +1,13 @@
/**
* <pre>
* Some text with inline {@code code}
* </pre>
* <pre>
* {@code
* String fullLine = "";
* }
* </pre>
*/
class Test {
public String field = null;
}
@@ -211,6 +211,9 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
public void testPreTagLeakBeforeCode() {
doTestClass();
}
public void testPreTagStrictBeforeCode(){
doTestClass();
}
public void testRepeatableAnnotations() {