diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractFunction/ExtractKotlinFunctionHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractFunction/ExtractKotlinFunctionHandler.kt index e954dd6da4c..37f647e7d08 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractFunction/ExtractKotlinFunctionHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractFunction/ExtractKotlinFunctionHandler.kt @@ -72,7 +72,7 @@ public class ExtractKotlinFunctionHandler( } fun selectElements(editor: Editor, file: PsiFile, continuation: (elements: List, targetSibling: PsiElement) -> Unit) { - selectElements( + selectElementsWithTargetSibling( EXTRACT_FUNCTION, editor, file, diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceProperty/KotlinIntroducePropertyHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceProperty/KotlinIntroducePropertyHandler.kt index faf2117dc7b..575c3a51dd7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceProperty/KotlinIntroducePropertyHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceProperty/KotlinIntroducePropertyHandler.kt @@ -54,7 +54,7 @@ public class KotlinIntroducePropertyHandler( override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) { if (file !is JetFile) return - selectElements( + selectElementsWithTargetSibling( operationName = INTRODUCE_PROPERTY, editor = editor, file = file, diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceUtil.kt index b48e7882301..1a04d6f603f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceUtil.kt @@ -33,18 +33,17 @@ fun showErrorHintByKey(project: Project, editor: Editor, messageKey: String, tit showErrorHint(project, editor, JetRefactoringBundle.message(messageKey), title) } -fun selectElements( +fun selectElementsWithTargetSibling( operationName: String, editor: Editor, file: PsiFile, getContainers: (elements: List, commonParent: PsiElement) -> List, continuation: (elements: List, targetSibling: PsiElement) -> Unit ) { - fun showErrorHintByKey(key: String) { - showErrorHintByKey(file.getProject(), editor, key, operationName) - } + fun onSelectionComplete(elements: List, targetContainer: PsiElement) { + val parent = PsiTreeUtil.findCommonParent(elements) + ?: throw AssertionError("Should have at least one parent: ${elements.joinToString("\n")}") - fun onSelectionComplete(parent: PsiElement, elements: List, targetContainer: PsiElement) { if (parent == targetContainer) { continuation(elements, elements.first()) return @@ -52,13 +51,27 @@ fun selectElements( val outermostParent = parent.getOutermostParentContainedIn(targetContainer) if (outermostParent == null) { - showErrorHintByKey("cannot.refactor.no.container") + showErrorHintByKey(file.getProject(), editor, "cannot.refactor.no.container", operationName) return } continuation(elements, outermostParent) } + selectElementsWithTargetParent(operationName, editor, file, getContainers, ::onSelectionComplete) +} + +fun selectElementsWithTargetParent( + operationName: String, + editor: Editor, + file: PsiFile, + getContainers: (elements: List, commonParent: PsiElement) -> List, + continuation: (elements: List, targetParent: PsiElement) -> Unit +) { + fun showErrorHintByKey(key: String) { + showErrorHintByKey(file.getProject(), editor, key, operationName) + } + fun selectTargetContainer(elements: List) { val parent = PsiTreeUtil.findCommonParent(elements) ?: throw AssertionError("Should have at least one parent: ${elements.joinToString("\n")}") @@ -75,7 +88,7 @@ fun selectElements( "Select target code block", true, { it }, - { onSelectionComplete(parent, elements, it) } + { continuation(elements, it) } ) }