Java surrounders: if and not surrounder should be able to handle java.lang

.Boolean expressions
This commit is contained in:
Alexander Zolotov
2014-01-08 15:41:08 +11:00
parent d35b82798f
commit 2665a70fef
10 changed files with 115 additions and 12 deletions
@@ -0,0 +1,28 @@
/*
* 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.generation.surroundWith;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
abstract public class JavaBooleanExpressionSurrounder extends JavaExpressionSurrounder {
@Override
public boolean isApplicable(PsiExpression expr) {
PsiType type = expr.getType();
return type != null && (PsiType.BOOLEAN.equals(type) || PsiType.BOOLEAN.equals(PsiPrimitiveType.getUnboxedType(type)));
}
}
@@ -1,6 +1,6 @@
/*
* Copyright 2000-2009 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.
@@ -23,15 +23,14 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.util.FileTypeUtils;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
public class JavaWithIfExpressionSurrounder extends JavaExpressionSurrounder{
public class JavaWithIfExpressionSurrounder extends JavaBooleanExpressionSurrounder {
@Override
public boolean isApplicable(PsiExpression expr) {
PsiType type = expr.getType();
if (PsiType.BOOLEAN != type) return false;
if (!super.isApplicable(expr)) return false;
if (!expr.isPhysical()) return false;
PsiElement parent = expr.getParent();
if (!(parent instanceof PsiExpressionStatement)) return false;
@@ -1,6 +1,6 @@
/*
* Copyright 2000-2009 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.
@@ -25,12 +25,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.util.IncorrectOperationException;
class JavaWithNotSurrounder extends JavaExpressionSurrounder{
@Override
public boolean isApplicable(PsiExpression expr) {
return PsiType.BOOLEAN.equals(expr.getType());
}
class JavaWithNotSurrounder extends JavaBooleanExpressionSurrounder {
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
PsiManager manager = expr.getManager();
@@ -0,0 +1,9 @@
class Test {
boolean foo() {
return null;
}
void bar() {
<selection>foo()</selection>
}
}
@@ -0,0 +1,11 @@
import java.lang.Boolean;
class Test {
Boolean foo() {
return null;
}
void bar() {
<selection>foo()</selection>
}
}
@@ -0,0 +1,13 @@
import java.lang.Boolean;
class Test {
Boolean foo() {
return null;
}
void bar() {
if (foo()) {
}
}
}
@@ -0,0 +1,11 @@
class Test {
boolean foo() {
return null;
}
void bar() {
if (foo()) {
}
}
}
@@ -0,0 +1,11 @@
import java.lang.Boolean;
class Test {
Boolean foo() {
return null;
}
void bar() {
<selection>foo()</selection>
}
}
@@ -0,0 +1,11 @@
import java.lang.Boolean;
class Test {
Boolean foo() {
return null;
}
void bar() {
!(foo())
}
}
@@ -130,6 +130,21 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
doTest(getTestName(false), new JavaWithNullCheckSurrounder());
}
public void testSurroundExpressionWithIf() {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
doTest(getTestName(false), new JavaWithIfExpressionSurrounder());
}
public void testSurroundExpressionWithIfForBoxedBooleans() {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
doTest(getTestName(false), new JavaWithIfExpressionSurrounder());
}
public void testSurroundExpressionWithNotForBoxedBooleans() {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
doTest(getTestName(false), new JavaWithNotSurrounder());
}
private void doTest(@NotNull String fileName, final Surrounder surrounder) {
configureByFile(BASE_PATH + fileName + ".java");