SSR: fix bad psi tree when replacing parameter

This commit is contained in:
Bas Leijdekkers
2016-03-05 11:46:15 +01:00
parent c7595ad7b4
commit 613871ca43
2 changed files with 21 additions and 0 deletions
@@ -284,6 +284,16 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
}
else if (statements.length == 1) {
PsiElement replacement = getMatchExpr(statements[0], elementToReplace);
if (elementToReplace instanceof PsiParameter && replacement instanceof PsiLocalVariable) {
final PsiVariable variable = (PsiVariable)replacement;
final PsiIdentifier identifier = variable.getNameIdentifier();
assert identifier != null;
final String text = variable.getText();
// chop off unneeded semicolons & initializers
final String parameterText = text.substring(0, identifier.getStartOffsetInParent() + identifier.getTextLength());
replacement = JavaPsiFacade.getElementFactory(variable.getProject()).createParameterFromText(parameterText, variable);
}
copyUnmatchedElements(elementToReplace, replacement);
replacement = handleSymbolReplacement(replacement, elementToReplace);
@@ -2457,4 +2457,15 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
" abstract void a(int i);\n" +
"}", replacer.testReplace(in, what, by, options));
}
public void testReplaceParameterWithComment() {
final String in = "class A {\n" +
" void a(int b) {}\n" +
"}";
final String what = "int '_a = '_b{0,1};";
final String by = "final long /*!*/ $a$ = $b$;";
assertEquals("class A {\n" +
" void a(final long /*!*/ b) {}\n" +
"}", replacer.testReplace(in, what, by, options));
}
}