mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Implemented "Inline 'else' code branch" intention (IDEA-165428)
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
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.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Pavel.Dolgov
|
||||
*/
|
||||
public class InlineElseBranchAction extends PsiElementBaseIntentionAction {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
|
||||
PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiIfStatement) {
|
||||
PsiIfStatement ifStatement = (PsiIfStatement)parent;
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
PsiKeyword elseKeyword = ifStatement.getElseElement();
|
||||
if (elseBranch != null && elseKeyword != null) {
|
||||
InvertIfConditionAction.addAfter(ifStatement, elseBranch);
|
||||
elseBranch.delete();
|
||||
elseKeyword.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
if (element instanceof PsiKeyword && ((PsiKeyword)element).getTokenType() == JavaTokenType.ELSE_KEYWORD) {
|
||||
PsiElement parent = element.getParent();
|
||||
return parent instanceof PsiIfStatement &&
|
||||
((PsiIfStatement)parent).getElseBranch() != null &&
|
||||
parent.getParent() instanceof PsiCodeBlock;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return CodeInsightBundle.message("intention.inline.else.branch");
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -312,9 +312,9 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
ifStatement.setElseBranch(thenBranch);
|
||||
}
|
||||
|
||||
private static void addAfter(PsiIfStatement ifStatement, PsiStatement thenBranch) throws IncorrectOperationException {
|
||||
if (thenBranch instanceof PsiBlockStatement) {
|
||||
PsiBlockStatement blockStatement = (PsiBlockStatement) thenBranch;
|
||||
static void addAfter(PsiIfStatement ifStatement, PsiStatement branch) throws IncorrectOperationException {
|
||||
if (branch instanceof PsiBlockStatement) {
|
||||
PsiBlockStatement blockStatement = (PsiBlockStatement) branch;
|
||||
final PsiCodeBlock block = blockStatement.getCodeBlock();
|
||||
final PsiElement firstBodyElement = block.getFirstBodyElement();
|
||||
final PsiElement lastBodyElement = block.getLastBodyElement();
|
||||
@@ -322,7 +322,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
ifStatement.getParent().addRangeAfter(firstBodyElement, lastBodyElement, ifStatement);
|
||||
}
|
||||
} else {
|
||||
ifStatement.getParent().addAfter(thenBranch, ifStatement);
|
||||
ifStatement.getParent().addAfter(branch, ifStatement);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b)
|
||||
System.out.println("When true");
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
}
|
||||
// Before
|
||||
System.out.println("Otherwise");
|
||||
// After
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b)
|
||||
System.out.println("When true");
|
||||
// Before
|
||||
System.out.println("Otherwise");
|
||||
// After
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
}
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b)
|
||||
System.out.println("When true");
|
||||
<caret>else {
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
} <caret>else {
|
||||
// Before
|
||||
System.out.println("Otherwise");
|
||||
// After
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Inline 'else' branch" "false"
|
||||
|
||||
class T {
|
||||
void f(boolean a, boolean b) {
|
||||
if (a)
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
} <caret>else {
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b)
|
||||
System.out.println("When true");
|
||||
<caret>else
|
||||
// Before
|
||||
System.out.println("Otherwise");
|
||||
// After
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Inline 'else' branch" "true"
|
||||
|
||||
class T {
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
} <caret>else {
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
|
||||
|
||||
/**
|
||||
* @author Pavel.Dolgov
|
||||
*/
|
||||
public class InlineElseBranchTest extends LightIntentionActionTestCase {
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/inlineElseBranch/";
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
doAllTests();
|
||||
}
|
||||
}
|
||||
@@ -235,6 +235,7 @@ intention.replace.cast.with.var.family=Replace cast with variable
|
||||
intention.convert.color.representation.text=Convert to ''new Color{0}''
|
||||
intention.convert.color.representation.family=Convert Color representation
|
||||
intention.break.string.on.line.breaks.text=Break string on '\\n'
|
||||
intention.inline.else.branch=Inline 'else' branch
|
||||
|
||||
|
||||
intention.create.test=Create Test
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
}
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
void f(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("When true");
|
||||
} <spot>else</spot> {
|
||||
System.out.println("Otherwise");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention inlines the code from the <b>else</b> branch after the <b>if</b> statement.
|
||||
</body>
|
||||
</html>
|
||||
@@ -972,6 +972,10 @@
|
||||
<className>com.intellij.codeInsight.intention.impl.ConvertCompareToToEqualsIntention</className>
|
||||
<category>Java/Control Flow</category>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.InlineElseBranchAction</className>
|
||||
<category>Java/Control Flow</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.CreateFieldFromParameterAction</className>
|
||||
|
||||
Reference in New Issue
Block a user