Move: Do not update Kotlin usages of extension members, add imports instead

#KT-5378 Fixed
This commit is contained in:
Alexey Sedunov
2014-07-10 21:11:28 +04:00
parent 99ecb82ce0
commit 17dd5b9d05
112 changed files with 1061 additions and 5 deletions
@@ -38,8 +38,6 @@ import com.intellij.psi.PsiDirectory
import org.jetbrains.jet.lang.psi.psiUtil.getPackage
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.refactoring.move.PackageNameInfo
import org.jetbrains.jet.lang.psi.JetPsiUtil
import org.jetbrains.jet.lang.resolve.name.FqName
import org.jetbrains.jet.plugin.refactoring.createKotlinFile
import org.jetbrains.jet.plugin.refactoring.move.updateInternalReferencesOnPackageNameChange
import org.jetbrains.jet.plugin.codeInsight.addToShorteningWaitSet
@@ -52,7 +50,6 @@ import java.util.ArrayList
import java.util.HashSet
import com.intellij.psi.PsiReference
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.refactoring.util.MoveRenameUsageInfo
import com.intellij.refactoring.util.TextOccurrencesUtil
import com.intellij.refactoring.move.moveClassesOrPackages.MoveClassHandler
@@ -72,6 +69,11 @@ import com.intellij.openapi.util.Ref
import org.jetbrains.jet.plugin.refactoring.getKotlinFqName
import org.jetbrains.jet.plugin.references.JetSimpleNameReference.ShorteningMode
import org.jetbrains.jet.plugin.search.projectScope
import org.jetbrains.jet.lang.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.jet.plugin.references.JetReference
import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper
import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.JetImportDirective
public class MoveKotlinTopLevelDeclarationsOptions(
val elementsToMove: Collection<JetNamedDeclaration>,
@@ -81,6 +83,14 @@ public class MoveKotlinTopLevelDeclarationsOptions(
val moveCallback: MoveCallback? = null
)
class MoveRenameUsageInfoForExtension(
element: PsiElement,
reference: PsiReference,
startOffset: Int,
endOffset: Int,
referencedElement: PsiElement
): MoveRenameUsageInfo(element, reference, startOffset, endOffset, referencedElement, false)
public class MoveKotlinTopLevelDeclarationsProcessor(project: Project, val options: MoveKotlinTopLevelDeclarationsOptions) : BaseRefactoringProcessor(project) {
class object {
private val LOG: Logger = Logger.getInstance(javaClass<MoveKotlinTopLevelDeclarationsProcessor>())
@@ -101,6 +111,21 @@ public class MoveKotlinTopLevelDeclarationsProcessor(project: Project, val optio
}
override fun findUsages(): Array<UsageInfo> {
fun createUsageInfo(
element: PsiElement,
reference: PsiReference,
startOffset: Int,
endOffset: Int,
referencedElement: PsiElement
): UsageInfo {
if (reference is JetReference
&& referencedElement.namedUnwrappedElement!!.isExtensionDeclaration()
&& element.getParentByType(javaClass<JetImportDirective>()) == null) {
return MoveRenameUsageInfoForExtension(element, reference, startOffset, endOffset, referencedElement)
}
return MoveRenameUsageInfo(element, reference, startOffset, endOffset, referencedElement, false)
}
val newPackageName = options.moveTarget.packageWrapper?.getQualifiedName() ?: ""
fun collectUsages(): List<UsageInfo> {
@@ -114,7 +139,7 @@ public class MoveKotlinTopLevelDeclarationsProcessor(project: Project, val optio
.mapTo(ArrayList<UsageInfo?>()) { ref ->
if (foundReferences.add(ref)) {
val range = ref.getRangeInElement()!!
MoveRenameUsageInfo(ref.getElement(), ref, range.getStartOffset(), range.getEndOffset(), lightElement, false)
createUsageInfo(ref.getElement(), ref, range.getStartOffset(), range.getEndOffset(), lightElement)
}
else null
}
@@ -274,7 +299,20 @@ public class MoveKotlinTopLevelDeclarationsProcessor(project: Project, val optio
}
}
nonCodeUsages = CommonMoveUtil.retargetUsages(usages, oldToNewElementsMapping)
if (usages == null) return
nonCodeUsages = CommonMoveUtil.retargetUsages(
usages.filter { it !is MoveRenameUsageInfoForExtension }.copyToArray(),
oldToNewElementsMapping
)
for (usage in usages.filterIsInstance(javaClass<MoveRenameUsageInfoForExtension>())) {
val newElement = oldToNewElementsMapping[usage.getReferencedElement()]
ImportInsertHelper.addImportDirectiveIfNeeded(
newElement!!.getKotlinFqName()!!,
usage.getElement()!!.getContainingFile() as JetFile
)
}
}
catch (e: IncorrectOperationException) {
nonCodeUsages = null
@@ -0,0 +1,7 @@
package a
import b.test
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package a
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,7 @@
package a
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,9 @@
package a
import b.test
class Test {
fun foo() {
test()
}
}
@@ -0,0 +1,7 @@
package a;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,7 @@
package a
import b.test
fun bar() {
Test().test()
}
@@ -0,0 +1,7 @@
package a;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package a
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package a;
import static b.BPackage.test;
class J {
void bar() {
test(new Test());
}
}
@@ -0,0 +1,30 @@
package b
import a.Test
open class Foo {
open class Bar {
}
}
fun Test.test() {
val aFoo: a.Foo = a.Foo()
val bFoo: Foo = Foo()
val cFoo: c.Foo = c.Foo()
val aBar: a.Foo.Bar = a.Foo.Bar()
val bBar: Foo.Bar = Foo.Bar()
val cBar: c.Foo.Bar = c.Foo.Bar()
fun foo(u: Int) {
class T(val t: Int)
object O {
val t: Int = 1
}
val v = T(u).t + O.t
println(v)
}
foo(1)
}
@@ -0,0 +1,7 @@
package b;
class J {
void bar() {
b.BPackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package b;
import a.*;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,7 @@
package b
import a.*
fun bar() {
Test().test()
}
@@ -0,0 +1,7 @@
package b;
class J {
void bar() {
b.BPackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package b;
import a.Test;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package b
import b.test
import a.Test
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package b
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package b;
import static b.BPackage.test;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,7 @@
package c
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,7 @@
package c;
class J {
void bar() {
b.BPackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package c;
import a.*;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package c
import a.*
import b.test
fun bar() {
Test().test()
}
@@ -0,0 +1,7 @@
package c;
class J {
void bar() {
b.BPackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package c;
import a.Test;
class J {
void bar() {
b.BPackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package c
import b.test
import a.Test
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package c
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package c;
import static b.BPackage.test;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,5 @@
package a
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package a
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,7 @@
package a
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,28 @@
package a
fun Test.<caret>test() {
val aFoo: Foo = Foo()
val bFoo: b.Foo = b.Foo()
val cFoo: c.Foo = c.Foo()
val aBar: Foo.Bar = Foo.Bar()
val bBar: b.Foo.Bar = b.Foo.Bar()
val cBar: c.Foo.Bar = c.Foo.Bar()
fun foo(u: Int) {
class T(val t: Int)
object O {
val t: Int = 1
}
val v = T(u).t + O.t
println(v)
}
foo(1)
}
class Test {
fun foo() {
test()
}
}
@@ -0,0 +1,7 @@
package a;
class J {
void bar() {
APackage.test(new Test());
}
}
@@ -0,0 +1,5 @@
package a
fun bar() {
Test().test()
}
@@ -0,0 +1,9 @@
package a;
import static a.APackage.*;
class J {
void bar() {
test(new Test());
}
}
@@ -0,0 +1,8 @@
package a
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package a;
import static a.APackage.test;
class J {
void bar() {
test(new Test());
}
}
@@ -0,0 +1,8 @@
package b
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,7 @@
package b;
class J {
void bar() {
a.APackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package b;
import a.*;
class J {
void bar() {
APackage.test(new Test());
}
}
@@ -0,0 +1,7 @@
package b
import a.*
fun bar() {
Test().test()
}
@@ -0,0 +1,9 @@
package b;
import static a.APackage.*;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,10 @@
package b;
import a.APackage;
import a.Test;
class J {
void bar() {
APackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package b
import a.test
import a.Test
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package b
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package b;
import static a.APackage.test;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,7 @@
package c
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,7 @@
package c;
class J {
void bar() {
a.APackage.test(new a.Test());
}
}
@@ -0,0 +1,9 @@
package c;
import a.*;
class J {
void bar() {
APackage.test(new Test());
}
}
@@ -0,0 +1,7 @@
package c
import a.*
fun bar() {
Test().test()
}
@@ -0,0 +1,9 @@
package c;
import static a.APackage.*;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,10 @@
package c;
import a.APackage;
import a.Test;
class J {
void bar() {
APackage.test(new Test());
}
}
@@ -0,0 +1,8 @@
package c
import a.test
import a.Test
fun bar() {
Test().test()
}
@@ -0,0 +1,8 @@
package c
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test()
}
@@ -0,0 +1,9 @@
package c;
import static a.APackage.test;
class J {
void bar() {
test(new a.Test());
}
}
@@ -0,0 +1,5 @@
{
"mainFile": "a/main.kt",
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
"targetFile": "b/dependency.kt"
}
@@ -0,0 +1,8 @@
package a
import b.test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,9 @@
package a
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,7 @@
package a
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,10 @@
package a
import b.test
class Test {
fun foo() {
test
test = 0
}
}
@@ -0,0 +1,8 @@
package a;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,8 @@
package a
import b.test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,8 @@
package a;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package a
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,11 @@
package a;
import static b.BPackage.getTest;
import static b.BPackage.setTest;
class J {
void bar() {
getTest(new Test());
setTest(new Test(), 0);
}
}
@@ -0,0 +1,32 @@
package b
import a.Test
open class Foo {
open class Bar {
}
}
var Test.test: Int
get() = 0
set(value: Int) {
val aFoo: a.Foo = a.Foo()
val bFoo: Foo = Foo()
val cFoo: c.Foo = c.Foo()
val aBar: a.Foo.Bar = a.Foo.Bar()
val bBar: Foo.Bar = Foo.Bar()
val cBar: c.Foo.Bar = c.Foo.Bar()
fun foo(u: Int) {
class T(val t: Int)
object O {
val t: Int = 1
}
val v = T(u).t + O.t
println(v)
}
foo(1)
}
@@ -0,0 +1,8 @@
package b;
class J {
void bar() {
b.BPackage.getTest(new a.Test());
b.BPackage.setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,10 @@
package b;
import a.*;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,8 @@
package b
import a.*
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,8 @@
package b;
class J {
void bar() {
b.BPackage.getTest(new a.Test());
b.BPackage.setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,10 @@
package b;
import a.Test;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package b
import b.test
import a.Test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,9 @@
package b
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,11 @@
package b;
import static b.BPackage.getTest;
import static b.BPackage.setTest;
class J {
void bar() {
getTest(new a.Test());
setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,7 @@
package c
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,8 @@
package c;
class J {
void bar() {
b.BPackage.getTest(new a.Test());
b.BPackage.setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,10 @@
package c;
import a.*;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package c
import a.*
import b.test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,8 @@
package c;
class J {
void bar() {
b.BPackage.getTest(new a.Test());
b.BPackage.setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,10 @@
package c;
import a.Test;
class J {
void bar() {
b.BPackage.getTest(new Test());
b.BPackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package c
import b.test
import a.Test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,9 @@
package c
import b.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,11 @@
package c;
import static b.BPackage.getTest;
import static b.BPackage.setTest;
class J {
void bar() {
getTest(new a.Test());
setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,6 @@
package a
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,9 @@
package a
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,7 @@
package a
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,31 @@
package a
var Test.<caret>test: Int
get() = 0
set(value: Int) {
val aFoo: Foo = Foo()
val bFoo: b.Foo = b.Foo()
val cFoo: c.Foo = c.Foo()
val aBar: Foo.Bar = Foo.Bar()
val bBar: b.Foo.Bar = b.Foo.Bar()
val cBar: c.Foo.Bar = c.Foo.Bar()
fun foo(u: Int) {
class T(val t: Int)
object O {
val t: Int = 1
}
val v = T(u).t + O.t
println(v)
}
foo(1)
}
class Test {
fun foo() {
test
test = 0
}
}
@@ -0,0 +1,8 @@
package a;
class J {
void bar() {
APackage.getTest(new Test());
APackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,6 @@
package a
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,10 @@
package a;
import static a.APackage.*;
class J {
void bar() {
getTest(new Test());
setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package a
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}
@@ -0,0 +1,11 @@
package a;
import static a.APackage.getTest;
import static a.APackage.setTest;
class J {
void bar() {
getTest(new Test());
setTest(new Test(), 0);
}
}
@@ -0,0 +1,8 @@
package b
open class Foo {
open class Bar {
}
}
@@ -0,0 +1,8 @@
package b;
class J {
void bar() {
a.APackage.getTest(new a.Test());
a.APackage.setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,10 @@
package b;
import a.*;
class J {
void bar() {
APackage.getTest(new Test());
APackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,8 @@
package b
import a.*
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,10 @@
package b;
import static a.APackage.*;
class J {
void bar() {
getTest(new a.Test());
setTest(new a.Test(), 0);
}
}
@@ -0,0 +1,11 @@
package b;
import a.APackage;
import a.Test;
class J {
void bar() {
APackage.getTest(new Test());
APackage.setTest(new Test(), 0);
}
}
@@ -0,0 +1,9 @@
package b
import a.test
import a.Test
fun bar() {
Test().test
Test().test = 0
}
@@ -0,0 +1,9 @@
package b
import a.test as _test
import a.Test as _Test
fun bar() {
_Test()._test
_Test()._test = 0
}

Some files were not shown because too many files have changed in this diff Show More