This commit is contained in:
Alexey Kudravtsev
2010-03-25 13:48:48 +03:00
parent 70e0261c2a
commit 781db7ad00
3 changed files with 92 additions and 92 deletions
@@ -17,15 +17,16 @@ package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.tree.IElementType;
public class ReplaceExpressionUtil implements Constants {
public class ReplaceExpressionUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.java.ReplaceExpressionUtil");
public static boolean isNeedParenthesis(ASTNode oldExpr, ASTNode newExpr) {
@@ -35,51 +36,54 @@ public class ReplaceExpressionUtil implements Constants {
int parentPriority = getExpressionPriority(oldParent);
if (priority > parentPriority) return false;
IElementType i = oldParent.getElementType();
if (i == ASSIGNMENT_EXPRESSION) {
if (priority < parentPriority) return true;
return ((CompositeElement)oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND ? true : false;
if (i == JavaElementType.ASSIGNMENT_EXPRESSION) {
return priority < parentPriority || ((CompositeElement)oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND;
}
else if (i == CONDITIONAL_EXPRESSION) {
else if (i == JavaElementType.CONDITIONAL_EXPRESSION) {
int role = ((CompositeElement)oldParent).getChildRole(oldExpr);
if (role == ChildRole.THEN_EXPRESSION) return false;
if (priority < parentPriority) return true;
return role == ChildRole.ELSE_EXPRESSION ? false : true;
return priority < parentPriority || role != ChildRole.ELSE_EXPRESSION;
}
else if (i == BINARY_EXPRESSION) {
else if (i == JavaElementType.BINARY_EXPRESSION) {
if (priority < parentPriority) return true;
final IElementType opType = ((PsiBinaryExpression)SourceTreeToPsiMap.treeElementToPsi(oldParent)).getOperationSign().getTokenType();
return ((CompositeElement)oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND ? false : opType != PLUS && opType != ASTERISK && opType != ANDAND;
return ((CompositeElement)oldParent).getChildRole(oldExpr) != ChildRole.LOPERAND &&
opType != JavaTokenType.PLUS &&
opType != JavaTokenType.ASTERISK &&
opType != JavaTokenType.ANDAND;
}
else if (i == INSTANCE_OF_EXPRESSION) {
else if (i == JavaElementType.INSTANCE_OF_EXPRESSION) {
return priority < parentPriority;
}
else if (i == PREFIX_EXPRESSION || i == TYPE_CAST_EXPRESSION) {
else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) {
return priority < parentPriority;
}
else if (i == POSTFIX_EXPRESSION) {
else if (i == JavaElementType.POSTFIX_EXPRESSION) {
return priority <= parentPriority;
}
else if (i == REFERENCE_EXPRESSION) {
else if (i == JavaElementType.REFERENCE_EXPRESSION) {
return priority < parentPriority;
}
else if (i == METHOD_CALL_EXPRESSION) {
else if (i == JavaElementType.METHOD_CALL_EXPRESSION) {
return false;
}
else if (i == NEW_EXPRESSION) {
else if (i == JavaElementType.NEW_EXPRESSION) {
return false;
}
else if (i == ARRAY_ACCESS_EXPRESSION) {
else if (i == JavaElementType.ARRAY_ACCESS_EXPRESSION) {
int role = ((CompositeElement)oldParent).getChildRole(oldExpr);
if (role == ChildRole.ARRAY_DIMENSION) return false;
return priority < parentPriority;
return role != ChildRole.ARRAY_DIMENSION && priority < parentPriority;
}
else if (i == ARRAY_INITIALIZER_EXPRESSION) {
else if (i == JavaElementType.ARRAY_INITIALIZER_EXPRESSION) {
return false;
}
else if (i == PARENTH_EXPRESSION) {
else if (i == JavaElementType.PARENTH_EXPRESSION) {
return false;
}
else if (i == LITERAL_EXPRESSION || i == THIS_EXPRESSION || i == SUPER_EXPRESSION || i == CLASS_OBJECT_ACCESS_EXPRESSION) {
else if (i == JavaElementType.LITERAL_EXPRESSION ||
i == JavaElementType.THIS_EXPRESSION ||
i == JavaElementType.SUPER_EXPRESSION ||
i == JavaElementType.CLASS_OBJECT_ACCESS_EXPRESSION) {
return false;
}
@@ -89,68 +93,67 @@ public class ReplaceExpressionUtil implements Constants {
private static int getExpressionPriority(ASTNode expr) {
IElementType i = expr.getElementType();
if (i == ASSIGNMENT_EXPRESSION) {
if (i == JavaElementType.ASSIGNMENT_EXPRESSION) {
return 0;
}
else if (i == CONDITIONAL_EXPRESSION) {
else if (i == JavaElementType.CONDITIONAL_EXPRESSION) {
return 1;
}
else if (i == BINARY_EXPRESSION) {
{
IElementType opType = ((PsiBinaryExpression)SourceTreeToPsiMap.treeElementToPsi(expr)).getOperationSign().getTokenType();
if (opType == OROR) {
return 2;
}
else if (opType == ANDAND) {
return 3;
}
else if (opType == OR) {
return 4;
}
else if (opType == XOR) {
return 5;
}
else if (opType == AND) {
return 6;
}
else if (opType == EQEQ || opType == NE) {
return 7;
}
else if (opType == LT || opType == GT || opType == LE || opType == GE) {
return 8;
}
else if (opType == LTLT || opType == GTGT || opType == GTGTGT) {
return 9;
}
else if (opType == PLUS || opType == MINUS) {
return 10;
}
else if (opType == ASTERISK || opType == DIV || opType == PERC) {
return 11;
}
else if (i == JavaElementType.BINARY_EXPRESSION) {
IElementType opType = ((PsiBinaryExpression)SourceTreeToPsiMap.treeElementToPsi(expr)).getOperationSign().getTokenType();
if (opType == JavaTokenType.OROR) {
return 2;
}
else if (opType == JavaTokenType.ANDAND) {
return 3;
}
else if (opType == JavaTokenType.OR) {
return 4;
}
else if (opType == JavaTokenType.XOR) {
return 5;
}
else if (opType == JavaTokenType.AND) {
return 6;
}
else if (opType == JavaTokenType.EQEQ || opType == JavaTokenType.NE) {
return 7;
}
else if (opType == JavaTokenType.LT || opType == JavaTokenType.GT || opType == JavaTokenType.LE || opType == JavaTokenType.GE) {
return 8;
}
else if (opType == JavaTokenType.LTLT || opType == JavaTokenType.GTGT || opType == JavaTokenType.GTGTGT) {
return 9;
}
else if (opType == JavaTokenType.PLUS || opType == JavaTokenType.MINUS) {
return 10;
}
else if (opType == JavaTokenType.ASTERISK || opType == JavaTokenType.DIV || opType == JavaTokenType.PERC) {
return 11;
}
return 8;
}
else if (i == INSTANCE_OF_EXPRESSION) {
else if (i == JavaElementType.INSTANCE_OF_EXPRESSION) {
return 8;
}
else if (i == PREFIX_EXPRESSION || i == TYPE_CAST_EXPRESSION) {
else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) {
return 12;
}
else if (i == POSTFIX_EXPRESSION) {
else if (i == JavaElementType.POSTFIX_EXPRESSION) {
return 13;
}
else if (i == LITERAL_EXPRESSION || i == REFERENCE_EXPRESSION || i == THIS_EXPRESSION || i == SUPER_EXPRESSION || i ==
PARENTH_EXPRESSION ||
i == METHOD_CALL_EXPRESSION ||
i == CLASS_OBJECT_ACCESS_EXPRESSION ||
i == NEW_EXPRESSION ||
i == ARRAY_ACCESS_EXPRESSION ||
i == ARRAY_INITIALIZER_EXPRESSION ||
i == JAVA_CODE_REFERENCE ||
i == EMPTY_EXPRESSION) {
else if (i == JavaElementType.LITERAL_EXPRESSION ||
i == JavaElementType.REFERENCE_EXPRESSION ||
i == JavaElementType.THIS_EXPRESSION ||
i == JavaElementType.SUPER_EXPRESSION ||
i == JavaElementType.PARENTH_EXPRESSION ||
i == JavaElementType.METHOD_CALL_EXPRESSION ||
i == JavaElementType.CLASS_OBJECT_ACCESS_EXPRESSION ||
i == JavaElementType.NEW_EXPRESSION ||
i == JavaElementType.ARRAY_ACCESS_EXPRESSION ||
i == JavaElementType.ARRAY_INITIALIZER_EXPRESSION ||
i == JavaElementType.JAVA_CODE_REFERENCE ||
i == JavaElementType.EMPTY_EXPRESSION) {
return 14;
}
else {
@@ -72,7 +72,7 @@ import java.util.List;
public abstract class IntroduceVariableBase extends IntroduceHandlerBase implements RefactoringActionHandler {
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceVariable.IntroduceVariableBase");
private static final @NonNls String PREFER_STATEMENTS_OPTION = "introduce.variable.prefer.statements";
@NonNls private static final String PREFER_STATEMENTS_OPTION = "introduce.variable.prefer.statements";
protected static String REFACTORING_NAME = RefactoringBundle.message("introduce.variable.title");
@@ -48,17 +48,17 @@ public class DebugUtil {
return false;
}
public static String psiTreeToString(PsiElement element, boolean skipWhitespaces) {
public static String psiTreeToString(@NotNull PsiElement element, boolean skipWhitespaces) {
return treeToString(SourceTreeToPsiMap.psiElementToTree(element), skipWhitespaces);
}
public static String treeToString(ASTNode root, boolean skipWhitespaces) {
public static String treeToString(@NotNull ASTNode root, boolean skipWhitespaces) {
StringBuilder buffer = new StringBuilder();
treeToBuffer(buffer, root, 0, skipWhitespaces, false, false);
return buffer.toString();
}
public static String treeToString(ASTNode root, boolean skipWhitespaces, boolean showRanges) {
public static String treeToString(@NotNull ASTNode root, boolean skipWhitespaces, boolean showRanges) {
StringBuilder buffer = new StringBuilder();
treeToBuffer(buffer, root, 0, skipWhitespaces, showRanges, false);
return buffer.toString();
@@ -77,8 +77,8 @@ public class DebugUtil {
return buffer.toString();
}
public static void treeToBuffer(StringBuilder buffer,
ASTNode root,
public static void treeToBuffer(@NotNull StringBuilder buffer,
@NotNull ASTNode root,
int indent,
boolean skipWhiteSpaces,
boolean showRanges,
@@ -192,31 +192,28 @@ public class DebugUtil {
}
public static void checkTreeStructure(ASTNode anyElement) {
public static void checkTreeStructure(@NotNull ASTNode anyElement) {
ASTNode root = anyElement;
while (root.getTreeParent() != null) {
root = root.getTreeParent();
}
if (root instanceof CompositeElement) {
synchronized (PsiLock.LOCK) {
checkSubtree(root);
checkSubtree((CompositeElement)root);
}
}
}
private static void checkSubtree(ASTNode root) {
if (!(root instanceof CompositeElement)) return;
CompositeElement node = (CompositeElement)root;
if (node.rawFirstChild() == null) {
if (node.rawLastChild() != null) {
throw new IncorrectTreeStructureException(node, "firstChild == null, but lastChild != null");
private static void checkSubtree(CompositeElement root) {
if (root.rawFirstChild() == null) {
if (root.rawLastChild() != null) {
throw new IncorrectTreeStructureException(root, "firstChild == null, but lastChild != null");
}
}
else {
for (ASTNode child = root.getFirstChildNode(); child != null; child = child.getTreeNext()) {
if (child instanceof CompositeElement) {
checkSubtree(child);
checkSubtree((CompositeElement)child);
}
if (child.getTreeParent() != root) {
throw new IncorrectTreeStructureException(child, "child has wrong parent value");
@@ -243,7 +240,7 @@ public class DebugUtil {
}
}
public static void checkParentChildConsistent(ASTNode element) {
public static void checkParentChildConsistent(@NotNull ASTNode element) {
ASTNode treeParent = element.getTreeParent();
if (treeParent == null) return;
ASTNode[] elements = treeParent.getChildren(null);
@@ -253,13 +250,13 @@ public class DebugUtil {
//LOG.debug("checked consistence: "+System.identityHashCode(element));
}
public static void checkSameCharTabs(ASTNode element1, ASTNode element2) {
public static void checkSameCharTabs(@NotNull ASTNode element1, @NotNull ASTNode element2) {
final CharTable fromCharTab = SharedImplUtil.findCharTableByTree(element1);
final CharTable toCharTab = SharedImplUtil.findCharTableByTree(element2);
LOG.assertTrue(fromCharTab == toCharTab);
}
public static String psiToString(final PsiElement element, final boolean skipWhitespaces) {
public static String psiToString(@NotNull PsiElement element, final boolean skipWhitespaces) {
return psiToString(element, skipWhitespaces, false);
}
@@ -276,8 +273,8 @@ public class DebugUtil {
return result.toString();
}
public static void psiToBuffer(StringBuilder buffer,
PsiElement root,
public static void psiToBuffer(@NotNull StringBuilder buffer,
@NotNull PsiElement root,
int indent,
boolean skipWhiteSpaces,
boolean showRanges,