Java: fix javadoc info generator array init printing (IDEA-366529)

close #2845

GitOrigin-RevId: eb27b47956da52b8b26f1932b0a5c75b7db92373
This commit is contained in:
SirYwell
2025-01-28 17:40:22 +00:00
committed by intellij-monorepo-bot
parent e5d3d153ec
commit faabff4ed1
5 changed files with 35 additions and 16 deletions
@@ -3285,23 +3285,37 @@ public class JavaDocInfoGenerator {
public void visitNewExpression(@NotNull PsiNewExpression expression) {
appendStyledSpan(myBuffer, getHighlightingManager().getKeywordAttributes(), "new ");
PsiType type = expression.getType();
if (type != null) {
generateType(myBuffer, type, expression);
}
PsiExpression[] dimensions = expression.getArrayDimensions();
if (dimensions.length > 0) {
int closeBracketIndex = myBuffer.length() - 1;
while (closeBracketIndex > 0 && myBuffer.charAt(closeBracketIndex) != ']') closeBracketIndex--;
LOG.assertTrue(myBuffer.charAt(closeBracketIndex) == ']');
myBuffer.setLength(closeBracketIndex);
for (int i = 0; i < dimensions.length; i++) {
PsiExpression dimension = dimensions[i];
dimension.accept(this);
if (i + 1 != dimensions.length) appendStyledSpan(myBuffer, getHighlightingManager().getCommaAttributes(), ", ");
if (type instanceof PsiArrayType arrayType) {
// array dimensions can be a mix of type information and dimension expressions
// so we need to reconstruct it here by interleaving them
// 1. it starts with the deep component at the beginning
generateType(myBuffer, arrayType.getDeepComponentType(), expression);
int i = 0;
// 2. then, from outer to inner types (excluding the deep component type), we
// need to mix potential dimension expressions, type annotations, and dimensions
// without expressions
PsiExpression[] dimensions = expression.getArrayDimensions();
TextAttributes attributes = getHighlightingManager().getBracketsAttributes();
while (type instanceof PsiArrayType dimensionType) {
generateTypeAnnotations(myBuffer, dimensionType, expression, true, true);
if (dimensions.length > i) {
appendStyledSpan(myBuffer, attributes, "[");
dimensions[i].accept(this);
appendStyledSpan(myBuffer, attributes, "]");
}
else {
appendStyledSpan(myBuffer, attributes, "[]");
}
type = dimensionType.getComponentType();
i++;
}
PsiArrayInitializerExpression initializer = expression.getArrayInitializer();
if (initializer != null) {
initializer.accept(this);
}
appendStyledSpan(myBuffer, getHighlightingManager().getBracketsAttributes(), "]");
}
else {
else if (type != null) {
generateType(myBuffer, type, expression);
expression.acceptChildren(this);
}
}
@@ -0,0 +1 @@
<html><head><base href="placeholder"></head><body><div class="bottom"><icon src="AllIcons.Nodes.Class">&nbsp;<a href="psi_element://A"><code><span style="color:#000000;">A</span></code></a></div><div class='definition'><pre><span style="color:#000080;font-weight:bold;">private static final</span> <span style="color:#000080;font-weight:bold;">int</span><span style="">[]</span><span style="">[]</span><span style="">[]</span> <span style="color:#660e7a;">x</span> <span style="">= </span><span style="color:#000080;font-weight:bold;">new </span><span style="color:#000080;font-weight:bold;">int</span><span style="">[</span><span style="color:#0000ff;">1</span><span style="">]</span><span style="">[</span><span style="color:#0000ff;">2</span><span style="">]</span><span style="">[]</span></pre></div><table class='sections'></table>
@@ -0,0 +1,3 @@
class A {
private static final int[][][] x = new int[1][2][];
}
@@ -1 +1 @@
<html><head><base href="placeholder"></head><body><div class="bottom"><icon src="AllIcons.Nodes.Class">&nbsp;<a href="psi_element://A"><code><span style="color:#000000;">A</span></code></a></div><div class='definition'><pre><span style="color:#000080;font-weight:bold;">public static</span> <span style="color:#000080;font-weight:bold;">int</span><span style="">[]</span> <span style="color:#660e7a;font-style:italic;">x</span> <span style="">= </span><span style="color:#000080;font-weight:bold;">new </span><span style="color:#000080;font-weight:bold;">int</span><span style="">[<span style="color:#0000ff;">1</span><span style="">]</span></pre></div><table class='sections'></table>
<html><head><base href="placeholder"></head><body><div class="bottom"><icon src="AllIcons.Nodes.Class">&nbsp;<a href="psi_element://A"><code><span style="color:#000000;">A</span></code></a></div><div class='definition'><pre><span style="color:#000080;font-weight:bold;">public static</span> <span style="color:#000080;font-weight:bold;">int</span><span style="">[]</span> <span style="color:#660e7a;font-style:italic;">x</span> <span style="">= </span><span style="color:#000080;font-weight:bold;">new </span><span style="color:#000080;font-weight:bold;">int</span><span style="">[</span><span style="color:#0000ff;">1</span><span style="">]</span></pre></div><table class='sections'></table>
@@ -112,6 +112,7 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
public void testFieldInitializedWithLambda() { doTestField(); }
public void testFieldInitializedWithArray() { doTestField(); }
public void testFieldInitializedWithSizedArray() { doTestField(); }
public void testFieldInitializedWithPartlySizedArray() { doTestField(); }
public void testDoubleLt() { doTestClass(); }
public void testNoSpaceAfterTagName() { doTestClass(); }
public void testRecordParameters() { doTestClass(); } //j.l.Record is unresolved as there is no mock jdk 14 yet