2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
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
.siyeh
.ipp
.trivialif
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.util
.PsiTreeUtil
;
20 import com
.intellij
.util
.IncorrectOperationException
;
21 import com
.siyeh
.ipp
.base
.Intention
;
22 import com
.siyeh
.ipp
.base
.PsiElementPredicate
;
23 import org
.jetbrains
.annotations
.NonNls
;
24 import org
.jetbrains
.annotations
.NotNull
;
26 public class ExpandBooleanIntention
extends Intention
{
30 public PsiElementPredicate
getElementPredicate() {
31 return new ExpandBooleanPredicate();
35 public void processIntention(@NotNull PsiElement element
)
36 throws IncorrectOperationException
{
37 final PsiStatement containingStatement
=
38 PsiTreeUtil
.getParentOfType(element
, PsiStatement
.class);
39 if (containingStatement
== null) {
42 if (ExpandBooleanPredicate
.isBooleanAssignment(containingStatement
)) {
43 final PsiExpressionStatement assignmentStatement
=
44 (PsiExpressionStatement
)containingStatement
;
45 final PsiAssignmentExpression assignmentExpression
=
46 (PsiAssignmentExpression
)assignmentStatement
.getExpression();
47 final PsiExpression rhs
= assignmentExpression
.getRExpression();
51 final String rhsText
= rhs
.getText();
52 final PsiExpression lhs
= assignmentExpression
.getLExpression();
53 final String lhsText
= lhs
.getText();
54 final PsiJavaToken sign
= assignmentExpression
.getOperationSign();
55 final String signText
= sign
.getText();
56 final String conditionText
;
57 if (signText
.length() == 2) {
58 conditionText
= lhsText
+ signText
.charAt(0) + rhsText
;
60 conditionText
= rhsText
;
62 @NonNls final String statement
=
63 "if(" + conditionText
+ ") " + lhsText
+ " = true; else " +
64 lhsText
+ " = false;";
65 replaceStatement(statement
, containingStatement
);
66 } else if (ExpandBooleanPredicate
.isBooleanReturn(
67 containingStatement
)) {
68 final PsiReturnStatement returnStatement
=
69 (PsiReturnStatement
)containingStatement
;
70 final PsiExpression returnValue
= returnStatement
.getReturnValue();
71 if (returnValue
== null) {
74 final String valueText
= returnValue
.getText();
75 @NonNls final String statement
=
76 "if(" + valueText
+ ") return true; else return false;";
77 replaceStatement(statement
, containingStatement
);