Minor: Extract container selection logic
This commit is contained in:
+1
-1
@@ -72,7 +72,7 @@ public class ExtractKotlinFunctionHandler(
|
||||
}
|
||||
|
||||
fun selectElements(editor: Editor, file: PsiFile, continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit) {
|
||||
selectElements(
|
||||
selectElementsWithTargetSibling(
|
||||
EXTRACT_FUNCTION,
|
||||
editor,
|
||||
file,
|
||||
|
||||
+1
-1
@@ -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,
|
||||
|
||||
@@ -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<PsiElement>, commonParent: PsiElement) -> List<PsiElement>,
|
||||
continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit
|
||||
) {
|
||||
fun showErrorHintByKey(key: String) {
|
||||
showErrorHintByKey(file.getProject(), editor, key, operationName)
|
||||
}
|
||||
fun onSelectionComplete(elements: List<PsiElement>, targetContainer: PsiElement) {
|
||||
val parent = PsiTreeUtil.findCommonParent(elements)
|
||||
?: throw AssertionError("Should have at least one parent: ${elements.joinToString("\n")}")
|
||||
|
||||
fun onSelectionComplete(parent: PsiElement, elements: List<PsiElement>, 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<PsiElement>, commonParent: PsiElement) -> List<PsiElement>,
|
||||
continuation: (elements: List<PsiElement>, targetParent: PsiElement) -> Unit
|
||||
) {
|
||||
fun showErrorHintByKey(key: String) {
|
||||
showErrorHintByKey(file.getProject(), editor, key, operationName)
|
||||
}
|
||||
|
||||
fun selectTargetContainer(elements: List<PsiElement>) {
|
||||
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) }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user