IDEA-283316 [extract method] fix: inplace template should only rename inserted call

GitOrigin-RevId: f6b2f583deb1726da8ac5672d94f55496c586ba0
This commit is contained in:
Alexandr Suhinin
2022-02-07 16:34:19 +00:00
committed by intellij-monorepo-bot
parent ee8d5d63e7
commit a8f79fd185
4 changed files with 43 additions and 1 deletions
@@ -15,6 +15,7 @@ import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.search.SearchScope
import com.intellij.refactoring.extractMethod.ExtractMethodDialog
import com.intellij.refactoring.extractMethod.ExtractMethodHandler
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper
@@ -36,6 +37,7 @@ import com.intellij.refactoring.rename.inplace.InplaceRefactoring
import com.intellij.refactoring.suggested.SuggestedRefactoringProvider
import com.intellij.refactoring.suggested.range
import com.intellij.refactoring.util.CommonRefactoringUtil
import com.intellij.util.SmartList
import org.jetbrains.annotations.Nls
class InplaceMethodExtractor(private val editor: Editor,
@@ -160,6 +162,11 @@ class InplaceMethodExtractor(private val editor: Editor,
return targetClass
}
override fun collectRefs(referencesSearchScope: SearchScope?): MutableCollection<PsiReference> {
val reference = call?.reference ?: return SmartList()
return SmartList(reference)
}
override fun revertState() {
super.revertState()
WriteCommandAction.runWriteCommandAction(myProject) {
@@ -174,7 +181,11 @@ class InplaceMethodExtractor(private val editor: Editor,
override fun afterTemplateStart() {
setActiveExtractor(editor, this)
val templateState = TemplateManagerImpl.getTemplateState(myEditor) ?: return
val templateState = TemplateManagerImpl.getTemplateState(myEditor)
if (templateState == null) {
Disposer.dispose(disposable)
throw IllegalStateException("Failed to start code template.")
}
Disposer.register(templateState) { SuggestedRefactoringProvider.getInstance(myProject).reset() }
Disposer.register(templateState, disposable)
super.afterTemplateStart()
@@ -0,0 +1,10 @@
import static java.lang.Integer.getInteger;
public abstract class Test {
private void test() {
String integer = <selection>"4" + "2"</selection>;
int x = getInteger("0");
System.out.println(integer);
}
}
@@ -0,0 +1,17 @@
import org.jetbrains.annotations.NotNull;
import static java.lang.Integer.getInteger;
public abstract class Test {
private void test() {
String integer = renamed();
int x = getInteger("0");
System.out.println(integer);
}
@NotNull
private String renamed() {
return "4" + "2";
}
}
@@ -227,6 +227,10 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
}
fun testTemplateRenamesInsertedCallOnly(){
doTest(changedName = "renamed")
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")