Minor: Extract container selection logic

This commit is contained in:
Alexey Sedunov
2015-04-02 17:38:09 +03:00
parent d3605a5352
commit ee8df94f19
3 changed files with 22 additions and 9 deletions
@@ -72,7 +72,7 @@ public class ExtractKotlinFunctionHandler(
} }
fun selectElements(editor: Editor, file: PsiFile, continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit) { fun selectElements(editor: Editor, file: PsiFile, continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit) {
selectElements( selectElementsWithTargetSibling(
EXTRACT_FUNCTION, EXTRACT_FUNCTION,
editor, editor,
file, file,
@@ -54,7 +54,7 @@ public class KotlinIntroducePropertyHandler(
override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) { override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) {
if (file !is JetFile) return if (file !is JetFile) return
selectElements( selectElementsWithTargetSibling(
operationName = INTRODUCE_PROPERTY, operationName = INTRODUCE_PROPERTY,
editor = editor, editor = editor,
file = file, file = file,
@@ -33,18 +33,17 @@ fun showErrorHintByKey(project: Project, editor: Editor, messageKey: String, tit
showErrorHint(project, editor, JetRefactoringBundle.message(messageKey), title) showErrorHint(project, editor, JetRefactoringBundle.message(messageKey), title)
} }
fun selectElements( fun selectElementsWithTargetSibling(
operationName: String, operationName: String,
editor: Editor, editor: Editor,
file: PsiFile, file: PsiFile,
getContainers: (elements: List<PsiElement>, commonParent: PsiElement) -> List<PsiElement>, getContainers: (elements: List<PsiElement>, commonParent: PsiElement) -> List<PsiElement>,
continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit continuation: (elements: List<PsiElement>, targetSibling: PsiElement) -> Unit
) { ) {
fun showErrorHintByKey(key: String) { fun onSelectionComplete(elements: List<PsiElement>, targetContainer: PsiElement) {
showErrorHintByKey(file.getProject(), editor, key, operationName) 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) { if (parent == targetContainer) {
continuation(elements, elements.first()) continuation(elements, elements.first())
return return
@@ -52,13 +51,27 @@ fun selectElements(
val outermostParent = parent.getOutermostParentContainedIn(targetContainer) val outermostParent = parent.getOutermostParentContainedIn(targetContainer)
if (outermostParent == null) { if (outermostParent == null) {
showErrorHintByKey("cannot.refactor.no.container") showErrorHintByKey(file.getProject(), editor, "cannot.refactor.no.container", operationName)
return return
} }
continuation(elements, outermostParent) 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>) { fun selectTargetContainer(elements: List<PsiElement>) {
val parent = PsiTreeUtil.findCommonParent(elements) val parent = PsiTreeUtil.findCommonParent(elements)
?: throw AssertionError("Should have at least one parent: ${elements.joinToString("\n")}") ?: throw AssertionError("Should have at least one parent: ${elements.joinToString("\n")}")
@@ -75,7 +88,7 @@ fun selectElements(
"Select target code block", "Select target code block",
true, true,
{ it }, { it },
{ onSelectionComplete(parent, elements, it) } { continuation(elements, it) }
) )
} }