mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-121379 ("Remove braces from if statement" is not available on "if" keyword and condition of if-else construct)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
{
|
||||
if (true) {
|
||||
System.out.println();
|
||||
}
|
||||
<caret> else {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
{
|
||||
if (true) {
|
||||
System.out.println();
|
||||
}<caret>
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
{
|
||||
if<caret> (true) {
|
||||
System.out.println();
|
||||
}
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class X {
|
||||
{
|
||||
if (true) System.out.println();
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class X {
|
||||
{
|
||||
if (true) System.out.println();
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(); }
|
||||
}
|
||||
Reference in New Issue
Block a user