diff --git a/java/java-impl/src/com/intellij/codeInsight/javadoc/JavaDocInfoGenerator.java b/java/java-impl/src/com/intellij/codeInsight/javadoc/JavaDocInfoGenerator.java index 3b3ede827b63..a0f529f57449 100644 --- a/java/java-impl/src/com/intellij/codeInsight/javadoc/JavaDocInfoGenerator.java +++ b/java/java-impl/src/com/intellij/codeInsight/javadoc/JavaDocInfoGenerator.java @@ -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); } } diff --git a/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.html b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.html new file mode 100644 index 000000000000..9b1cca3fff30 --- /dev/null +++ b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.html @@ -0,0 +1 @@ +
 A
private static final int[][][] x = new int[1][2][]
diff --git a/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.java b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.java new file mode 100644 index 000000000000..e3b66458382a --- /dev/null +++ b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithPartlySizedArray.java @@ -0,0 +1,3 @@ +class A { + private static final int[][][] x = new int[1][2][]; +} diff --git a/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithSizedArray.html b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithSizedArray.html index e4ef5f905b69..1d3d3eb9636e 100644 --- a/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithSizedArray.html +++ b/java/java-tests/testData/codeInsight/javadocIG/fieldInitializedWithSizedArray.html @@ -1 +1 @@ -
 A
public static int[] x = new int[1]
+
 A
public static int[] x = new int[1]
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 fd2935c33836..cfa014e86af9 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 @@ -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