2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.codeInsight
.daemon
.impl
.quickfix
;
18 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
19 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
20 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
21 import com
.intellij
.openapi
.editor
.Editor
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.psi
.*;
24 import com
.intellij
.psi
.util
.TypeConversionUtil
;
25 import com
.intellij
.util
.IncorrectOperationException
;
26 import org
.jetbrains
.annotations
.NotNull
;
30 * if (!a == b) ... => if (!(a == b)) ...
33 public class NegationBroadScopeFix
implements IntentionAction
{
34 private final PsiPrefixExpression myPrefixExpression
;
36 public NegationBroadScopeFix(PsiPrefixExpression prefixExpression
) {
37 myPrefixExpression
= prefixExpression
;
41 public String
getText() {
42 String text
= myPrefixExpression
.getOperand().getText();
44 PsiElement parent
= myPrefixExpression
.getParent();
45 String operation
= parent
instanceof PsiInstanceOfExpression
46 ? PsiKeyword
.INSTANCEOF
47 : ((PsiBinaryExpression
)parent
).getOperationSign().getText();
48 text
+= operation
+ " ";
51 if (parent
instanceof PsiInstanceOfExpression
) {
52 final PsiTypeElement type
= ((PsiInstanceOfExpression
)parent
).getCheckType();
53 rop
= type
== null ?
"" : type
.getText();
56 final PsiExpression rOperand
= ((PsiBinaryExpression
)parent
).getROperand();
57 rop
= rOperand
== null ?
"" : rOperand
.getText();
61 return QuickFixBundle
.message("negation.broader.scope.text", text
);
65 public String
getFamilyName() {
66 return QuickFixBundle
.message("negation.broader.scope.family");
69 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
70 if (myPrefixExpression
== null || !myPrefixExpression
.isValid()) return false;
72 PsiElement parent
= myPrefixExpression
.getParent();
73 if (parent
instanceof PsiInstanceOfExpression
&& ((PsiInstanceOfExpression
)parent
).getOperand() == myPrefixExpression
) {
76 if (!(parent
instanceof PsiBinaryExpression
)) return false;
77 PsiBinaryExpression binaryExpression
= (PsiBinaryExpression
)parent
;
78 return binaryExpression
.getLOperand() == myPrefixExpression
&& TypeConversionUtil
.isBooleanType(binaryExpression
.getType());
81 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) throws IncorrectOperationException
{
82 if (!CodeInsightUtilBase
.preparePsiElementForWrite(myPrefixExpression
)) return;
83 PsiExpression operand
= myPrefixExpression
.getOperand();
84 PsiElement unnegated
= myPrefixExpression
.replace(operand
);
85 PsiElement parent
= unnegated
.getParent();
86 PsiElementFactory factory
= JavaPsiFacade
.getInstance(file
.getProject()).getElementFactory();
88 PsiPrefixExpression negated
= (PsiPrefixExpression
)factory
.createExpressionFromText("!(xxx)", parent
);
89 PsiParenthesizedExpression parentheses
= (PsiParenthesizedExpression
)negated
.getOperand();
90 parentheses
.getExpression().replace(parent
.copy());
91 parent
.replace(negated
);
94 public boolean startInWriteAction() {