SSR: don't loose comma when replacing array elements (IDEA-319000)

GitOrigin-RevId: 47e3fd4fdadaab66075c37732891af09e3167974
This commit is contained in:
Bas Leijdekkers
2023-04-26 11:58:57 +02:00
committed by intellij-monorepo-bot
parent d2bdcf661d
commit d54705b1f1
3 changed files with 41 additions and 2 deletions

View File

@@ -453,7 +453,11 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
return;
}
if (listContext) {
if (replacements.length > 1) {
if (replacements.length == 1 && replacements[0] instanceof PsiExpressionListStatement statement) {
PsiElement[] children = statement.getExpressionList().getChildren();
elementParent.addRangeBefore(children[0], children[children.length - 1], elementToReplace);
}
else if (replacements.length > 1) {
final PsiElement replacement = elementParent.addRangeBefore(replacements[0], replacements[replacements.length - 1], elementToReplace);
copyUnmatchedElements(elementToReplace, replacement, info);
}

View File

@@ -736,7 +736,10 @@ public final class JavaStructuralSearchProfile extends StructuralSearchProfile {
addSeparatorTextMatchedInAnyOrder(currentElement,
parent instanceof PsiClass ? PsiMember.class : PsiJavaCodeReferenceElement.class, buf);
}
else if (info.isStatementContext() || info.isArgumentContext() || parent instanceof PsiPolyadicExpression) {
else if (info.isStatementContext() ||
info.isArgumentContext() ||
parent instanceof PsiPolyadicExpression ||
parent instanceof PsiArrayInitializerExpression) {
addSeparatorText(previous, currentElement, buf);
}
else {

View File

@@ -3082,6 +3082,38 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
" public final X[] EMPTY_ARRAY = {};" +
"}",
replace(in2, "'_FieldType 'Field = '_Init?;", "$FieldType$ $Field$ = $Init$;", true));
String in3 = """
class X {
void x(int... ss) {}
void y() {
x(new int[] {1, 2});\s
}
}
""";
assertEquals("Should keep commas",
"""
class X {
void x(int... ss) {}
void y() {
x(1, 2);\s
}
}
""",
replace(in3, "new int[] {'_a*}", "$a$", true));
assertEquals("Should keep commas 2",
"""
class X {
void x(int... ss) {}
void y() {
x(new long[] {1, 2});\s
}
}
""",
replace(in3, "new int[] {'_a*}", "new long[] {$a$}"));
}
public void testMethodCall() {