Rename: Do not report redeclaration conflict for private top-level declarations located in different files
#KT-14361 Fixed
This commit is contained in:
@@ -340,6 +340,7 @@ These artifacts include extensions for the types available in the latter JDKs, s
|
||||
- [`KT-14583`](https://youtrack.jetbrains.com/issue/KT-14583) Change Signature: Use new signature when looking for redeclaration conflicts
|
||||
- [`KT-14854`](https://youtrack.jetbrains.com/issue/KT-14854) Extract Interface: Fix NPE on dialog opening
|
||||
- [`KT-14814`](https://youtrack.jetbrains.com/issue/KT-14814) Rename: Fix renaming of .kts file to .kt and vice versa
|
||||
- [`KT-14361`](https://youtrack.jetbrains.com/issue/KT-14361) Rename: Do not report redeclaration conflict for private top-level declarations located in different files
|
||||
|
||||
## 1.0.5
|
||||
|
||||
|
||||
@@ -86,14 +86,24 @@ internal fun checkRedeclarations(
|
||||
newName: String,
|
||||
result: MutableList<UsageInfo>
|
||||
) {
|
||||
fun MemberScope.findSiblingByName(): DeclarationDescriptor? {
|
||||
fun DeclarationDescriptor.isTopLevelPrivate(): Boolean {
|
||||
return this is DeclarationDescriptorWithVisibility
|
||||
&& visibility == Visibilities.PRIVATE
|
||||
&& containingDeclaration is PackageFragmentDescriptor
|
||||
}
|
||||
|
||||
fun isInSameFile(d1: DeclarationDescriptor, d2: DeclarationDescriptor): Boolean {
|
||||
return (d1 as? DeclarationDescriptorWithSource)?.source?.getPsi()?.containingFile == (d2 as? DeclarationDescriptorWithSource)?.source?.getPsi()?.containingFile
|
||||
}
|
||||
|
||||
fun MemberScope.findSiblingsByName(): List<DeclarationDescriptor> {
|
||||
val descriptorKindFilter = when (descriptor) {
|
||||
is ClassDescriptor -> DescriptorKindFilter.CLASSIFIERS
|
||||
is PropertyDescriptor -> DescriptorKindFilter.VARIABLES
|
||||
is FunctionDescriptor -> DescriptorKindFilter.FUNCTIONS
|
||||
else -> return null
|
||||
else -> return emptyList()
|
||||
}
|
||||
return getDescriptorsFiltered(descriptorKindFilter) { it.asString() == newName }.firstOrNull { it != descriptor }
|
||||
return getDescriptorsFiltered(descriptorKindFilter) { it.asString() == newName }.filter { it != descriptor }
|
||||
}
|
||||
|
||||
fun getSiblingWithNewName(): DeclarationDescriptor? {
|
||||
@@ -120,8 +130,11 @@ internal fun checkRedeclarations(
|
||||
}
|
||||
|
||||
return when (containingDescriptor) {
|
||||
is ClassDescriptor -> containingDescriptor.unsubstitutedMemberScope.findSiblingByName()
|
||||
is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().findSiblingByName()
|
||||
is ClassDescriptor -> containingDescriptor.unsubstitutedMemberScope.findSiblingsByName().firstOrNull()
|
||||
is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().findSiblingsByName().firstOrNull {
|
||||
it != descriptor
|
||||
&& (!(descriptor.isTopLevelPrivate() || it.isTopLevelPrivate()) || isInSameFile(descriptor, it))
|
||||
}
|
||||
else -> {
|
||||
val block = (descriptor as? DeclarationDescriptorWithSource)?.source?.getPsi()?.parent as? KtBlockExpression
|
||||
?: return null
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
val /*rename*/foo = 1
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
val bar = 2
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"type": "MARKED_ELEMENT",
|
||||
"mainFile": "test1.kt",
|
||||
"newName": "bar",
|
||||
"hint": "Property 'bar' is already declared in package 'p'"
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
val bar = 1
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val bar = 2
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
val /*rename*/foo = 1
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val bar = 2
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "MARKED_ELEMENT",
|
||||
"mainFile": "test1.kt",
|
||||
"newName": "bar"
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val bar = 1
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val bar = 2
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val /*rename*/foo = 1
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p
|
||||
|
||||
private val bar = 2
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "MARKED_ELEMENT",
|
||||
"mainFile": "test1.kt",
|
||||
"newName": "bar"
|
||||
}
|
||||
@@ -246,6 +246,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nonPrivateTopLevelDeclarationsConflict/nonPrivateTopLevelDeclarationsNoConflict.test")
|
||||
public void testNonPrivateTopLevelDeclarationsConflict_NonPrivateTopLevelDeclarationsNoConflict() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/nonPrivateTopLevelDeclarationsConflict/nonPrivateTopLevelDeclarationsNoConflict.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noShadowingConflictForSiblingDeclarations/noShadowingConflictForSiblingDeclarations.test")
|
||||
public void testNoShadowingConflictForSiblingDeclarations_NoShadowingConflictForSiblingDeclarations() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/noShadowingConflictForSiblingDeclarations/noShadowingConflictForSiblingDeclarations.test");
|
||||
@@ -294,6 +300,18 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("privateAndNonPrivateTopLevelDeclarationsNoConflict/privateAndNonPrivateTopLevelDeclarationsNoConflict.test")
|
||||
public void testPrivateAndNonPrivateTopLevelDeclarationsNoConflict_PrivateAndNonPrivateTopLevelDeclarationsNoConflict() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/privateAndNonPrivateTopLevelDeclarationsNoConflict/privateAndNonPrivateTopLevelDeclarationsNoConflict.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("privateTopLevelDeclarationsNoConflict/privateTopLevelDeclarationsNoConflict.test")
|
||||
public void testPrivateTopLevelDeclarationsNoConflict_PrivateTopLevelDeclarationsNoConflict() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/privateTopLevelDeclarationsNoConflict/privateTopLevelDeclarationsNoConflict.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyAccidentalOverrideSubclass/propertyAccidentalOverrideSubclass.test")
|
||||
public void testPropertyAccidentalOverrideSubclass_PropertyAccidentalOverrideSubclass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/propertyAccidentalOverrideSubclass/propertyAccidentalOverrideSubclass.test");
|
||||
|
||||
Reference in New Issue
Block a user