mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
rename local variable: survive after inplace rename (e.g. when accept typo quick fix); test
This commit is contained in:
+1
-1
@@ -326,7 +326,7 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
|
||||
final PsiMethod method = (PsiMethod)currentInfo.getMethod();
|
||||
return getSignatureRange(method).contains(element.getTextRange());
|
||||
} else if (currentInfo instanceof RenameChangeInfo) {
|
||||
final PsiElement nameIdentifier = ((RenameChangeInfo)currentInfo).getNamedElement().getNameIdentifier();
|
||||
final PsiElement nameIdentifier = ((RenameChangeInfo)currentInfo).getNameIdentifier();
|
||||
return nameIdentifier != null && nameIdentifier.getTextRange().contains(element.getTextRange());
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2000-2010 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.
|
||||
*/
|
||||
public class Test {
|
||||
void foo() {
|
||||
int my<caret>i = 0;
|
||||
if (myi > 0) {
|
||||
System.out.println(myi);
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2000-2010 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.
|
||||
*/
|
||||
public class Test {
|
||||
void foo() {
|
||||
int my1i = 0;
|
||||
if (my1i > 0) {
|
||||
System.out.println(my1i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ import java.util.List;
|
||||
*/
|
||||
public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
private void doTest(final Runnable run, boolean shouldShow) {
|
||||
private void doTest(final Runnable run, boolean shouldShow, final String hint) {
|
||||
myFixture.configureByFile("/refactoring/changeSignatureGesture/" + getTestName(false) + ".java");
|
||||
final ChangeSignatureGestureDetector detector = ChangeSignatureGestureDetector.getInstance(getProject());
|
||||
final Document document = myFixture.getEditor().getDocument();
|
||||
@@ -52,7 +52,6 @@ public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase
|
||||
|
||||
|
||||
myFixture.doHighlighting();
|
||||
final String hint = ChangeSignatureDetectorAction.CHANGE_SIGNATURE;
|
||||
if (shouldShow) {
|
||||
final IntentionAction intention = myFixture.findSingleIntention(hint);
|
||||
myFixture.launchAction(intention);
|
||||
@@ -84,13 +83,21 @@ public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase
|
||||
doTypingTest(", int param");
|
||||
}
|
||||
|
||||
public void testRenameLocalVariable() {
|
||||
doTypingTest("1", ChangeSignatureDetectorAction.NEW_NAME);
|
||||
}
|
||||
|
||||
private void doTypingTest(final String param) {
|
||||
doTypingTest(param, ChangeSignatureDetectorAction.CHANGE_SIGNATURE);
|
||||
}
|
||||
|
||||
private void doTypingTest(final String param, final String hint) {
|
||||
doTest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myFixture.type(param);
|
||||
}
|
||||
}, true);
|
||||
}, true, hint);
|
||||
}
|
||||
|
||||
public void testReturnValue() {
|
||||
@@ -111,7 +118,7 @@ public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase
|
||||
public void run() {
|
||||
myFixture.type(param);
|
||||
}
|
||||
}, false);
|
||||
}, false, ChangeSignatureDetectorAction.CHANGE_SIGNATURE);
|
||||
}
|
||||
|
||||
public void testDeleteParamInSuperUsed() {
|
||||
@@ -130,7 +137,7 @@ public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase
|
||||
document.deleteString(selectionStart, selectionEnd);
|
||||
editor.getCaretModel().moveToOffset(selectionStart);
|
||||
}
|
||||
}, true);
|
||||
}, true, ChangeSignatureDetectorAction.CHANGE_SIGNATURE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -260,6 +260,7 @@ public class TemplateState implements Disposable {
|
||||
public void start(TemplateImpl template,
|
||||
@Nullable final PairProcessor<String, String> processor,
|
||||
@Nullable Map<String, String> predefinedVarValues) {
|
||||
myTemplate = template;
|
||||
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
|
||||
|
||||
myProcessor = processor;
|
||||
@@ -286,7 +287,7 @@ public class TemplateState implements Disposable {
|
||||
myCurrentVariableNumber = -1;
|
||||
mySegments = new TemplateSegments(myEditor);
|
||||
myPrevTemplate = myTemplate;
|
||||
myTemplate = template;
|
||||
|
||||
//myArgument = argument;
|
||||
myPredefinedVariableValues = predefinedVarValues;
|
||||
|
||||
|
||||
+6
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.refactoring.changeSignature;
|
||||
|
||||
import com.intellij.codeInsight.template.TemplateManager;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.components.ProjectComponent;
|
||||
@@ -52,15 +53,18 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
private final PsiManager myPsiManager;
|
||||
private final FileEditorManager myFileEditorManager;
|
||||
private final Project myProject;
|
||||
private final TemplateManager myTemplateManager;
|
||||
|
||||
public ChangeSignatureGestureDetector(final PsiDocumentManager psiDocumentManager,
|
||||
final PsiManager psiManager,
|
||||
final FileEditorManager fileEditorManager,
|
||||
final TemplateManager templateManager,
|
||||
final Project project) {
|
||||
myPsiDocumentManager = psiDocumentManager;
|
||||
myPsiManager = psiManager;
|
||||
myFileEditorManager = fileEditorManager;
|
||||
myProject = project;
|
||||
myTemplateManager = templateManager;
|
||||
}
|
||||
|
||||
public static ChangeSignatureGestureDetector getInstance(Project project){
|
||||
@@ -187,6 +191,8 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
if (file != null) {
|
||||
final MyDocumentChangeAdapter changeBean = myListenerMap.get(file);
|
||||
if (changeBean != null && changeBean.getInitialText() != null) {
|
||||
final Editor editor = myFileEditorManager.getSelectedTextEditor();
|
||||
if (editor != null && myTemplateManager.getActiveTemplate(editor) != null) return;
|
||||
final ChangeInfo info = LanguageChangeSignatureDetectors.createCurrentChangeInfo(child, changeBean.getInitialChangeInfo());
|
||||
if (info == null) {
|
||||
changeBean.reinit();
|
||||
|
||||
+29
-15
@@ -13,23 +13,26 @@
|
||||
package com.intellij.refactoring.changeSignature;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiNameIdentifierOwner;
|
||||
import com.intellij.psi.PsiNamedElement;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.rename.RenameProcessor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 10/29/10
|
||||
*/
|
||||
public abstract class RenameChangeInfo implements ChangeInfo {
|
||||
private final PsiNameIdentifierOwner myNamedElement;
|
||||
private final PsiFile myFile;
|
||||
private final int myOffset;
|
||||
private final String myOldName;
|
||||
|
||||
public RenameChangeInfo(final PsiNameIdentifierOwner namedElement, final ChangeInfo oldInfo) {
|
||||
myNamedElement = namedElement;
|
||||
myOldName = oldInfo instanceof RenameChangeInfo ? ((RenameChangeInfo)oldInfo).getOldName() : myNamedElement.getName();
|
||||
myOldName = oldInfo instanceof RenameChangeInfo ? ((RenameChangeInfo)oldInfo).getOldName() : namedElement.getName();
|
||||
myFile = namedElement.getContainingFile();
|
||||
myOffset = namedElement.getTextOffset();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -75,24 +78,35 @@ public abstract class RenameChangeInfo implements ChangeInfo {
|
||||
|
||||
@Override
|
||||
public String getNewName() {
|
||||
return myNamedElement.getName();
|
||||
final PsiNameIdentifierOwner nameOwner = getNamedElement();
|
||||
return nameOwner != null ? nameOwner.getName() : null;
|
||||
}
|
||||
|
||||
public String getOldName() {
|
||||
return myOldName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiNameIdentifierOwner getNamedElement() {
|
||||
return myNamedElement;
|
||||
return PsiTreeUtil.getParentOfType(myFile.findElementAt(myOffset), PsiNameIdentifierOwner.class);
|
||||
}
|
||||
|
||||
public void perform() {
|
||||
final String name = myNamedElement.getName();
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
myNamedElement.setName(myOldName);
|
||||
}
|
||||
});
|
||||
new RenameProcessor(myNamedElement.getProject(), myNamedElement, name, true, true).run();
|
||||
final PsiNameIdentifierOwner element = getNamedElement();
|
||||
if (element != null) {
|
||||
final String name = element.getName();
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
element.setName(myOldName);
|
||||
}
|
||||
});
|
||||
new RenameProcessor(element.getProject(), element, name, true, true).run();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiElement getNameIdentifier() {
|
||||
final PsiNameIdentifierOwner namedElement = getNamedElement();
|
||||
return namedElement != null ? namedElement.getNameIdentifier() : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user