Extract property: do not suggest duplicate property name

#KT-36504 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-02-12 05:28:44 +01:00
committed by Vladimir Dolzhenko
parent dc949658d6
commit a96cafe066
5 changed files with 43 additions and 6 deletions
@@ -16,6 +16,7 @@ import com.intellij.refactoring.BaseRefactoringProcessor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.core.util.isMultiLine
@@ -29,11 +30,15 @@ import org.jetbrains.kotlin.idea.refactoring.introduce.*
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.*
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValueBoxer.AsTuple
import org.jetbrains.kotlin.idea.refactoring.removeTemplateEntryBracesIfPossible
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.getAllAccessibleVariables
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.psi.patternMatching.*
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.StronglyMatched
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.WeaklyMatched
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtPsiFactory.CallableBuilder
import org.jetbrains.kotlin.psi.psiUtil.*
@@ -681,10 +686,19 @@ fun ExtractionGeneratorConfiguration.generateDeclaration(
}
}
if (declaration is KtProperty && declaration.isExtensionDeclaration() && !declaration.isTopLevel) {
val receiverTypeReference = (declaration as? KtCallableDeclaration)?.receiverTypeReference
receiverTypeReference?.siblings(withItself = false)?.firstOrNull { it.node.elementType == KtTokens.DOT }?.delete()
receiverTypeReference?.delete()
if (declaration is KtProperty) {
if (declaration.isExtensionDeclaration() && !declaration.isTopLevel) {
val receiverTypeReference = (declaration as? KtCallableDeclaration)?.receiverTypeReference
receiverTypeReference?.siblings(withItself = false)?.firstOrNull { it.node.elementType == KtTokens.DOT }?.delete()
receiverTypeReference?.delete()
}
if ((declaration.descriptor as? PropertyDescriptor)?.let { DescriptorUtils.isOverride(it) } == true) {
val scope = declaration.getResolutionScope()
val newName = KotlinNameSuggester.suggestNameByName(descriptor.name) {
it != descriptor.name && scope.getAllAccessibleVariables(Name.identifier(it)).isEmpty()
}
declaration.setName(newName)
}
}
CodeStyleManager.getInstance(descriptor.extractionData.project).reformat(declaration)
@@ -0,0 +1,8 @@
// EXTRACTION_TARGET: property with initializer
open class Base(protected val i: Int)
class Impl(z: Int) : Base(z) {
fun foo(): Int {
return <selection>2 + 3 + i</selection>
}
}
@@ -0,0 +1,10 @@
// EXTRACTION_TARGET: property with initializer
open class Base(protected val i: Int)
class Impl(z: Int) : Base(z) {
private val i1 = 2 + 3 + i
fun foo(): Int {
return i1
}
}
@@ -3,10 +3,10 @@
open class Base(protected val i: Int)
class Impl(i: Int) : Base(i) {
private val i: Int
private val i1: Int
get() = 2 + 3 + i
fun foo(): Int {
return i
return i1
}
}
@@ -2787,6 +2787,11 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/refactoring/introduceProperty"), Pattern.compile("^(.+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("conflictWithParentClass.kt")
public void testConflictWithParentClass() throws Exception {
runTest("idea/testData/refactoring/introduceProperty/conflictWithParentClass.kt");
}
@TestMetadata("extractExtensionWithInitializer.kt")
public void testExtractExtensionWithInitializer() throws Exception {
runTest("idea/testData/refactoring/introduceProperty/extractExtensionWithInitializer.kt");