mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SSR: keep unmatched variable initializer
This commit is contained in:
+12
-15
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.structuralsearch;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
@@ -199,6 +185,17 @@ public class JavaReplaceHandler extends StructuralReplaceHandler {
|
||||
copyMethodBodyIfNotReplaced((PsiMethod)originalNamedElement, (PsiMethod)patternNamedElement, (PsiMethod)replacementNamedElement);
|
||||
}
|
||||
|
||||
if (originalNamedElement instanceof PsiVariable &&
|
||||
patternNamedElement instanceof PsiVariable &&
|
||||
replacementNamedElement instanceof PsiVariable) {
|
||||
final PsiVariable originalVariable = (PsiVariable)originalNamedElement;
|
||||
final PsiVariable queryVariable = (PsiVariable)patternNamedElement;
|
||||
final PsiVariable replacementVariable = (PsiVariable)replacementNamedElement;
|
||||
if (originalVariable.hasInitializer() && !queryVariable.hasInitializer() && !replacementVariable.hasInitializer()) {
|
||||
replacementVariable.setInitializer(originalVariable.getInitializer());
|
||||
}
|
||||
}
|
||||
|
||||
if (originalNamedElement instanceof PsiClass &&
|
||||
patternNamedElement instanceof PsiClass &&
|
||||
replacementNamedElement instanceof PsiClass) {
|
||||
|
||||
+13
@@ -2269,4 +2269,17 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
|
||||
"}",
|
||||
replacer.testReplace(in, what, by, options, true));
|
||||
}
|
||||
|
||||
public void testReplaceVariableInitializer() {
|
||||
String in = "class X {" +
|
||||
" private final int i = 1;" +
|
||||
"}";
|
||||
String what = "int '_v;";
|
||||
String by = "long $v$;";
|
||||
assertEquals("initializer should remain",
|
||||
"class X {" +
|
||||
" private final long i=1;" +
|
||||
"}",
|
||||
replacer.testReplace(in, what, by, options, true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user