From 0cb01271b64220484a03c3c8bab2bd979c4fae2d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 3 Mar 2014 19:19:19 +0100 Subject: [PATCH] IDEA-121379 ("Remove braces from if statement" is not available on "if" keyword and condition of if-else construct) --- .../siyeh/ipp/braces/BaseBracesIntention.java | 63 ++++--------------- .../ipp/braces/remove/BetweenIfAndElse.java | 10 +++ .../com/siyeh/ipp/braces/remove/IfElse.java | 10 +++ .../com/siyeh/ipp/braces/remove/IfElse2.java | 10 +++ .../ipp/braces/remove/IfElse2_after.java | 8 +++ .../siyeh/ipp/braces/remove/IfElse_after.java | 8 +++ .../ipp/braces/RemoveBracesIntentionTest.java | 39 ++++++++++++ 7 files changed, 97 insertions(+), 51 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/BetweenIfAndElse.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2_after.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse_after.java create mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/braces/RemoveBracesIntentionTest.java diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/braces/BaseBracesIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/braces/BaseBracesIntention.java index 9b1e57afe720..ecaa8acad5cf 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/braces/BaseBracesIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/braces/BaseBracesIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -15,7 +15,6 @@ */ package com.siyeh.ipp.braces; -import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ipp.base.MutablyNamedIntention; @@ -57,59 +56,21 @@ public abstract class BaseBracesIntention extends MutablyNamedIntention { final PsiElement parent = element.getParent(); if (parent instanceof PsiIfStatement) { final PsiIfStatement ifStatement = (PsiIfStatement)parent; - if (isBetweenThen(ifStatement, element)) { - return ifStatement.getThenBranch(); - } - - if (isBetweenElse(ifStatement, element)) { + final PsiStatement thenBranch = ifStatement.getThenBranch(); + final int offset = element.getTextOffset(); + if (thenBranch != null && offset > thenBranch.getTextOffset()) { + final PsiKeyword elseElement = ifStatement.getElseElement(); + if (elseElement == null || offset < elseElement.getTextOffset()) { + // no 'else' branch or after 'then' branch but before 'else' keyword + return null; + } return ifStatement.getElseBranch(); } + return thenBranch; } - if (parent instanceof PsiWhileStatement) { - return ((PsiWhileStatement)parent).getBody(); - } - if (parent instanceof PsiDoWhileStatement) { - return ((PsiDoWhileStatement)parent).getBody(); - } - if (parent instanceof PsiForStatement) { - return ((PsiForStatement)parent).getBody(); - } - if (parent instanceof PsiForeachStatement) { - return ((PsiForeachStatement)parent).getBody(); + if (parent instanceof PsiLoopStatement) { + return ((PsiLoopStatement)parent).getBody(); } return null; } - - private static boolean isBetweenThen(@NotNull PsiIfStatement ifStatement, @NotNull PsiElement element) { - final PsiElement rParenth = ifStatement.getRParenth(); - final PsiElement elseElement = ifStatement.getElseElement(); - - if (rParenth == null) { - return false; - } - - if (elseElement == null) { - return true; - } - - final TextRange rParenthTextRangeTextRange = rParenth.getTextRange(); - final TextRange elseElementTextRange = elseElement.getTextRange(); - final TextRange elementTextRange = element.getTextRange(); - - return new TextRange(rParenthTextRangeTextRange.getEndOffset(), elseElementTextRange.getStartOffset()).contains(elementTextRange); - } - - private static boolean isBetweenElse(@NotNull PsiIfStatement ifStatement, @NotNull PsiElement element) { - final PsiElement elseElement = ifStatement.getElseElement(); - - if (elseElement == null) { - return false; - } - - final TextRange ifStatementTextRange = ifStatement.getTextRange(); - final TextRange elseElementTextRange = elseElement.getTextRange(); - final TextRange elementTextRange = element.getTextRange(); - - return new TextRange(elseElementTextRange.getStartOffset(), ifStatementTextRange.getEndOffset()).contains(elementTextRange); - } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/BetweenIfAndElse.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/BetweenIfAndElse.java new file mode 100644 index 000000000000..00658098d56d --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/BetweenIfAndElse.java @@ -0,0 +1,10 @@ +class X { + { + if (true) { + System.out.println(); + } + else { + System.out.println(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse.java new file mode 100644 index 000000000000..29a595804f5c --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse.java @@ -0,0 +1,10 @@ +class X { + { + if (true) { + System.out.println(); + } + else { + System.out.println(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2.java new file mode 100644 index 000000000000..9815cdf830e4 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2.java @@ -0,0 +1,10 @@ +class X { + { + if (true) { + System.out.println(); + } + else { + System.out.println(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2_after.java new file mode 100644 index 000000000000..feae02671942 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse2_after.java @@ -0,0 +1,8 @@ +class X { + { + if (true) System.out.println(); + else { + System.out.println(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse_after.java new file mode 100644 index 000000000000..feae02671942 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/braces/remove/IfElse_after.java @@ -0,0 +1,8 @@ +class X { + { + if (true) System.out.println(); + else { + System.out.println(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/braces/RemoveBracesIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/braces/RemoveBracesIntentionTest.java new file mode 100644 index 000000000000..74ebd04fe402 --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/braces/RemoveBracesIntentionTest.java @@ -0,0 +1,39 @@ +/* + * 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.siyeh.ipp.braces; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +/** + * @see RemoveBracesIntention + * @author Bas Leijdekkers + */ +public class RemoveBracesIntentionTest extends IPPTestCase { + @Override + protected String getRelativePath() { + return "braces/remove"; + } + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("remove.braces.intention.name", "if"); + } + + public void testBetweenIfAndElse() { assertIntentionNotAvailable(RemoveBracesIntention.class);} + public void testIfElse() { doTest(); } + public void testIfElse2() { doTest(); } +}