From 59f37a8291aa4fe0b457480299f8d680de1ed661 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 23 Jun 2016 17:44:15 +0300 Subject: [PATCH] Introduce Property: Do not skip outer classes if extractable expression is contained in object literal. Skip outer classes of non-inner class #KT-12084 Fixed (cherry picked from commit 2852f44) --- ChangeLog.md | 1 + .../idea/refactoring/kotlinRefactoringUtil.kt | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 564276f458c..a6c2ec99431 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -119,6 +119,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - [`KT-12294`](https://youtrack.jetbrains.com/issue/KT-12294) Introduce Property: Fix extraction of expressions referring to primary constructor parameters - [`KT-12413`](https://youtrack.jetbrains.com/issue/KT-12413) Change Signature: Fix bogus warning about unresolved type parameters/invalid functional type replacement +- [`KT-12084`](https://youtrack.jetbrains.com/issue/KT-12084) Introduce Property: Do not skip outer classes if extractable expression is contained in object literal ## 1.0.3 diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt index 6ecb2388d84..aabfeeb800c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/kotlinRefactoringUtil.kt @@ -146,20 +146,25 @@ fun KtFile.createTempCopy(text: String? = null): KtFile { fun PsiElement.getAllExtractionContainers(strict: Boolean = true): List { val containers = ArrayList() - var objectFound = false + var objectOrNonInnerNestedClassFound = false val parents = if (strict) parents else parentsWithSelf for (element in parents) { val isValidContainer = when (element) { is KtFile -> true - is KtClassBody -> !objectFound || element.parent is KtObjectDeclaration - is KtBlockExpression -> !objectFound + is KtClassBody -> !objectOrNonInnerNestedClassFound || element.parent is KtObjectDeclaration + is KtBlockExpression -> !objectOrNonInnerNestedClassFound else -> false } if (!isValidContainer) continue containers.add(element as KtElement) - ((element as? KtClassBody)?.parent as? KtObjectDeclaration)?.let { objectFound = true } + if (!objectOrNonInnerNestedClassFound) { + val bodyParent = (element as? KtClassBody)?.parent + objectOrNonInnerNestedClassFound = + (bodyParent is KtObjectDeclaration && !bodyParent.isObjectLiteral()) + || (bodyParent is KtClass && !bodyParent.isInner()) + } } return containers