mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG/IPP: fix extract increment/decrement to separate statement (IDEA-203698)
This commit is contained in:
+14
-16
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -143,24 +144,27 @@ public class IncrementDecrementUsedAsExpressionInspection
|
||||
return;
|
||||
}
|
||||
final Project project = element.getProject();
|
||||
final PsiElementFactory factory =
|
||||
JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
final String newStatementText = element.getText() + ';';
|
||||
final String operandText = operand.getText();
|
||||
if (parent instanceof PsiIfStatement ||
|
||||
parent instanceof PsiLoopStatement) {
|
||||
if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement || parent instanceof PsiSwitchLabeledRuleStatement) {
|
||||
// need to add braces because
|
||||
// in/decrement is inside braceless control statement body
|
||||
final StringBuilder text = new StringBuilder();
|
||||
text.append('{');
|
||||
final String elementText =
|
||||
PsiReplacementUtil.getElementText(statement, element, operandText);
|
||||
final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText);
|
||||
if (element instanceof PsiPostfixExpression) {
|
||||
if (parent instanceof PsiSwitchLabeledRuleStatement) {
|
||||
text.append("break ");
|
||||
}
|
||||
text.append(elementText);
|
||||
text.append(newStatementText);
|
||||
}
|
||||
else {
|
||||
text.append(newStatementText);
|
||||
if (parent instanceof PsiSwitchLabeledRuleStatement) {
|
||||
text.append("break ");
|
||||
}
|
||||
text.append(elementText);
|
||||
}
|
||||
text.append('}');
|
||||
@@ -323,16 +327,11 @@ public class IncrementDecrementUsedAsExpressionInspection
|
||||
}
|
||||
|
||||
public static boolean isSuitableForReplacement(@NotNull PsiUnaryExpression expression) {
|
||||
final PsiElement parent = expression.getParent();
|
||||
if (parent instanceof PsiExpressionStatement ||
|
||||
(parent instanceof PsiExpressionList &&
|
||||
parent.getParent() instanceof
|
||||
PsiExpressionListStatement)) {
|
||||
if (ExpressionUtils.isVoidContext(expression)) {
|
||||
return false;
|
||||
}
|
||||
final IElementType tokenType = expression.getOperationTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.PLUSPLUS) &&
|
||||
!tokenType.equals(JavaTokenType.MINUSMINUS)) {
|
||||
if (!tokenType.equals(JavaTokenType.PLUSPLUS) && !tokenType.equals(JavaTokenType.MINUSMINUS)) {
|
||||
return false;
|
||||
}
|
||||
return PsiTreeUtil.getParentOfType(expression, PsiStatement.class) != null;
|
||||
@@ -347,8 +346,7 @@ public class IncrementDecrementUsedAsExpressionInspection
|
||||
extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitUnaryExpression(
|
||||
@NotNull PsiUnaryExpression expression) {
|
||||
public void visitUnaryExpression(@NotNull PsiUnaryExpression expression) {
|
||||
super.visitUnaryExpression(expression);
|
||||
|
||||
if (isSuitableForReplacement(expression)) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Main {
|
||||
int test(int i) {
|
||||
return switch(i) {
|
||||
default -> ++<caret>i;
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class Main {
|
||||
int test(int i) {
|
||||
return switch(i) {
|
||||
default -> {
|
||||
++i;
|
||||
break i;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+3
-15
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ipp.extractIncrement;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
@@ -34,6 +20,8 @@ public class ExtractIncrementAndDecrementTest extends IPPTestCase {
|
||||
|
||||
public void testPrefixIncrement() {doExtractTest("++");}
|
||||
|
||||
public void testSwitchExpression() {doExtractTest("++");}
|
||||
|
||||
public void testSingleDoWhileBody() {doExtractTest("++");}
|
||||
|
||||
public void testDecrementInForUpdate() {doNegativeTest("--");}
|
||||
|
||||
Reference in New Issue
Block a user