Introduce Property: Duplicate search & replace

This commit is contained in:
Alexey Sedunov
2015-03-02 18:03:41 +03:00
parent b78ed32a07
commit 6ae989e330
7 changed files with 61 additions and 2 deletions
@@ -110,4 +110,10 @@ public fun processDuplicates(
project.executeWriteCommand(MethodDuplicatesHandler.REFACTORING_NAME, replacer)
}
}
public fun processDuplicatesSilently(duplicateReplacers: Map<JetPsiRange, () -> Unit>, project: Project) {
project.executeWriteCommand(MethodDuplicatesHandler.REFACTORING_NAME) {
duplicateReplacers.values().forEach { it() }
}
}
@@ -244,10 +244,11 @@ private fun makeCall(
}
}
val calleeName = declaration.getName()
val callText = when (declaration) {
is JetNamedFunction ->
arguments.joinToString(separator = ", ", prefix = "${extractableDescriptor.name}(", postfix = ")")
else -> extractableDescriptor.name
arguments.joinToString(separator = ", ", prefix = "$calleeName(", postfix = ")")
else -> calleeName
}
val anchorInBlock = stream(anchor) { it.getParent() }.firstOrNull { it.getParent() is JetBlockExpression }
@@ -66,6 +66,8 @@ public class KotlinInplacePropertyIntroducer(
updatePanelControls()
}
private var replaceAll: Boolean = true
private fun isInitializer(): Boolean = currentTarget == ExtractionTarget.PROPERTY_WITH_INITIALIZER
override fun initPanelControls() {
@@ -121,6 +123,20 @@ public class KotlinInplacePropertyIntroducer(
addPanelControl(ControlWrapper(it, condition, initializer))
}
}
val occurrenceCount = extractionResult.duplicateReplacers.size() + 1
if (occurrenceCount > 1) {
addPanelControl(
ControlWrapper {
val replaceAllCheckBox = NonFocusableCheckBox("Replace all occurrences ($occurrenceCount)")
replaceAllCheckBox.setSelected(replaceAll)
replaceAllCheckBox.setMnemonic('R')
replaceAllCheckBox.addActionListener { replaceAll = replaceAllCheckBox.isSelected() }
replaceAllCheckBox
}
)
}
}
override fun addTypeReferenceVariable(builder: TemplateBuilderImpl) {
@@ -131,4 +147,11 @@ public class KotlinInplacePropertyIntroducer(
override fun checkLocalScope(): PsiElement? {
return myElementToRename.parents().first { it is JetClassOrObject || it is JetFile }
}
override fun moveOffsetAfter(success: Boolean) {
super.moveOffsetAfter(success)
if (success && replaceAll) {
processDuplicatesSilently(extractionResult.duplicateReplacers, myProject)
}
}
}
@@ -89,6 +89,9 @@ public class KotlinIntroducePropertyHandler(
)
introducer.performInplaceRefactoring(LinkedHashSet(descriptor.suggestedNames))
}
else {
processDuplicatesSilently(it.duplicateReplacers, project)
}
}
}
else {
@@ -0,0 +1,9 @@
// EXTRACTION_TARGET: property with initializer
class A(val n: Int) {
fun foo(k: Int) {
val a = <selection>n + 1</selection>
val b = n + 1 - 2
val c = foo(n + 1)
}
}
@@ -0,0 +1,11 @@
// EXTRACTION_TARGET: property with initializer
class A(val n: Int) {
fun foo(k: Int) {
val a = i
val b = i - 2
val c = foo(i)
}
private val i = n + 1
}
@@ -2037,5 +2037,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceProperty/extractWithParams.kt");
doIntroducePropertyTest(fileName);
}
@TestMetadata("replaceDuplicates.kt")
public void testReplaceDuplicates() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceProperty/replaceDuplicates.kt");
doIntroducePropertyTest(fileName);
}
}
}