ExpectedTypesProvider and CreateLocalFromUsageFix updated for Java 12 (IDEA-204006)

This commit is contained in:
Tagir Valeev
2018-12-21 16:32:43 +07:00
parent 012fe62a55
commit 03e4f37774
8 changed files with 94 additions and 1 deletions
@@ -305,11 +305,26 @@ public class ExpectedTypesProvider {
@Override
public void visitExpressionStatement(PsiExpressionStatement statement) {
if (statement.getParent() instanceof PsiSwitchLabeledRuleStatement) {
PsiSwitchBlock block = ((PsiSwitchLabeledRuleStatement)statement.getParent()).getEnclosingSwitchBlock();
if (block instanceof PsiSwitchExpression) {
block.getParent().accept(this);
return;
}
}
if (myVoidable) {
myResult.add(VOID_EXPECTED);
}
}
@Override
public void visitBreakStatement(PsiBreakStatement statement) {
PsiElement exitedElement = statement.findExitedElement();
if (exitedElement instanceof PsiSwitchExpression) {
exitedElement.getParent().accept(this);
}
}
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
myExpr = (PsiExpression)myExpr.getParent();
@@ -469,6 +484,15 @@ public class ExpectedTypesProvider {
@Override
public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) {
processSwitchBlock(statement);
}
@Override
public void visitSwitchExpression(@NotNull PsiSwitchExpression expression) {
processSwitchBlock(expression);
}
public void processSwitchBlock(@NotNull PsiSwitchBlock statement) {
myResult.add(createInfoImpl(PsiType.LONG, PsiType.INT));
if (!PsiUtil.isLanguageLevel5OrHigher(statement)) {
return;
@@ -175,7 +175,17 @@ public class CreateLocalFromUsageFix extends CreateVarFromUsageFix {
minOffset = Math.min(minOffset, expressionOccurences[i].getTextRange().getStartOffset());
}
final PsiCodeBlock block = PsiTreeUtil.getParentOfType(parent, PsiCodeBlock.class, false);
PsiCodeBlock block = null;
while (parent != null) {
if (parent instanceof PsiCodeBlock) {
block = (PsiCodeBlock)parent;
break;
} else if (parent instanceof PsiSwitchLabeledRuleStatement) {
parent = ((PsiSwitchLabeledRuleStatement)parent).getEnclosingSwitchBlock();
} else {
parent = parent.getParent();
}
}
LOG.assertTrue(block != null && !block.isEmpty(), "block: " + block +"; parent: " + parent);
PsiStatement[] statements = block.getStatements();
for (int i = 1; i < statements.length; i++) {
@@ -0,0 +1,11 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
return switch (i) {
default -> {
String foo;
break foo;
}
};
}
}
@@ -0,0 +1,9 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
String foo;
return switch (i) {
default -> foo;
};
}
}
@@ -0,0 +1,11 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
int foo;
return switch (foo) {
default -> {
break i;
}
};
}
}
@@ -0,0 +1,10 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
return switch (i) {
default -> {
break fo<caret>o;
}
};
}
}
@@ -0,0 +1,8 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
return switch (i) {
default -> fo<caret>o;
};
}
}
@@ -0,0 +1,10 @@
// "Create local variable 'foo'" "true"
class Foo {
String test(int i) {
return switch (f<caret>oo) {
default -> {
break i;
}
};
}
}