mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-50592 'Surround with cast' for binary expressions (e.g. a+b) should parenthesize those expressions
This commit is contained in:
+3
-1
@@ -25,6 +25,7 @@ import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiBinaryExpression;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.refactoring.introduceField.ElementToWorkOn;
|
||||
@@ -44,7 +45,8 @@ class JavaWithCastSurrounder extends JavaExpressionSurrounder {
|
||||
public TextRange surroundExpression(final Project project, final Editor editor, PsiExpression expr) throws IncorrectOperationException {
|
||||
assert expr.isValid();
|
||||
PsiType[] types = GuessManager.getInstance(project).guessTypeToCast(expr);
|
||||
final Template template = generateTemplate(project, expr.getText(), types);
|
||||
String exprText = expr instanceof PsiBinaryExpression ? "(" + expr.getText() + ")" : expr.getText();
|
||||
final Template template = generateTemplate(project, exprText, types);
|
||||
TextRange range;
|
||||
if (expr.isPhysical()) {
|
||||
range = expr.getTextRange();
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void foo(int a, double b) {
|
||||
if (<selection><caret>a + b</selection>) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void foo(int a, double b) {
|
||||
if (((<caret>) (a + b))) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
String a = "", b = "", c = "";
|
||||
if (a + (() b + c)) {
|
||||
if (a + (() (b + c))) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -106,7 +106,18 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
|
||||
doTest(getTestName(false), new JavaWithNotSurrounder());
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String fileName, final Surrounder surrounder) throws Exception {
|
||||
public void testSurroundBinaryWithCast() {
|
||||
final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject());
|
||||
templateManager.setTemplateTesting(true);
|
||||
try {
|
||||
doTest(getTestName(false), new JavaWithCastSurrounder());
|
||||
}
|
||||
finally {
|
||||
templateManager.setTemplateTesting(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String fileName, final Surrounder surrounder) {
|
||||
configureByFile(BASE_PATH + fileName + ".java");
|
||||
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
|
||||
checkResultByFile(BASE_PATH + fileName + "_after.java");
|
||||
|
||||
+1
@@ -15,6 +15,7 @@ public class SurroundExpressionTest extends SurroundTestCase {
|
||||
public void testType_cast2() throws Exception { doTest(new TypeCastSurrounder()); }
|
||||
public void testWhile1() throws Exception { doTest(new WhileExprSurrounder()); }
|
||||
public void testWith2() throws Exception { doTest(new WithExprSurrounder()); }
|
||||
public void testBinaryWithCast() throws Exception { doTest(new TypeCastSurrounder()); }
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<selection>a + b</selection>
|
||||
-----
|
||||
(() (a + b))
|
||||
@@ -1,3 +1,3 @@
|
||||
<selection>expr</selection>
|
||||
-----
|
||||
(() expr)
|
||||
((<caret>) expr)
|
||||
Reference in New Issue
Block a user