Introduce Property: Fix rename of primary occurrence
#KT-9404 Fixed
This commit is contained in:
+2
-1
@@ -407,7 +407,8 @@ data class ExtractionGeneratorOptions(
|
||||
val target: ExtractionTarget = ExtractionTarget.FUNCTION,
|
||||
val flexibleTypesAllowed: Boolean = false,
|
||||
val allowDummyName: Boolean = false,
|
||||
val allowExpressionBody: Boolean = true
|
||||
val allowExpressionBody: Boolean = true,
|
||||
val delayInitialOccurrenceReplacement: Boolean = false
|
||||
) {
|
||||
companion object {
|
||||
val DEFAULT = ExtractionGeneratorOptions()
|
||||
|
||||
+13
-2
@@ -644,14 +644,25 @@ fun ExtractionGeneratorConfiguration.generateDeclaration(
|
||||
|
||||
if (generatorOptions.inTempFile) return ExtractionResult(this, declaration, Collections.emptyMap(), nameByOffset)
|
||||
|
||||
makeCall(descriptor, declaration, descriptor.controlFlow, descriptor.extractionData.originalRange, descriptor.parameters.map { it.argumentText })
|
||||
val replaceInitialOccurrence = {
|
||||
val arguments = descriptor.parameters.map { it.argumentText }
|
||||
makeCall(descriptor, declaration, descriptor.controlFlow, descriptor.extractionData.originalRange, arguments)
|
||||
}
|
||||
|
||||
if (!generatorOptions.delayInitialOccurrenceReplacement) replaceInitialOccurrence()
|
||||
|
||||
if (shouldInsert) {
|
||||
ShortenReferences.DEFAULT.process(declaration)
|
||||
}
|
||||
|
||||
if (generatorOptions.inTempFile) return ExtractionResult(this, declaration, emptyMap(), nameByOffset)
|
||||
|
||||
val duplicateReplacers = duplicates.map { it.range to { makeCall(descriptor, declaration, it.controlFlow, it.range, it.arguments) } }.toMap()
|
||||
val duplicateReplacers = HashMap<KotlinPsiRange, () -> Unit>().apply {
|
||||
if (generatorOptions.delayInitialOccurrenceReplacement) {
|
||||
put(descriptor.extractionData.originalRange, replaceInitialOccurrence)
|
||||
}
|
||||
putAll(duplicates.map { it.range to { makeCall(descriptor, declaration, it.controlFlow, it.range, it.arguments) } })
|
||||
}
|
||||
|
||||
if (descriptor.typeParameters.isNotEmpty()) {
|
||||
for (ref in ReferencesSearch.search(declaration, LocalSearchScope(descriptor.getOccurrenceContainer()!!))) {
|
||||
|
||||
+3
-4
@@ -137,7 +137,6 @@ public class KotlinInplacePropertyIntroducer(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val occurrenceCount = extractionResult.duplicateReplacers.size() + 1
|
||||
if (occurrenceCount > 1) {
|
||||
addPanelControl(
|
||||
@@ -161,10 +160,10 @@ public class KotlinInplacePropertyIntroducer(
|
||||
return myElementToRename.parentsWithSelf.first { it is KtClassOrObject || it is KtFile }
|
||||
}
|
||||
|
||||
override fun moveOffsetAfter(success: Boolean) {
|
||||
super.moveOffsetAfter(success)
|
||||
if (success && replaceAll) {
|
||||
override fun performRefactoring(): Boolean {
|
||||
if (replaceAll) {
|
||||
processDuplicatesSilently(extractionResult.duplicateReplacers, myProject)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -45,7 +45,8 @@ public class KotlinIntroducePropertyHandler(
|
||||
val descriptor = descriptorWithConflicts.descriptor
|
||||
val target = propertyTargets.filter { it.isAvailable(descriptor) }.firstOrNull()
|
||||
if (target != null) {
|
||||
doRefactor(ExtractionGeneratorConfiguration(descriptor, ExtractionGeneratorOptions.DEFAULT.copy(target = target)), onFinish)
|
||||
val options = ExtractionGeneratorOptions.DEFAULT.copy(target = target, delayInitialOccurrenceReplacement = true)
|
||||
doRefactor(ExtractionGeneratorConfiguration(descriptor, options), onFinish)
|
||||
}
|
||||
else {
|
||||
showErrorHint(project, editor, "Can't introduce property for this expression", INTRODUCE_PROPERTY)
|
||||
|
||||
+1
-1
@@ -219,7 +219,7 @@ public abstract class AbstractExtractionTest() : KotlinLightCodeInsightFixtureTe
|
||||
doRefactor(
|
||||
ExtractionGeneratorConfiguration(
|
||||
descriptorWithConflicts.descriptor,
|
||||
ExtractionGeneratorOptions.DEFAULT.copy(target = extractionTarget)
|
||||
ExtractionGeneratorOptions.DEFAULT.copy(target = extractionTarget, delayInitialOccurrenceReplacement = true)
|
||||
),
|
||||
onFinish
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user