mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
IDEA-119364 (not introduces case statements for enum)
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class CreateSwitchIntention extends PsiElementBaseIntentionAction {
|
||||
public static final String TEXT = "Create switch statement";
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
|
||||
final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
PsiSwitchStatement switchStatement = (PsiSwitchStatement)elementFactory
|
||||
.createStatementFromText(String.format("switch (%s) {\n$MARKER$\n}", expressionStatement.getExpression().getText()), null);
|
||||
switchStatement = (PsiSwitchStatement)expressionStatement.replace(switchStatement);
|
||||
|
||||
for (final PsiStatement psiStatement : switchStatement.getBody().getStatements()) {
|
||||
if (psiStatement.getText().equals("$MARKER$")) {
|
||||
final Document document = editor.getDocument();
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
|
||||
final int textOffset = psiStatement.getTextOffset();
|
||||
editor.getCaretModel().moveToOffset(textOffset);
|
||||
document.deleteString(textOffset, textOffset + psiStatement.getTextLength());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
|
||||
final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
|
||||
return expressionStatement != null && isValidTypeForSwitch(expressionStatement.getExpression().getType());
|
||||
}
|
||||
|
||||
private static PsiExpressionStatement resolveExpressionStatement(final PsiElement element) {
|
||||
if (element instanceof PsiExpressionStatement) {
|
||||
return (PsiExpressionStatement)element;
|
||||
} else if (element instanceof PsiWhiteSpace) {
|
||||
final PsiElement prevSibling = element.getPrevSibling();
|
||||
return prevSibling instanceof PsiExpressionStatement ? (PsiExpressionStatement)prevSibling : null;
|
||||
} else {
|
||||
final PsiStatement psiStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
|
||||
return psiStatement instanceof PsiExpressionStatement ? (PsiExpressionStatement)psiStatement : null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidTypeForSwitch(@Nullable final PsiType type) {
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClass resolvedClass = ((PsiClassType)type).resolve();
|
||||
if (resolvedClass == null) {
|
||||
return false;
|
||||
}
|
||||
return resolvedClass.isEnum() || (CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName()));
|
||||
}
|
||||
return type.equals(PsiType.INT) || type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return TEXT;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
}
|
||||
11
java/java-tests/testData/codeInsight/createSwitch/enum.java
Normal file
11
java/java-tests/testData/codeInsight/createSwitch/enum.java
Normal file
@@ -0,0 +1,11 @@
|
||||
class X {
|
||||
|
||||
enum MyEnum {
|
||||
a,b,c
|
||||
}
|
||||
|
||||
void m(MyEnum e) {
|
||||
e<caret>
|
||||
}
|
||||
|
||||
}
|
||||
11
java/java-tests/testData/codeInsight/createSwitch/enum2.java
Normal file
11
java/java-tests/testData/codeInsight/createSwitch/enum2.java
Normal file
@@ -0,0 +1,11 @@
|
||||
class X {
|
||||
|
||||
enum MyEnum {
|
||||
a,b,c
|
||||
}
|
||||
|
||||
void m(MyEnum e) {
|
||||
<caret>e
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
class X {
|
||||
|
||||
enum MyEnum {
|
||||
a,b,c
|
||||
}
|
||||
|
||||
void m(MyEnum e) {
|
||||
switch (e) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
class X {
|
||||
|
||||
enum MyEnum {
|
||||
a,b,c
|
||||
}
|
||||
|
||||
void m(MyEnum e) {
|
||||
switch (e) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
int foo = 10;
|
||||
m2(fo<caret>o);
|
||||
|
||||
}
|
||||
|
||||
void m2(int i) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
int foo = 10;
|
||||
int bar = fo<caret>o + 10;
|
||||
|
||||
}
|
||||
|
||||
void m2(int i) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
int foo = 10;
|
||||
fo<caret>o
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
int foo = 10;
|
||||
switch (foo) {
|
||||
<caret>
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
String s = getStr();
|
||||
s<caret>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class X {
|
||||
|
||||
void m() {
|
||||
|
||||
String s = getStr();
|
||||
switch (s) {
|
||||
<caret>
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.JavaTestUtil;
|
||||
import com.intellij.codeInsight.intention.impl.CreateSwitchIntention;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class CreateSwitchTest extends JavaCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/createSwitch/";
|
||||
}
|
||||
|
||||
public void testEnum() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testEnum2() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testString() {
|
||||
doTestString();
|
||||
}
|
||||
|
||||
public void testPrimitive() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNotAvailable() {
|
||||
doTestNotAvailable();
|
||||
}
|
||||
|
||||
public void testNotAvailable2() {
|
||||
doTestNotAvailable();
|
||||
}
|
||||
|
||||
private void doTestString() {
|
||||
final LanguageLevelProjectExtension languageLevelProjectExtension = LanguageLevelProjectExtension.getInstance(getProject());
|
||||
final LanguageLevel oldLanguageLevel = languageLevelProjectExtension.getLanguageLevel();
|
||||
languageLevelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
try {
|
||||
doTest();
|
||||
}
|
||||
finally {
|
||||
languageLevelProjectExtension.setLanguageLevel(oldLanguageLevel);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
final String name = getTestName(true);
|
||||
CodeInsightTestUtil.doIntentionTest(myFixture, CreateSwitchIntention.TEXT, name + ".java", name + "_after.java");
|
||||
}
|
||||
|
||||
private void doTestNotAvailable() {
|
||||
myFixture.configureByFile(getTestName(true) + ".java");
|
||||
assertEmpty(myFixture.filterAvailableIntentions(CreateSwitchIntention.TEXT));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
void m(int var) {
|
||||
switch(var) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
void m(int var) {
|
||||
<spot>var<spot>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Creates switch statement for selected variable
|
||||
</body>
|
||||
</html>
|
||||
@@ -712,6 +712,10 @@
|
||||
<className>com.intellij.codeInsight.intention.impl.DeannotateIntentionAction</className>
|
||||
<category>Control Flow</category>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.CreateSwitchIntention</className>
|
||||
<category>Control Flow</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.CreateFieldFromParameterAction</className>
|
||||
|
||||
Reference in New Issue
Block a user