From dc949658d611c7611a0a0ad9b9c758a87aa984b5 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Wed, 25 Mar 2020 17:26:12 +0000 Subject: [PATCH] Lookup super methods under progress in find usages Do not try to lookup super methods if named declaration does not have `override` keyword #KT-18472 Fixed --- .../messages/KotlinBundle.properties | 1 + .../idea/refactoring/kotlinRefactoringUtil.kt | 59 +++++++++++-------- .../idea/util/ProgressIndicatorUtils.kt | 2 +- .../idea/util/ProgressIndicatorUtils.kt.191 | 1 + .../idea/util/ProgressIndicatorUtils.kt.192 | 1 + 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/idea/resources/messages/KotlinBundle.properties b/idea/resources/messages/KotlinBundle.properties index db6c63b2958..2f9addfe385 100644 --- a/idea/resources/messages/KotlinBundle.properties +++ b/idea/resources/messages/KotlinBundle.properties @@ -502,6 +502,7 @@ find.usages.type.receiver=Receiver find.usages.type.delegate=Delegate find.usages.type.packageDirective=Package directive find.usages.type.packageMemberAccess=Package member access +find.usages.progress.text.declaration.superMethods=Resolving super methods... formatter.button.text.use.import.with.when.at.least=Use import with '*' when at least formatter.button.text.use.import.with=Use import with '*' diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt index 7323018bf6b..ee62793dfc4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt @@ -28,6 +28,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.Messages import com.intellij.openapi.ui.popup.* +import com.intellij.openapi.util.Computable import com.intellij.openapi.util.Pass import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.text.StringUtil @@ -76,12 +77,10 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.toValVar import org.jetbrains.kotlin.idea.refactoring.memberInfo.KtPsiClassWrapper import org.jetbrains.kotlin.idea.refactoring.rename.canonicalRender import org.jetbrains.kotlin.idea.roots.isOutsideKotlinAwareSourceRoot -import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers -import org.jetbrains.kotlin.idea.util.ProjectRootsUtil -import org.jetbrains.kotlin.idea.util.actualsForExpected -import org.jetbrains.kotlin.idea.util.liftToExpected +import org.jetbrains.kotlin.idea.util.* import org.jetbrains.kotlin.idea.util.string.collapseSpaces import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.lexer.KtTokens.OVERRIDE_KEYWORD import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.psi.* @@ -855,6 +854,39 @@ fun checkSuperMethods( ignore: Collection?, @Nls actionString: String ): List { + if (!declaration.hasModifier(OVERRIDE_KEYWORD)) return listOf(declaration) + + val project = declaration.project + + val (declarationDescriptor, overriddenElementsToDescriptor) = + ProgressIndicatorUtils.underModalProgress( + project, + KotlinBundle.message("find.usages.progress.text.declaration.superMethods"), + Computable { + val declarationDescriptor = declaration.unsafeResolveToDescriptor() as CallableDescriptor + + if (declarationDescriptor is LocalVariableDescriptor) return@Computable (declarationDescriptor to emptyMap()) + + val overriddenElementsToDescriptor = HashMap() + for (overriddenDescriptor in DescriptorUtils.getAllOverriddenDescriptors( + declarationDescriptor + )) { + val overriddenDeclaration = DescriptorToSourceUtilsIde.getAnyDeclaration( + project, + overriddenDescriptor + ) ?: continue + if (overriddenDeclaration is KtNamedFunction || overriddenDeclaration is KtProperty || overriddenDeclaration is PsiMethod || overriddenDeclaration is KtParameter) { + overriddenElementsToDescriptor[overriddenDeclaration] = overriddenDescriptor + } + } + if (ignore != null) { + overriddenElementsToDescriptor.keys.removeAll(ignore) + } + (declarationDescriptor to overriddenElementsToDescriptor) + }) + + if (overriddenElementsToDescriptor.isEmpty()) return listOf(declaration) + fun getClassDescriptions(overriddenElementsToDescriptor: Map): List { return overriddenElementsToDescriptor.entries.map { entry -> val (element, descriptor) = entry @@ -894,25 +926,6 @@ fun checkSuperMethods( } } - - val declarationDescriptor = declaration.unsafeResolveToDescriptor() as CallableDescriptor - - if (declarationDescriptor is LocalVariableDescriptor) return listOf(declaration) - - val project = declaration.project - val overriddenElementsToDescriptor = HashMap() - for (overriddenDescriptor in DescriptorUtils.getAllOverriddenDescriptors(declarationDescriptor)) { - val overriddenDeclaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, overriddenDescriptor) ?: continue - if (overriddenDeclaration is KtNamedFunction || overriddenDeclaration is KtProperty || overriddenDeclaration is PsiMethod || overriddenDeclaration is KtParameter) { - overriddenElementsToDescriptor[overriddenDeclaration] = overriddenDescriptor - } - } - if (ignore != null) { - overriddenElementsToDescriptor.keys.removeAll(ignore) - } - - if (overriddenElementsToDescriptor.isEmpty()) return listOf(declaration) - return askUserForMethodsToSearch(declarationDescriptor, overriddenElementsToDescriptor) } diff --git a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt index 5ec3cfa97e0..88ec6be652e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt @@ -21,7 +21,7 @@ import java.util.concurrent.TimeoutException * Copied from [com.intellij.openapi.progress.util.ProgressIndicatorUtils] */ object ProgressIndicatorUtils { - @Throws(ProcessCanceledException::class) + @JvmStatic fun underModalProgress( project: Project, @Nls progressTitle: String, diff --git a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.191 b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.191 index 7ec156f4c29..992db1fc059 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.191 +++ b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.191 @@ -21,6 +21,7 @@ import java.util.concurrent.TimeoutException object ProgressIndicatorUtils { private val LOG = Logger.getInstance(ProgressIndicatorUtils::class.java) + @JvmStatic fun underModalProgress( project: Project, @Nls progressTitle: String, diff --git a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.192 b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.192 index 3fdc0aa8b2c..6018c1b387d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.192 +++ b/idea/src/org/jetbrains/kotlin/idea/util/ProgressIndicatorUtils.kt.192 @@ -21,6 +21,7 @@ import java.util.concurrent.TimeoutException object ProgressIndicatorUtils { private val LOG = Logger.getInstance(ProgressIndicatorUtils::class.java) + @JvmStatic fun underModalProgress( project: Project, @Nls progressTitle: String,