introduce variable: allow subtyping during non-physical expression creation (IDEA-216460)

GitOrigin-RevId: d26f7b045b8c88ebfcc64e854221db5c1b2ff6c1
This commit is contained in:
Anna Kozlova
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 824db4c08b
commit 68eb38b104
4 changed files with 28 additions and 3 deletions
@@ -26,7 +26,6 @@ import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.TextRange;
@@ -485,8 +484,13 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
return null;
}
}
else if (containingExpression != null && !Comparing.equal(containingExpression.getType(), tempExpr.getType())){
return null;
else if (containingExpression != null) {
PsiType containingExpressionType = containingExpression.getType();
PsiType tempExprType = tempExpr.getType();
if (containingExpressionType != null &&
(tempExprType == null || !TypeConversionUtil.isAssignable(containingExpressionType, tempExprType))) {
return null;
}
}
final PsiReferenceExpression refExpr = PsiTreeUtil.getParentOfType(toBeExpression.findElementAt(refIdx[0]), PsiReferenceExpression.class);
@@ -0,0 +1,9 @@
import java.util.*;
class MyTest {
private void foo(List<Object> b) {
int i = 6 - b.size();
if (i - 2L < 1) {}
}
}
@@ -0,0 +1,8 @@
import java.util.*;
class MyTest {
private void foo(List<Object> b) {
if (<selection>6 - b.size()</selection> - 2L < 1) {}
}
}
@@ -368,6 +368,10 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("str", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}
public void testSubExpressionFromPrimitiveWithConversion() {
doTest(new MockIntroduceVariableHandler("i", false, false, false, "int"));
}
public void testSubPrimitiveLiteral() {
doTest(new MockIntroduceVariableHandler("str", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}