DeprecatedSymbolUsageInWholeProjectFix removes imports too

This commit is contained in:
Valentin Kipyatkov
2015-08-28 22:10:03 +03:00
parent e98edbcbac
commit 937f243046
7 changed files with 26 additions and 4 deletions
@@ -31,15 +31,18 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.targetDescriptors
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.stubindex.JetSourceFilterScope
import org.jetbrains.kotlin.idea.util.application.executeCommand
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.psi.JetImportDirective
import org.jetbrains.kotlin.psi.JetNamedFunction
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.renderer.NameShortness
import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy
@@ -95,9 +98,21 @@ public class DeprecatedSymbolUsageInWholeProjectFix(
UIUtil.invokeLaterIfNeeded {
project.executeCommand(getText()) {
runWriteAction {
// we should delete imports later to not affect other usages
val importsToDelete = arrayListOf<JetImportDirective>()
for (usage in usages) {
try {
if (!usage.isValid()) continue // TODO: nested calls
val importDirective = usage.getStrictParentOfType<JetImportDirective>()
if (importDirective != null) {
if (!importDirective.isAllUnder && importDirective.targetDescriptors().size() == 1) {
importsToDelete.add(importDirective)
}
continue
}
val bindingContext = usage.analyze(BodyResolveMode.PARTIAL)
val resolvedCall = usage.getResolvedCall(bindingContext) ?: continue
if (!resolvedCall.getStatus().isSuccess()) continue
@@ -108,6 +123,8 @@ public class DeprecatedSymbolUsageInWholeProjectFix(
LOG.error(e)
}
}
importsToDelete.forEach { it.delete() }
}
}
}
@@ -1,5 +1,4 @@
import newPack.newFun
import pack.oldFun
fun x() {
newFun(1 + 1)
@@ -1,7 +1,6 @@
// "Replace usages of 'oldFun(Int): Unit' in whole project" "true"
import newPack.newFun
import pack.oldFun
fun foo() {
<caret>newFun(0 + 1)
@@ -3,6 +3,8 @@ package pack
@deprecated("", ReplaceWith("newProp"))
val oldProp: String = ""
val String.oldProp: String get() = ""
val newProp: String = ""
fun foo(s: String){}
@@ -1,6 +1,8 @@
// "Replace usages of 'oldProp: String' in whole project" "true"
import pack.*
import pack.oldProp
import pack.foo
import pack.newProp
fun foo() {
foo(<caret>newProp)
@@ -3,6 +3,8 @@ package pack
@deprecated("", ReplaceWith("newProp"))
val oldProp: String = ""
val String.oldProp: String get() = ""
val newProp: String = ""
fun foo(s: String){}
@@ -1,6 +1,7 @@
// "Replace usages of 'oldProp: String' in whole project" "true"
import pack.*
import pack.oldProp
import pack.foo
fun foo() {
foo(<caret>oldProp)