From 071744a57f062f69bfafef677c2edec8d631be0d Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 1 Mar 2017 14:23:53 +0300 Subject: [PATCH] Extract Superclass/Interface: show inapplicability error before choosing the extraction container #KT-15339 Fixed --- .../KotlinExtractInterfaceHandler.kt | 2 -- .../KotlinExtractSuperHandlerBase.kt | 24 +++++++++++++++++++ .../KotlinExtractSuperclassHandler.kt | 6 ----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractInterfaceHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractInterfaceHandler.kt index 2900d9b445a..dd133987d88 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractInterfaceHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractInterfaceHandler.kt @@ -33,8 +33,6 @@ object KotlinExtractInterfaceHandler : KotlinExtractSuperHandlerBase(true) { } override fun doInvoke(klass: KtClassOrObject, targetParent: PsiElement, project: Project, editor: Editor?) { - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, klass)) return - KotlinExtractInterfaceDialog( originalClass = klass, targetParent = targetParent, diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperHandlerBase.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperHandlerBase.kt index af4b4e19dcb..fc6297bc273 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperHandlerBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperHandlerBase.kt @@ -24,9 +24,12 @@ import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil +import com.intellij.refactoring.HelpID import com.intellij.refactoring.RefactoringActionHandler +import com.intellij.refactoring.RefactoringBundle import com.intellij.refactoring.extractSuperclass.ExtractSuperClassUtil import com.intellij.refactoring.lang.ElementsHandler +import com.intellij.refactoring.util.CommonRefactoringUtil import org.jetbrains.kotlin.idea.refactoring.SeparateFileWrapper import org.jetbrains.kotlin.idea.refactoring.chooseContainerElementIfNecessary import org.jetbrains.kotlin.idea.refactoring.getExtractionContainers @@ -42,6 +45,7 @@ abstract class KotlinExtractSuperHandlerBase(private val isExtractInterface: Boo val offset = editor.caretModel.offset val element = file.findElementAt(offset) ?: return val klass = element.getNonStrictParentOfType() ?: return + if (!checkClass(klass, editor)) return editor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE) selectElements(klass, project, editor) } @@ -50,9 +54,29 @@ abstract class KotlinExtractSuperHandlerBase(private val isExtractInterface: Boo if (dataContext == null) return val editor = CommonDataKeys.EDITOR.getData(dataContext) val klass = PsiTreeUtil.findCommonParent(*elements)?.getNonStrictParentOfType() ?: return + if (!checkClass(klass, editor)) return selectElements(klass, project, editor) } + private fun checkClass(klass: KtClassOrObject, editor: Editor?): Boolean { + val project = klass.project + + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, klass)) return false + + getErrorMessage(klass)?.let { + CommonRefactoringUtil.showErrorHint( + project, + editor, + RefactoringBundle.getCannotRefactorMessage(it), + KotlinExtractSuperclassHandler.REFACTORING_NAME, + HelpID.EXTRACT_SUPERCLASS + ) + return false + } + + return true + } + fun selectElements(klass: KtClassOrObject, project: Project, editor: Editor?) { val containers = klass.getExtractionContainers(strict = true, includeAll = true) + SeparateFileWrapper(klass.manager) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperclassHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperclassHandler.kt index f4dae1b2d0f..d8fa1bfe4fa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperclassHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractClass/KotlinExtractSuperclassHandler.kt @@ -39,12 +39,6 @@ object KotlinExtractSuperclassHandler : KotlinExtractSuperHandlerBase(false) { } override fun doInvoke(klass: KtClassOrObject, targetParent: PsiElement, project: Project, editor: Editor?) { - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, klass)) return - - getErrorMessage(klass)?.let { - CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage(it), REFACTORING_NAME, HelpID.EXTRACT_SUPERCLASS) - } - KotlinExtractSuperclassDialog( originalClass = klass, targetParent = targetParent,