Use lazy analysis in intention actions
This commit is contained in:
@@ -81,7 +81,6 @@ public class ReconstructTypeInCastOrIsAction extends PsiElementBaseIntentionActi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static JetType getReconstructedType(JetTypeReference typeRef) {
|
private static JetType getReconstructedType(JetTypeReference typeRef) {
|
||||||
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) typeRef.getContainingFile()).getBindingContext();
|
return AnalyzerFacadeWithCache.getContextForElement(typeRef).get(BindingContext.TYPE, typeRef);
|
||||||
return bindingContext.get(BindingContext.TYPE, typeRef);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -127,13 +127,13 @@ public class BranchedUnfoldingUtils {
|
|||||||
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
|
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) {
|
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull Editor editor) {
|
||||||
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file);
|
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property);
|
||||||
unfoldAssignmentToIf(assignment, editor);
|
unfoldAssignmentToIf(assignment, editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) {
|
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull Editor editor) {
|
||||||
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file);
|
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property);
|
||||||
unfoldAssignmentToWhen(assignment, editor);
|
unfoldAssignmentToWhen(assignment, editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+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, editor);
|
BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, 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, editor);
|
BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, editor);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
RETURN_TO_WHEN("unfold.return.to.when") {
|
RETURN_TO_WHEN("unfold.return.to.when") {
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
|||||||
import org.jetbrains.jet.plugin.util.JetPsiMatcher;
|
import org.jetbrains.jet.plugin.util.JetPsiMatcher;
|
||||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class DeclarationUtils {
|
public class DeclarationUtils {
|
||||||
private DeclarationUtils() {
|
private DeclarationUtils() {
|
||||||
}
|
}
|
||||||
@@ -84,20 +82,18 @@ public class DeclarationUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property, @NotNull JetFile file) {
|
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property) {
|
||||||
if (property.getTypeRef() != null) return null;
|
if (property.getTypeRef() != null) return null;
|
||||||
|
|
||||||
JetType type = AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext().get(
|
JetType type = AnalyzerFacadeWithCache.getContextForElement(property).get(
|
||||||
BindingContext.EXPRESSION_TYPE,
|
BindingContext.EXPRESSION_TYPE, property.getInitializer()
|
||||||
property.getInitializer()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return type == null || type.isError() ? null : type;
|
return type == null || type.isError() ? null : type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns assignment which replaces initializer
|
// returns assignment which replaces initializer
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JetBinaryExpression splitPropertyDeclaration(@NotNull JetProperty property, @NotNull JetFile file) {
|
public static JetBinaryExpression splitPropertyDeclaration(@NotNull JetProperty property) {
|
||||||
Project project = property.getProject();
|
Project project = property.getProject();
|
||||||
|
|
||||||
PsiElement parent = property.getParent();
|
PsiElement parent = property.getParent();
|
||||||
@@ -116,7 +112,7 @@ public class DeclarationUtils {
|
|||||||
parent.addAfter(JetPsiFactory.createNewLine(project), property);
|
parent.addAfter(JetPsiFactory.createNewLine(project), property);
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
JetType inferredType = getPropertyTypeIfNeeded(property, file);
|
JetType inferredType = getPropertyTypeIfNeeded(property);
|
||||||
|
|
||||||
String typeStr = inferredType != null
|
String typeStr = inferredType != null
|
||||||
? DescriptorRenderer.TEXT.renderType(inferredType)
|
? DescriptorRenderer.TEXT.renderType(inferredType)
|
||||||
|
|||||||
+1
-4
@@ -29,9 +29,6 @@ public class SplitPropertyDeclarationIntention : JetSelfTargetingIntention<JetPr
|
|||||||
override fun isApplicableTo(element: JetProperty): Boolean = DeclarationUtils.checkSplitProperty(element)
|
override fun isApplicableTo(element: JetProperty): Boolean = DeclarationUtils.checkSplitProperty(element)
|
||||||
|
|
||||||
override fun applyTo(element: JetProperty, editor: Editor) {
|
override fun applyTo(element: JetProperty, editor: Editor) {
|
||||||
val file = element.getContainingFile()
|
DeclarationUtils.splitPropertyDeclaration(element)
|
||||||
if (file is JetFile) {
|
|
||||||
DeclarationUtils.splitPropertyDeclaration(element, file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user