Introduce Variable: Allow extraction to class body/file

This commit is contained in:
Alexey Sedunov
2016-12-07 15:21:48 +03:00
parent abca774422
commit 9cadfd01ae
9 changed files with 26 additions and 18 deletions
@@ -163,7 +163,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
}
var anchor = calculateAnchor(commonParent, commonContainer, allReplaces) ?: return
val needBraces = commonContainer !is KtBlockExpression
val needBraces = commonContainer !is KtBlockExpression && commonContainer !is KtClassBody && commonContainer !is KtFile
if (!needBraces) {
property = commonContainer.addBefore(property, anchor) as KtDeclaration
commonContainer.addBefore(psiFactory.createNewLine(), anchor)
@@ -355,6 +355,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
is KtBlockExpression -> true
is KtWhenEntry -> place == parent.expression
is KtDeclarationWithBody -> parent.bodyExpression == place
is KtClassBody -> true
is KtFile -> true
else -> false
}
}?.second as? KtElement
@@ -375,7 +377,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
for ((place, parent) in parentsWithSelf.zip(parents)) {
when {
parent is KtContainerNode && place !is KtBlockExpression && !parent.isBadContainerNode(place) -> result = parent
parent is KtClassBody || parent is KtFile -> return result
parent is KtClassBody || parent is KtFile -> return if (result == null) parent as KtElement else result
parent is KtBlockExpression -> result = parent
parent is KtWhenEntry && place !is KtBlockExpression -> result = parent
parent is KtDeclarationWithBody && parent.bodyExpression == place && place !is KtBlockExpression -> result = parent
@@ -480,7 +482,9 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
PsiTreeUtil.getNonStrictParentOfType(physicalExpression,
KtTypeReference::class.java,
KtConstructorCalleeExpression::class.java,
KtSuperExpression::class.java)?.let {
KtSuperExpression::class.java,
KtConstructorDelegationReferenceExpression::class.java,
KtAnnotationEntry::class.java)?.let {
return showErrorHint(project, editor, KotlinRefactoringBundle.message("cannot.refactor.no.container"))
}
@@ -1 +0,0 @@
Cannot refactor in this place
@@ -1 +0,0 @@
Cannot refactor in this place
@@ -0,0 +1,4 @@
class X {
val i = 10
val s = i.toString()
}
@@ -0,0 +1,2 @@
val i = 10
val s = i.toString()
@@ -1 +1 @@
Cannot refactor in this place
Cannot perform refactoring without an expression
@@ -56,18 +56,6 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
doIntroduceVariableTest(fileName);
}
@TestMetadata("CantIntroduceToClass.kt")
public void testCantIntroduceToClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/CantIntroduceToClass.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("CantIntroduceToFile.kt")
public void testCantIntroduceToFile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/CantIntroduceToFile.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("commentSkipping.kt")
public void testCommentSkipping() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/commentSkipping.kt");
@@ -224,6 +212,18 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
doIntroduceVariableTest(fileName);
}
@TestMetadata("introduceToClass.kt")
public void testIntroduceToClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/introduceToClass.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("introduceToFile.kt")
public void testIntroduceToFile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/introduceToFile.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("It.kt")
public void testIt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/It.kt");