mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-105132 Swap adjoined if-statements intention. Invokes on "else" keyword
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class SwapIfStatementsIntentionAction extends PsiElementBaseIntentionAction {
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
|
||||
final PsiIfStatement ifStatement = (PsiIfStatement)element.getParent();
|
||||
final PsiIfStatement nestedIfStatement = (PsiIfStatement) ifStatement.getElseBranch();
|
||||
assert nestedIfStatement != null;
|
||||
|
||||
final PsiExpression condition = ifStatement.getCondition();
|
||||
final PsiExpression nestedCondition = nestedIfStatement.getCondition();
|
||||
|
||||
final PsiStatement thenBranch = ifStatement.getThenBranch();
|
||||
final PsiStatement nestedThenBranch = nestedIfStatement.getThenBranch();
|
||||
|
||||
assert condition != null;
|
||||
assert nestedCondition != null;
|
||||
assert thenBranch != null;
|
||||
assert nestedThenBranch != null;
|
||||
|
||||
final PsiElement conditionCopy = condition.copy();
|
||||
condition.replace(nestedCondition);
|
||||
nestedCondition.replace(conditionCopy);
|
||||
|
||||
final PsiElement thenBranchCopy = thenBranch.copy();
|
||||
thenBranch.replace(nestedThenBranch);
|
||||
nestedThenBranch.replace(thenBranchCopy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
|
||||
if (!(element instanceof PsiKeyword) || !PsiKeyword.ELSE.equals(element.getText())) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement parent = element.getParent();
|
||||
return parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() instanceof PsiIfStatement;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Swap If Statements";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (otherCondition) {
|
||||
doAnotherAction();
|
||||
} else if (someCondition) {
|
||||
doSomeAction();
|
||||
} else {
|
||||
defaultAction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (a) {
|
||||
System.out.println(1);
|
||||
} else if (c) {
|
||||
System.out.println(3);
|
||||
} else if (b) {
|
||||
System.out.println(2);
|
||||
} else if (d) {
|
||||
System.out.println(4);
|
||||
} else {
|
||||
System.out.println(5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (cond2) m2();
|
||||
else if (cond1) m1();
|
||||
else if (cond3) m3();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (someCondition) {
|
||||
doSomeAction();
|
||||
} e<caret>lse if (otherCondition) {
|
||||
doAnotherAction();
|
||||
} else {
|
||||
defaultAction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (a) {
|
||||
System.out.println(1);
|
||||
} else if (b) {
|
||||
System.out.println(2);
|
||||
} el<caret>se if (c) {
|
||||
System.out.println(3);
|
||||
} else if (d) {
|
||||
System.out.println(4);
|
||||
} else {
|
||||
System.out.println(5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Swap If Statements" "false"
|
||||
class A {
|
||||
void m() {
|
||||
if (someCondition) {
|
||||
doSomeAction();
|
||||
} e<caret>lse {
|
||||
defaultAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Swap If Statements" "true"
|
||||
class A {
|
||||
|
||||
void m() {
|
||||
|
||||
if (cond1) m1();
|
||||
el<caret>se if (cond2) m2();
|
||||
else if (cond3) m3();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.intention;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class SwapIfStatementsTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/swapIfStatements";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldBeAvailableAfterExecution() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
if (otherCondition) {
|
||||
doAnotherAction();
|
||||
} else if (someCondition) {
|
||||
doSomeAction();
|
||||
} else {
|
||||
defaultAction();
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
if (someCondition) {
|
||||
doSomeAction();
|
||||
} <spot>else</spot> if (otherCondition) {
|
||||
doAnotherAction();
|
||||
} else {
|
||||
defaultAction();
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention swaps if-statements adjoined to else keyword
|
||||
</body>
|
||||
</html>
|
||||
@@ -719,6 +719,10 @@
|
||||
<className>com.intellij.codeInsight.intention.impl.CreateSwitchIntention</className>
|
||||
<category>Control Flow</category>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.SwapIfStatementsIntentionAction</className>
|
||||
<category>Control Flow</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.CreateFieldFromParameterAction</className>
|
||||
|
||||
Reference in New Issue
Block a user