SSR: fix replacing array initializer removes semicolon (IDEA-192548)

This commit is contained in:
Bas Leijdekkers
2018-06-04 12:54:53 +02:00
parent 5cebf2f239
commit 84f3069914
2 changed files with 18 additions and 9 deletions
@@ -803,17 +803,17 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
private static int removeExtraSemicolon(ParameterInfo info, int offset, StringBuilder result, MatchResult match) {
if (info.isStatementContext()) {
final int index = offset + info.getStartIndex();
if (result.charAt(index)==';' &&
( match == null ||
final PsiElement matchElement = (match == null) ? null : match.getMatch();
if (result.charAt(index) == ';' &&
( matchElement == null ||
( result.charAt(index-1)=='}' &&
!(match.getMatch() instanceof PsiDeclarationStatement) && // array init in dcl
!(match.getMatch() instanceof PsiNewExpression) // array initializer
!(matchElement instanceof PsiDeclarationStatement) && // array init in dcl
!(matchElement instanceof PsiNewExpression) && // array initializer
!(matchElement instanceof PsiArrayInitializerExpression)
) ||
( !match.isMultipleMatch() && // ; in comment
match.getMatch() instanceof PsiComment
) ||
( match.isMultipleMatch() && // ; in comment
match.getChildren().get( match.getChildren().size() - 1 ).getMatch() instanceof PsiComment
( match.isMultipleMatch() // ; in comment
? match.getChildren().get(match.getChildren().size() - 1).getMatch() instanceof PsiComment
: matchElement instanceof PsiComment
)
)
) {
@@ -2460,5 +2460,14 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
"}",
replace(in, "'_ReturnType '_Method('_ParameterType '_Parameter*);",
"$ReturnType$ $Method$ ($ParameterType$ $Parameter$);", true));
String in2 = "class X {" +
" public final X[] EMPTY_ARRAY = {};" +
"}";
assertEquals("shouldn't delete semicolon",
"class X {" +
" public final X[] EMPTY_ARRAY = {};" +
"}",
replace(in2, "'_FieldType 'Field = '_Init?;", "$FieldType$ $Field$ = $Init$;", true));
}
}