introduce parameter: ensure correct scope is picked in case of anonymous class (IDEA-143435)

This commit is contained in:
Anna Kozlova
2015-08-14 19:37:39 +02:00
parent 75eb6dd892
commit 7dc4a1e1b3
4 changed files with 57 additions and 2 deletions
@@ -128,8 +128,7 @@ public class InplaceIntroduceParameterPopup extends AbstractJavaInplaceIntroduce
@Override
protected PsiElement checkLocalScope() {
final PsiVariable variable = getLocalVariable();
return variable == null ? myMethod : PsiTreeUtil.getParentOfType(variable, PsiMethod.class);
return myMethod;
}
@Override
@@ -0,0 +1,13 @@
class Abc {
void foo() {
new Runnable(){
@Override
public void run() {
final String na<caret>me = "name";
System.out.println(name);
}
};
}
}
@@ -0,0 +1,12 @@
class Abc {
void foo(final String name) {
new Runnable(){
@Override
public void run() {
System.out.println(name);
}
};
}
}
@@ -15,6 +15,8 @@
*/
package com.intellij.refactoring;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
@@ -22,6 +24,7 @@ import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import com.intellij.refactoring.introduceParameter.IntroduceParameterHandler;
import com.intellij.testFramework.LightPlatformTestCase;
import org.jetbrains.annotations.NotNull;
/**
@@ -122,6 +125,34 @@ public class InplaceIntroduceParameterTest extends AbstractJavaInplaceIntroduceT
});
}
public void testLocalInsideAnonymous1() throws Exception {
final Pass<AbstractInplaceIntroducer> pass = new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroducePopup) {
}
};
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
try {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
//ensure extract local var
final MyIntroduceHandler introduceHandler = createIntroduceHandler();
introduceHandler.invokeImpl(LightPlatformTestCase.getProject(), getLocalVariableFromEditor(), getEditor());
final AbstractInplaceIntroducer introducer = introduceHandler.getInplaceIntroducer();
pass.pass(introducer);
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 BASE_PATH;