Simplify unfolding of properties, fix caret position
This commit is contained in:
+7
-90
@@ -22,13 +22,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations.DeclarationUtils;
|
||||||
import org.jetbrains.jet.lang.types.JetType;
|
|
||||||
import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening;
|
|
||||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
|
||||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class BranchedUnfoldingUtils {
|
public class BranchedUnfoldingUtils {
|
||||||
private BranchedUnfoldingUtils() {
|
private BranchedUnfoldingUtils() {
|
||||||
@@ -133,91 +127,14 @@ public class BranchedUnfoldingUtils {
|
|||||||
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
|
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property, @NotNull JetFile file) {
|
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) {
|
||||||
if (property.getTypeRef() != null) return null;
|
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file);
|
||||||
return AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext().get(BindingContext.EXPRESSION_TYPE, property.getInitializer());
|
unfoldAssignmentToIf(assignment, editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected interface PropertyUnfolder<T extends JetExpression> {
|
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) {
|
||||||
void processInitializer(@NotNull T newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project);
|
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file);
|
||||||
}
|
unfoldAssignmentToWhen(assignment, editor);
|
||||||
|
|
||||||
protected static final PropertyUnfolder<JetIfExpression> IF_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder<JetIfExpression>() {
|
|
||||||
@Override
|
|
||||||
public void processInitializer(
|
|
||||||
@NotNull JetIfExpression newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project
|
|
||||||
) {
|
|
||||||
JetExpression thenExpr = getOutermostLastBlockElement(newInitializer.getThen());
|
|
||||||
JetExpression elseExpr = getOutermostLastBlockElement(newInitializer.getElse());
|
|
||||||
|
|
||||||
assertNotNull(thenExpr);
|
|
||||||
assertNotNull(elseExpr);
|
|
||||||
|
|
||||||
thenExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", thenExpr));
|
|
||||||
elseExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", elseExpr));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
protected static final PropertyUnfolder<JetWhenExpression> WHEN_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder<JetWhenExpression>() {
|
|
||||||
@Override
|
|
||||||
public void processInitializer(
|
|
||||||
@NotNull JetWhenExpression newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project
|
|
||||||
) {
|
|
||||||
for (JetWhenEntry entry : newInitializer.getEntries()) {
|
|
||||||
JetExpression currExpr = getOutermostLastBlockElement(entry.getExpression());
|
|
||||||
|
|
||||||
assertNotNull(currExpr);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
currExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", currExpr));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private static <T extends JetExpression> void unfoldProperty(
|
|
||||||
@NotNull JetProperty property, @NotNull JetFile file, PropertyUnfolder<T> unfolder
|
|
||||||
) {
|
|
||||||
Project project = property.getProject();
|
|
||||||
|
|
||||||
PsiElement parent = property.getParent();
|
|
||||||
assertNotNull(parent);
|
|
||||||
|
|
||||||
//noinspection unchecked
|
|
||||||
T initializer = (T) property.getInitializer();
|
|
||||||
assertNotNull(initializer);
|
|
||||||
|
|
||||||
JetSimpleNameExpression propertyName = JetPsiFactory.createSimpleName(project, property.getName());
|
|
||||||
|
|
||||||
//noinspection ConstantConditions, unchecked
|
|
||||||
T newInitializer = (T) initializer.copy();
|
|
||||||
|
|
||||||
unfolder.processInitializer(newInitializer, propertyName, project);
|
|
||||||
|
|
||||||
parent.addAfter(newInitializer, property);
|
|
||||||
parent.addAfter(JetPsiFactory.createNewLine(project), property);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
JetType inferredType = getPropertyTypeIfNeeded(property, file);
|
|
||||||
|
|
||||||
String typeStr = inferredType != null
|
|
||||||
? DescriptorRenderer.TEXT.renderType(inferredType)
|
|
||||||
: JetPsiUtil.getNullableText(property.getTypeRef());
|
|
||||||
|
|
||||||
property = (JetProperty) property.replace(
|
|
||||||
JetPsiFactory.createProperty(project, property.getName(), typeStr, property.isVar())
|
|
||||||
);
|
|
||||||
|
|
||||||
if (inferredType != null) {
|
|
||||||
ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(property.getTypeRef()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull JetFile file) {
|
|
||||||
unfoldProperty(property, file, IF_EXPRESSION_PROPERTY_UNFOLDER);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull JetFile file) {
|
|
||||||
unfoldProperty(property, file, WHEN_EXPRESSION_PROPERTY_UNFOLDER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
|
public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
|
||||||
|
|||||||
+2
-2
@@ -35,7 +35,7 @@ public enum UnfoldableKind implements Transformer {
|
|||||||
PROPERTY_TO_IF("unfold.property.to.if") {
|
PROPERTY_TO_IF("unfold.property.to.if") {
|
||||||
@Override
|
@Override
|
||||||
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
||||||
BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, file);
|
BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, file, editor);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
RETURN_TO_IF("unfold.return.to.if") {
|
RETURN_TO_IF("unfold.return.to.if") {
|
||||||
@@ -53,7 +53,7 @@ public enum UnfoldableKind implements Transformer {
|
|||||||
PROPERTY_TO_WHEN("unfold.property.to.when") {
|
PROPERTY_TO_WHEN("unfold.property.to.when") {
|
||||||
@Override
|
@Override
|
||||||
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
||||||
BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, file);
|
BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, file, editor);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
RETURN_TO_WHEN("unfold.return.to.when") {
|
RETURN_TO_WHEN("unfold.return.to.when") {
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String? {
|
fun test(n: Int): String? {
|
||||||
val res:<caret> String?
|
val res: String?
|
||||||
if (n == 1) {
|
<caret>if (n == 1) {
|
||||||
res = if (3 > 2) {
|
res = if (3 > 2) {
|
||||||
println("***")
|
println("***")
|
||||||
"one"
|
"one"
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String? {
|
fun test(n: Int): String? {
|
||||||
var res:<caret> String?
|
var res: String?
|
||||||
if (n == 1) {
|
<caret>if (n == 1) {
|
||||||
res = if (3 > 2) {
|
res = if (3 > 2) {
|
||||||
println("***")
|
println("***")
|
||||||
"one"
|
"one"
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
val <caret>res: String
|
val res: String
|
||||||
if (n == 1) res = "one" else res = "two"
|
<caret>if (n == 1) res = "one" else res = "two"
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
var <caret>res: String
|
var res: String
|
||||||
if (n == 1) res = "one" else res = "two"
|
<caret>if (n == 1) res = "one" else res = "two"
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
val res<caret>: String
|
val res: String
|
||||||
if (n == 1) {
|
<caret>if (n == 1) {
|
||||||
println("***")
|
println("***")
|
||||||
res = "one"
|
res = "one"
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
var res<caret>: String
|
var res: String
|
||||||
if (n == 1) {
|
<caret>if (n == 1) {
|
||||||
println("***")
|
println("***")
|
||||||
res = "one"
|
res = "one"
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
val <caret>res: jet.String
|
val res: jet.String
|
||||||
if (n == 1) res = "one" else res = "two"
|
<caret>if (n == 1) res = "one" else res = "two"
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String? {
|
fun test(n: Int): String? {
|
||||||
val res<caret>: String?
|
val res: String?
|
||||||
when(n) {
|
<caret>when(n) {
|
||||||
1 -> res = "one"
|
1 -> res = "one"
|
||||||
2 -> res = "two"
|
2 -> res = "two"
|
||||||
else -> res = null
|
else -> res = null
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String? {
|
fun test(n: Int): String? {
|
||||||
var res<caret>: String?
|
var res: String?
|
||||||
when(n) {
|
<caret>when(n) {
|
||||||
1 -> res = "one"
|
1 -> res = "one"
|
||||||
2 -> res = "two"
|
2 -> res = "two"
|
||||||
else -> res = null
|
else -> res = null
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
val res<caret>: String
|
val res: String
|
||||||
when (n) {
|
<caret>when (n) {
|
||||||
1 -> {
|
1 -> {
|
||||||
println("***")
|
println("***")
|
||||||
res = "one"
|
res = "one"
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
var res<caret>: String
|
var res: String
|
||||||
when (n) {
|
<caret>when (n) {
|
||||||
1 -> {
|
1 -> {
|
||||||
println("***")
|
println("***")
|
||||||
res = "one"
|
res = "one"
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun test(n: Int): String? {
|
fun test(n: Int): String? {
|
||||||
val res<caret>: jet.String?
|
val res: jet.String?
|
||||||
when(n) {
|
<caret>when(n) {
|
||||||
1 -> res = "one"
|
1 -> res = "one"
|
||||||
2 -> res = "two"
|
2 -> res = "two"
|
||||||
else -> res = null
|
else -> res = null
|
||||||
|
|||||||
Reference in New Issue
Block a user