Add folding/unfolding operations for branched control structures
This commit is contained in:
@@ -249,13 +249,13 @@ public class JetPsiFactory {
|
||||
return createExpression(project, "$" + fieldName);
|
||||
}
|
||||
|
||||
public static JetBinaryExpression createAssignment(Project project, @NotNull String lhs, @NotNull String rhs) {
|
||||
return (JetBinaryExpression) createExpression(project, lhs + " = " + rhs);
|
||||
public static JetBinaryExpression createBinaryExpression(Project project, @NotNull String lhs, @NotNull String op, @NotNull String rhs) {
|
||||
return (JetBinaryExpression) createExpression(project, lhs + " " + op + " " + rhs);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public static JetBinaryExpression createAssignment(Project project, @NotNull JetExpression lhs, @NotNull JetExpression rhs) {
|
||||
JetBinaryExpression assignment = createAssignment(project, "_", "_");
|
||||
public static JetBinaryExpression createBinaryExpression(Project project, @NotNull JetExpression lhs, @NotNull String op, @NotNull JetExpression rhs) {
|
||||
JetBinaryExpression assignment = createBinaryExpression(project, "_", op, "_");
|
||||
|
||||
assert assignment.getRight() != null;
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.jet.lang.psi;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -623,8 +624,62 @@ public class JetPsiUtil {
|
||||
return innerPrecedence < parentPrecedence;
|
||||
}
|
||||
|
||||
public static boolean isAssignment(@NotNull JetElement element) {
|
||||
public static boolean isAssignment(@NotNull PsiElement element) {
|
||||
return element instanceof JetBinaryExpression &&
|
||||
JetTokens.ALL_ASSIGNMENTS.contains(((JetBinaryExpression) element).getOperationToken());
|
||||
}
|
||||
|
||||
public static boolean isBranchedExpression(@Nullable PsiElement element) {
|
||||
return element instanceof JetIfExpression || element instanceof JetWhenExpression;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static JetElement getOutermostLastBlockElement(
|
||||
@Nullable JetElement element,
|
||||
@Nullable Predicate<JetElement> checkElement) {
|
||||
if (element == null) return null;
|
||||
|
||||
if (!(element instanceof JetBlockExpression)) return checkElement == null || checkElement.apply(element) ? element : null;
|
||||
|
||||
JetBlockExpression block = (JetBlockExpression)element;
|
||||
int n = block.getStatements().size();
|
||||
|
||||
if (n == 0) return null;
|
||||
|
||||
JetElement lastElement = block.getStatements().get(n - 1);
|
||||
return checkElement == null || checkElement.apply(lastElement) ? lastElement : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiElement getParentByTypeAndPredicate(
|
||||
@Nullable PsiElement element, @NotNull Class<? extends PsiElement> aClass, @NotNull Predicate<PsiElement> predicate, boolean strict) {
|
||||
if (element == null) return null;
|
||||
if (strict) {
|
||||
element = element.getParent();
|
||||
}
|
||||
|
||||
while (element != null) {
|
||||
//noinspection unchecked
|
||||
if (aClass.isInstance(element) && predicate.apply(element)) {
|
||||
//noinspection unchecked
|
||||
return element;
|
||||
}
|
||||
if (element instanceof PsiFile) return null;
|
||||
element = element.getParent();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean checkVariableDeclarationInBlock(@NotNull JetBlockExpression block, @NotNull String varName) {
|
||||
for (JetElement element : block.getStatements()) {
|
||||
if (element instanceof JetVariableDeclaration) {
|
||||
if (((JetVariableDeclaration) element).getNameAsSafeName().getName().equals(varName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user