2 * Copyright 2009 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
.forloop
;
18 import com
.siyeh
.ipp
.base
.Intention
;
19 import com
.siyeh
.ipp
.base
.PsiElementPredicate
;
20 import com
.siyeh
.ipp
.psiutils
.VariableAccessUtils
;
21 import com
.siyeh
.ipp
.psiutils
.ComparisonUtils
;
22 import com
.intellij
.psi
.*;
23 import com
.intellij
.psi
.tree
.IElementType
;
24 import com
.intellij
.util
.IncorrectOperationException
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 public class ReverseForLoopDirectionIntention
extends Intention
{
32 protected PsiElementPredicate
getElementPredicate() {
33 return new ReverseForLoopDirectionPredicate();
38 protected void processIntention(@NotNull PsiElement element
)
39 throws IncorrectOperationException
{
40 final PsiForStatement forStatement
=
41 (PsiForStatement
)element
.getParent();
42 final PsiDeclarationStatement initialization
=
43 (PsiDeclarationStatement
)forStatement
.getInitialization();
44 System
.out
.println("initialization: " + initialization
);
45 if (initialization
== null) {
48 final PsiBinaryExpression condition
=
49 (PsiBinaryExpression
)forStatement
.getCondition();
50 System
.out
.println("condition: " + condition
);
51 if (condition
== null) {
54 final PsiVariable variable
=
55 (PsiVariable
)initialization
.getDeclaredElements()[0];
56 final PsiExpression initializer
= variable
.getInitializer();
57 System
.out
.println("initializer: " + initializer
);
58 if (initializer
== null) {
61 final PsiExpression lhs
= condition
.getLOperand();
62 final PsiExpression rhs
= condition
.getROperand();
66 final PsiExpressionStatement update
=
67 (PsiExpressionStatement
)forStatement
.getUpdate();
68 final PsiExpression updateExpression
= update
.getExpression();
69 System
.out
.println("update: " + update
);
70 final String variableName
= variable
.getName();
71 final StringBuilder updateText
= new StringBuilder();
72 if (updateExpression
instanceof PsiPrefixExpression
) {
73 final PsiPrefixExpression prefixExpression
=
74 (PsiPrefixExpression
)updateExpression
;
75 final IElementType tokenType
=
76 prefixExpression
.getOperationTokenType();
77 if (JavaTokenType
.PLUSPLUS
== tokenType
) {
78 updateText
.append("--");
79 } else if (JavaTokenType
.MINUSMINUS
== tokenType
) {
80 updateText
.append("++");
82 System
.out
.println("here");
85 updateText
.append(variableName
);
86 } else if (updateExpression
instanceof PsiPostfixExpression
) {
87 updateText
.append(variableName
);
88 final PsiPostfixExpression postfixExpression
=
89 (PsiPostfixExpression
)updateExpression
;
90 final IElementType tokenType
=
91 postfixExpression
.getOperationTokenType();
92 if (JavaTokenType
.PLUSPLUS
== tokenType
) {
93 updateText
.append("--");
94 } else if (JavaTokenType
.MINUSMINUS
== tokenType
) {
95 updateText
.append("++");
102 final Project project
= element
.getProject();
103 final PsiElementFactory factory
=
104 JavaPsiFacade
.getElementFactory(project
);
105 final PsiExpression newUpdate
= factory
.createExpressionFromText(
106 updateText
.toString(), element
);
107 System
.out
.println("newUpdate: " + newUpdate
);
108 updateExpression
.replace(newUpdate
);
109 final PsiJavaToken sign
= condition
.getOperationSign();
110 final String negatedSign
= ComparisonUtils
.getNegatedComparison(sign
);
111 final StringBuilder conditionText
= new StringBuilder();
112 final String initializerText
= initializer
.getText();
113 if (VariableAccessUtils
.evaluatesToVariable(lhs
, variable
)) {
114 conditionText
.append(variableName
);
115 conditionText
.append(negatedSign
);
116 conditionText
.append(initializerText
);
117 initializer
.replace(rhs
);
118 } else if (VariableAccessUtils
.evaluatesToVariable(rhs
, variable
)) {
119 conditionText
.append(initializerText
);
120 conditionText
.append(negatedSign
);
121 conditionText
.append(variableName
);
122 initializer
.replace(lhs
);
126 final PsiExpression newCondition
= factory
.createExpressionFromText(
127 conditionText
.toString(), element
);
128 condition
.replace(newCondition
);