Java: Fixed "Unwrap else branch" - create consistent PSI (IDEA-192543)

This commit is contained in:
Pavel Dolgov
2018-05-30 20:42:00 +03:00
parent 7d8ae63657
commit 10909be55a
3 changed files with 33 additions and 3 deletions

View File

@@ -44,9 +44,10 @@ public class UnwrapElseBranchAction extends PsiElementBaseIntentionAction {
if (elseBranch != null && grandParent != null) {
if (!(grandParent instanceof PsiCodeBlock)) {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
PsiCodeBlock codeBlock = factory.createCodeBlockFromText("{" + ifStatement.getText() + "}", ifStatement);
codeBlock = (PsiCodeBlock)ifStatement.replace(codeBlock);
ifStatement = (PsiIfStatement)codeBlock.getStatements()[0];
PsiBlockStatement blockStatement =
(PsiBlockStatement)factory.createStatementFromText("{" + ifStatement.getText() + "}", ifStatement);
blockStatement = (PsiBlockStatement)ifStatement.replace(blockStatement);
ifStatement = (PsiIfStatement)blockStatement.getCodeBlock().getStatements()[0];
elseBranch = ifStatement.getElseBranch();
LOG.assertTrue(elseBranch != null);
}

View File

@@ -0,0 +1,15 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (b) {
return "When true";
} else {
if (a) {
return "a";
}
return "Otherwise";
}
return "Default";
}
}

View File

@@ -0,0 +1,14 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (b) {
return "When true";
} else if (a) {
return "a";
} <caret>else {
return "Otherwise";
}
return "Default";
}
}