mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
EnsureCodeBlock: support ternary in variable declaration (also fixes IDEA-202048)
This commit is contained in:
@@ -3,6 +3,7 @@ package com.intellij.refactoring.util;
|
||||
|
||||
import com.intellij.codeInsight.BlockUtils;
|
||||
import com.intellij.codeInsight.intention.impl.SplitConditionUtil;
|
||||
import com.intellij.codeInsight.intention.impl.SplitDeclarationAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
@@ -189,7 +190,20 @@ class EnsureCodeBlockImpl {
|
||||
return newParent;
|
||||
}
|
||||
private static PsiElement replaceTernaryWithIf(PsiStatement statement, PsiConditionalExpression ternary) {
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(statement.getProject());
|
||||
Project project = statement.getProject();
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
PsiElement parent = PsiUtil.skipParenthesizedExprUp(ternary.getParent());
|
||||
if (parent instanceof PsiLocalVariable) {
|
||||
PsiLocalVariable variable = (PsiLocalVariable)parent;
|
||||
variable.normalizeDeclaration();
|
||||
PsiDeclarationStatement declaration = (PsiDeclarationStatement)variable.getParent();
|
||||
PsiAssignmentExpression assignment =
|
||||
SplitDeclarationAction.invokeOnDeclarationStatement(declaration, PsiManager.getInstance(project), project);
|
||||
if (assignment != null) {
|
||||
ternary = (PsiConditionalExpression)Objects.requireNonNull(PsiUtil.skipParenthesizedExprDown(assignment.getRExpression()));
|
||||
statement = (PsiStatement)assignment.getParent();
|
||||
}
|
||||
}
|
||||
PsiIfStatement ifStatement =
|
||||
(PsiIfStatement)factory.createStatementFromText("if(" + ternary.getCondition().getText() + ") {} else {}", statement);
|
||||
Object mark = new Object();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
void test(String s) {
|
||||
final String x;
|
||||
if (s == null) {
|
||||
x = "";
|
||||
} else {
|
||||
String temp = s.trim();
|
||||
x = temp.substring(1);
|
||||
}
|
||||
final String y = x.replace("foo", "bar");
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
void test(String s) {
|
||||
final String x = s == null ? "" : <selection>s.trim()</selection>.substring(1), y = x.replace("foo", "bar");
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
@@ -247,6 +247,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING));
|
||||
}
|
||||
|
||||
public void testDeclareTernary() {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING));
|
||||
}
|
||||
|
||||
public void testLambdaAndChain() {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING));
|
||||
}
|
||||
|
||||
+3
@@ -930,6 +930,9 @@ public class ControlFlowUtils {
|
||||
if (parent instanceof PsiConditionalExpression && ((PsiConditionalExpression)parent).getCondition() != cur) {
|
||||
PsiElement ternaryParent = PsiUtil.skipParenthesizedExprUp(parent.getParent());
|
||||
return ternaryParent instanceof PsiReturnStatement ||
|
||||
(ternaryParent instanceof PsiLocalVariable &&
|
||||
(!((PsiLocalVariable)ternaryParent).getTypeElement().isInferredType() ||
|
||||
PsiTypesUtil.isDenotableType(((PsiLocalVariable)ternaryParent).getType(), ternaryParent))) ||
|
||||
(ternaryParent instanceof PsiAssignmentExpression && ternaryParent.getParent() instanceof PsiExpressionStatement &&
|
||||
PsiUtil.skipParenthesizedExprDown(((PsiAssignmentExpression)ternaryParent).getRExpression()) == parent);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user