Introduce Variable: Forbid extraction from class initializer (aside of its
body) #KT-8329 Fixed
This commit is contained in:
+4
-6
@@ -330,8 +330,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
|||||||
JetProperty property = psiFactory.createProperty(variableText);
|
JetProperty property = psiFactory.createProperty(variableText);
|
||||||
PsiElement anchor = calculateAnchor(commonParent, commonContainer, allReplaces);
|
PsiElement anchor = calculateAnchor(commonParent, commonContainer, allReplaces);
|
||||||
if (anchor == null) return;
|
if (anchor == null) return;
|
||||||
boolean needBraces = !(commonContainer instanceof JetBlockExpression ||
|
boolean needBraces = !(commonContainer instanceof JetBlockExpression);
|
||||||
commonContainer instanceof JetClassInitializer);
|
|
||||||
if (!needBraces) {
|
if (!needBraces) {
|
||||||
property = (JetProperty)commonContainer.addBefore(property, anchor);
|
property = (JetProperty)commonContainer.addBefore(property, anchor);
|
||||||
commonContainer.addBefore(psiFactory.createNewLine(), anchor);
|
commonContainer.addBefore(psiFactory.createNewLine(), anchor);
|
||||||
@@ -548,7 +547,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static PsiElement getContainer(PsiElement place) {
|
private static PsiElement getContainer(PsiElement place) {
|
||||||
if (place instanceof JetBlockExpression || place instanceof JetClassInitializer) {
|
if (place instanceof JetBlockExpression) {
|
||||||
return place;
|
return place;
|
||||||
}
|
}
|
||||||
while (place != null) {
|
while (place != null) {
|
||||||
@@ -559,8 +558,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (parent instanceof JetBlockExpression
|
if (parent instanceof JetBlockExpression
|
||||||
|| (parent instanceof JetWhenEntry && place == ((JetWhenEntry) parent).getExpression())
|
|| (parent instanceof JetWhenEntry && place == ((JetWhenEntry) parent).getExpression())) {
|
||||||
|| parent instanceof JetClassInitializer) {
|
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
if (parent instanceof JetDeclarationWithBody && ((JetDeclarationWithBody) parent).getBodyExpression() == place) {
|
if (parent instanceof JetDeclarationWithBody && ((JetDeclarationWithBody) parent).getBodyExpression() == place) {
|
||||||
@@ -596,7 +594,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
|||||||
result = parent;
|
result = parent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (parent instanceof JetClassBody || parent instanceof JetFile || parent instanceof JetClassInitializer) {
|
else if (parent instanceof JetClassBody || parent instanceof JetFile) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else if (parent instanceof JetBlockExpression) {
|
else if (parent instanceof JetBlockExpression) {
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
class C {
|
||||||
|
deprecated(<selection>""</selection>)
|
||||||
|
init {}
|
||||||
|
}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
Cannot refactor in this place
|
||||||
+6
@@ -145,6 +145,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doIntroduceVariableTest(fileName);
|
doIntroduceVariableTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("InsideOfInitializerAnnotation.kt")
|
||||||
|
public void testInsideOfInitializerAnnotation() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/InsideOfInitializerAnnotation.kt");
|
||||||
|
doIntroduceVariableTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("IntroduceAndCreateBlock.kt")
|
@TestMetadata("IntroduceAndCreateBlock.kt")
|
||||||
public void testIntroduceAndCreateBlock() throws Exception {
|
public void testIntroduceAndCreateBlock() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/IntroduceAndCreateBlock.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/IntroduceAndCreateBlock.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user