(no message)

This commit is contained in:
Alexey Kudravtsev
2005-03-21 18:59:11 +03:00
parent 4d95b77e92
commit a07baea8ef
2 changed files with 77 additions and 7 deletions
@@ -66,7 +66,7 @@ public class HighlightUtil {
public static final String EXPRESSION_EXPECTED = "Expression expected";
private static final Set<String> ourConstructorNotAllowedModifiers;
public static final String INCONVERTIBLE_TYPE_CAST = "Inconvertible types; cannot cast ''{0}'' to ''{1}''";
public static final String BINARY_OPERATOR_NOT_APPLICABLE = "Operator ''{0}'' cannot be applied to ''{1}'',''{2}''";
private static final String BINARY_OPERATOR_NOT_APPLICABLE = "Operator ''{0}'' cannot be applied to ''{1}'',''{2}''";
private static final Key<String> HAS_OVERFLOW_IN_CHILD = Key.create("HAS_OVERFLOW_IN_CHILD");
@@ -942,9 +942,7 @@ public class HighlightUtil {
formatType(lOperand.getType()),
formatType(rOperand.getType())
});
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR,
expression,
message);
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, expression, message);
}
return null;
}
@@ -961,9 +959,12 @@ public class HighlightUtil {
token.getText(),
formatType(type)
});
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR,
token.getParent(),
message);
final PsiElement parentExpr = token.getParent();
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, parentExpr, message);
if (parentExpr instanceof PsiPrefixExpression && token.getTokenType() == JavaTokenType.EXCL) {
QuickFixAction.registerQuickFixAction(highlightInfo, new NegationBroadScopeFix((PsiPrefixExpression)parentExpr));
}
return highlightInfo;
}
return null;
}
@@ -0,0 +1,69 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
/**
* @author cdr
* if (!a == b) ... => if (!(a == b)) ...
*/
public class NegationBroadScopeFix implements IntentionAction {
private final PsiPrefixExpression myPrefixExpression;
public NegationBroadScopeFix(PsiPrefixExpression prefixExpression) {
myPrefixExpression = prefixExpression;
}
public String getText() {
String text = "Change to '!(";
text += myPrefixExpression.getOperand().getText();
text += " ";
final PsiElement parent = myPrefixExpression.getParent();
final String operation = parent instanceof PsiInstanceOfExpression ? "instanceof" : ((PsiBinaryExpression)parent).getOperationSign().getText();
text += operation + " ";
final String rop = parent instanceof PsiInstanceOfExpression ? ((PsiInstanceOfExpression)parent).getCheckType().getText()
: ((PsiBinaryExpression)parent).getROperand().getText();
text += rop;
text += ")'";
return text;
}
public String getFamilyName() {
return null;
}
public boolean isAvailable(Project project, Editor editor, PsiFile file) {
if (myPrefixExpression == null || !myPrefixExpression.isValid()) return false;
final PsiElement parent = myPrefixExpression.getParent();
if (parent instanceof PsiInstanceOfExpression && ((PsiInstanceOfExpression)parent).getOperand() == myPrefixExpression) {
return true;
}
if (!(parent instanceof PsiBinaryExpression)) return false;
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)parent;
return binaryExpression.getLOperand() == myPrefixExpression && TypeConversionUtil.isBooleanType(binaryExpression.getType());
}
public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiExpression operand = myPrefixExpression.getOperand();
final PsiElement unnegated = myPrefixExpression.replace(operand);
final PsiElement parent = unnegated.getParent();
final PsiElementFactory factory = file.getManager().getElementFactory();
final PsiPrefixExpression negated = (PsiPrefixExpression)factory.createExpressionFromText("!(xxx)", parent);
final PsiParenthesizedExpression parentheses = (PsiParenthesizedExpression)negated.getOperand();
parentheses.getExpression().replace(parent.copy());
parent.replace(negated);
}
public boolean startInWriteAction() {
return true;
}
}