mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reassign on introduce variable: ensure renamed; tests
This commit is contained in:
+11
-12
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
package com.intellij.refactoring.introduceVariable;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.ui.ListCellRendererWrapper;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
@@ -37,6 +39,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
@@ -49,7 +52,8 @@ public class ReassignVariableUtil {
|
||||
private ReassignVariableUtil() {
|
||||
}
|
||||
|
||||
static boolean reassign(final Editor editor) {
|
||||
@VisibleForTesting
|
||||
public static boolean reassign(final Editor editor) {
|
||||
final SmartPsiElementPointer<PsiDeclarationStatement> pointer = editor.getUserData(DECLARATION_KEY);
|
||||
final PsiDeclarationStatement declaration = pointer != null ? pointer.getElement() : null;
|
||||
final PsiType type = getVariableType(declaration);
|
||||
@@ -138,18 +142,13 @@ public class ReassignVariableUtil {
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(variable.getProject());
|
||||
final String chosenVariableName = variable.getName();
|
||||
//would generate red code for final variables
|
||||
PsiElement newDeclaration = elementFactory.createStatementFromText(chosenVariableName + " = " + initializer.getText() + ";",
|
||||
declaration);
|
||||
PsiElement newDeclaration = elementFactory.createStatementFromText(chosenVariableName + " = " + initializer.getText() + ";", declaration);
|
||||
final Collection<PsiReference> references = ReferencesSearch.search(var).findAll();
|
||||
newDeclaration = declaration.replace(newDeclaration);
|
||||
final PsiFile containingFile = newDeclaration.getContainingFile();
|
||||
final RangeMarker[] occurrenceMarkers = editor.getUserData(OCCURRENCES_KEY);
|
||||
if (occurrenceMarkers != null) {
|
||||
for (RangeMarker marker : occurrenceMarkers) {
|
||||
final PsiElement refVariableElement = containingFile.findElementAt(marker.getStartOffset());
|
||||
final PsiExpression expression = PsiTreeUtil.getParentOfType(refVariableElement, PsiReferenceExpression.class);
|
||||
if (expression != null) {
|
||||
expression.replace(elementFactory.createExpressionFromText(chosenVariableName, newDeclaration));
|
||||
}
|
||||
for (PsiReference reference : references) {
|
||||
final PsiElement element = reference.getElement();
|
||||
if (element instanceof PsiExpression) {
|
||||
element.replace(elementFactory.createExpressionFromText(chosenVariableName, newDeclaration));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class C {
|
||||
{
|
||||
String s;
|
||||
System.out.println("<caret>");
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class C {
|
||||
{
|
||||
String s;
|
||||
s = "";
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class C {
|
||||
{
|
||||
String strA = "bar";
|
||||
if (true
|
||||
) System.out.println("<caret>");
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class C {
|
||||
{
|
||||
String strA = "bar";
|
||||
if (true
|
||||
) {
|
||||
strA = "";
|
||||
System.out.println(strA);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2000-2015 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.
|
||||
*/
|
||||
package com.intellij.refactoring;
|
||||
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||
import com.intellij.refactoring.introduceVariable.ReassignVariableUtil;
|
||||
|
||||
public class InplaceReassignVariableTest extends AbstractJavaInplaceIntroduceTest {
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
doRunTest();
|
||||
}
|
||||
|
||||
public void testReassignSimple() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testReassignWhenVariableWasPutInLoopBody() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
String name = getTestName(true);
|
||||
configureByFile(getBasePath() + name + getExtension());
|
||||
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
|
||||
try {
|
||||
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
|
||||
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
|
||||
|
||||
invokeRefactoring();
|
||||
ReassignVariableUtil.reassign(getEditor());
|
||||
|
||||
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
|
||||
assert state != null;
|
||||
state.gotoEnd(false);
|
||||
checkResultByFile(getBasePath() + name + "_after" + getExtension());
|
||||
}
|
||||
finally {
|
||||
getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/refactoring/inplaceIntroduceVariable/";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MyIntroduceHandler createIntroduceHandler() {
|
||||
return new InplaceIntroduceVariableTest.MyIntroduceVariableHandler();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user