Allow resource variables in-place rename; Java in-place rename tests updated

This commit is contained in:
Roman Shevchenko
2011-03-01 11:42:09 +01:00
parent beab19875d
commit 8f62093a5c
11 changed files with 70 additions and 43 deletions
@@ -111,8 +111,9 @@ public class JavaRefactoringSupportProvider extends RefactoringSupportProvider {
SearchScope useScope = elementToRename.getManager().getSearchHelper().getUseScope(elementToRename);
if (!(useScope instanceof LocalSearchScope)) return false;
PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
if (scopeElements.length > 1 && // assume there are no elements with use scopes with holes in'em
!isElementWithComment(scopeElements)) return false; // except a case of element and it's doc comment
if (scopeElements.length > 1 && // assume there are no elements with use scopes with holes in them
!isElementWithComment(scopeElements) && // ... except a case of element and it's doc comment
!isResourceVariable(scopeElements)) return false; // ... and badly scoped resource variables
PsiFile containingFile = elementToRename.getContainingFile();
return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
@@ -129,4 +130,10 @@ public class JavaRefactoringSupportProvider extends RefactoringSupportProvider {
return comment != null && comment.getOwner() == owner;
}
private static boolean isResourceVariable(final PsiElement[] scopeElements) {
return scopeElements.length == 2 &&
scopeElements[0] instanceof PsiResourceList &&
scopeElements[1] instanceof PsiCodeBlock;
}
}
@@ -1,7 +1,7 @@
public class Test {
int myI;
void foo(int i){
myI = i;
void foo(int pp){
myI = pp;
}
}
@@ -1,7 +1,7 @@
public class Test {
int myI;
void foo(int i){
myI = i;
void foo(int pp){
myI = pp;
}
}
@@ -1,6 +1,6 @@
class Test {
int myI;
void foo(int i){
myI = i;
void foo(int myI){
this.myI = myI;
}
}
@@ -0,0 +1,7 @@
public class Test {
void m() throws Exception {
try (AutoCloseable <caret>r = null; AutoCloseable r2 = r) {
System.out.println(r + ", " + r2);
}
}
}
@@ -0,0 +1,7 @@
public class Test {
void m() throws Exception {
try (AutoCloseable <caret>r = null; AutoCloseable r2 = r) {
System.out.println(r + ", " + r2);
}
}
}
@@ -0,0 +1,7 @@
public class Test {
void m() throws Exception {
try (AutoCloseable r1 = null; AutoCloseable r2 = r1) {
System.out.println(r1 + ", " + r2);
}
}
}
@@ -0,0 +1,7 @@
public class Test {
void m() throws Exception {
try (AutoCloseable r1 = null; AutoCloseable r2 = r1) {
System.out.println(r1 + ", " + r2);
}
}
}
@@ -2,18 +2,12 @@ package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupManagerImpl;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.lang.java.JavaRefactoringSupportProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.inplace.ResolveSnapshotProvider;
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer;
import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
/**
* @author ven
@@ -71,34 +65,24 @@ public class RenameLocalTest extends LightCodeInsightTestCase {
doTestInplaceRename("pp");
}
//reference itself won't be renamed
private void doTestInplaceRename(String newName) throws Exception {
public void testRenameResource() throws Exception {
doTest("r1");
}
public void testRenameResourceInPlace() throws Exception {
doTestInplaceRename("r1");
}
private void doTestInplaceRename(final String newName) throws Exception {
configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
final PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
assertNotNull(element);
final PsiMethod methodScope = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
assertNotNull(methodScope);
assertTrue("In-place rename not allowed for " + element,
JavaRefactoringSupportProvider.mayRenameInplace(element, null));
ResolveSnapshotProvider resolveSnapshotProvider = VariableInplaceRenamer.INSTANCE.forLanguage(getFile().getLanguage());
assertNotNull(resolveSnapshotProvider);
final ResolveSnapshotProvider.ResolveSnapshot snapshot = resolveSnapshotProvider.createSnapshot(methodScope.getBody());
assertNotNull(snapshot);
CodeInsightTestUtil.doInlineRename(new VariableInplaceRenameHandler(), newName, getEditor(), element);
final int offset = element.getTextOffset();
VariableInplaceRenamer renamer = new VariableInplaceRenamer((PsiNameIdentifierOwner)element, getEditor());
((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true);
try {
renamer.performInplaceRename();
}
finally {
renamer.finish();
snapshot.apply(newName);
TemplateManagerImpl.getTemplateState(myEditor).gotoEnd();
renamer.performAutomaticRename(newName, PsiTreeUtil.getParentOfType(myFile.findElementAt(offset), PsiNameIdentifierOwner.class));
((LookupManagerImpl)LookupManager.getInstance(getProject())).clearLookup();
((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false);
}
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
}
@@ -39,6 +39,7 @@ import com.intellij.util.PairProcessor;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.util.*;
@@ -81,6 +82,7 @@ public class TemplateManagerImpl extends TemplateManager implements ProjectCompo
EditorFactory.getInstance().addEditorFactoryListener(myEditorFactoryListener, myProject);
}
@TestOnly
public void setTemplateTesting(final boolean templateTesting) {
myTemplateTesting = templateTesting;
}
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.testFramework.fixtures;
import com.intellij.codeInsight.editorActions.SelectWordHandler;
@@ -46,6 +45,7 @@ import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.util.List;
@@ -152,10 +152,12 @@ public class CodeInsightTestUtil {
fixture.checkResultByFile(after, false);
}
@TestOnly
public static void doInlineRename(VariableInplaceRenameHandler handler, final String newName, CodeInsightTestFixture fixture) {
doInlineRename(handler, newName, fixture.getEditor(), fixture.getElementAtCaret());
}
@TestOnly
public static void doInlineRename(VariableInplaceRenameHandler handler, final String newName, Editor editor, PsiElement elementAtCaret) {
Project project = editor.getProject();
TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(project);
@@ -166,6 +168,7 @@ public class CodeInsightTestUtil {
editor = ((EditorWindow)editor).getDelegate();
}
TemplateState state = TemplateManagerImpl.getTemplateState(editor);
assert state != null;
final TextRange range = state.getCurrentVariableRange();
assert range != null;
final Editor finalEditor = editor;
@@ -178,7 +181,9 @@ public class CodeInsightTestUtil {
assert renamer != null;
renamer.finish();
TemplateManagerImpl.getTemplateState(editor).gotoEnd();
state = TemplateManagerImpl.getTemplateState(editor);
assert state != null;
state.gotoEnd();
renamer.performAutomaticRename(newName, elementAtCaret);
}
finally {
@@ -186,6 +191,7 @@ public class CodeInsightTestUtil {
}
}
@TestOnly
public static void doInlineRenameTest(VariableInplaceRenameHandler handler, String file, String extension,
String newName, CodeInsightTestFixture fixture) {
fixture.configureByFile(file + "." + extension);