extract if condition: ensure brackets (IDEA-134698)

This commit is contained in:
Anna Kozlova
2014-12-29 18:57:46 +01:00
parent 25dd5b4814
commit 92bb2b6d65
12 changed files with 147 additions and 4 deletions
@@ -186,7 +186,7 @@ public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
private static String createIfString(@NotNull PsiExpression condition,
@NotNull PsiStatement thenBranch,
@Nullable PsiStatement elseBranch) {
return createIfString(condition.getText(), toThenBranchString(thenBranch), toElseBranchString(elseBranch));
return createIfString(condition.getText(), toThenBranchString(thenBranch), toElseBranchString(elseBranch, false));
}
@NotNull
@@ -200,7 +200,7 @@ public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
private static String createIfString(@NotNull PsiExpression condition,
@NotNull String thenBranch,
@Nullable PsiStatement elseBranch) {
return createIfString(condition.getText(), thenBranch, toElseBranchString(elseBranch));
return createIfString(condition.getText(), thenBranch, toElseBranchString(elseBranch, true));
}
@NotNull
@@ -221,12 +221,12 @@ public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
}
@Nullable
private static String toElseBranchString(@Nullable PsiStatement statement) {
private static String toElseBranchString(@Nullable PsiStatement statement, boolean skipElse) {
if (statement == null) {
return null;
}
if (statement instanceof PsiBlockStatement || statement instanceof PsiIfStatement) {
if (statement instanceof PsiBlockStatement || skipElse && statement instanceof PsiIfStatement) {
return statement.getText();
}
@@ -0,0 +1,16 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (a)
if (b) {
System.out.println("a&b");
} else {
if (c) {
System.out.println("c");
}
}
else if (c) {
System.out.println("c");
}
}
}
@@ -0,0 +1,9 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (a)
if (b) {
System.out.println("a&b");
}
}
}
@@ -0,0 +1,14 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (a) {
System.out.println("a&b");
} else if (b) {
System.out.println("a&b");
} else {
if (c) {
System.out.println("c");
}
}
}
}
@@ -0,0 +1,12 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (a) {
System.out.println("a&b");
} else if (b) {
System.out.println("a&b");
} else {
System.out.println("c");
}
}
}
@@ -0,0 +1,14 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (a)
if (b) {
System.out.println("a&b");
} else {
System.out.println("c");
}
else {
System.out.println("c");
}
}
}
@@ -0,0 +1,10 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (<caret>a && b) {
System.out.println("a&b");
} else if (c) {
System.out.println("c");
}
}
}
@@ -0,0 +1,8 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (<caret>a && b) {
System.out.println("a&b");
}
}
}
@@ -0,0 +1,10 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (<caret>a || b) {
System.out.println("a&b");
} else if (c) {
System.out.println("c");
}
}
}
@@ -0,0 +1,10 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (<caret>a || b) {
System.out.println("a&b");
} else {
System.out.println("c");
}
}
}
@@ -0,0 +1,10 @@
// "Extract if (a)" "true"
class TestThreadInspection {
void f(boolean a, boolean b, boolean c){
if (<caret>a && b) {
System.out.println("a&b");
} else {
System.out.println("c");
}
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2000-2014 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.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class ExtractIfConditionTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/extractIfCondition";
}
}