mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
EnsureCodeBlock: simplify switch rule handling
This commit is contained in:
+6
-15
@@ -120,18 +120,6 @@ class VariableExtractor {
|
||||
if (myAnchor instanceof PsiExpression) {
|
||||
myAnchor = RefactoringUtil.getParentStatement(RefactoringUtil.ensureCodeBlock(((PsiExpression)myAnchor)), false);
|
||||
}
|
||||
else if (myAnchor instanceof PsiSwitchLabeledRuleStatement) {
|
||||
PsiStatement body = ((PsiSwitchLabeledRuleStatement)myAnchor).getBody();
|
||||
if (body instanceof PsiExpressionStatement) {
|
||||
myAnchor = RefactoringUtil.getParentStatement(RefactoringUtil.ensureCodeBlock(((PsiExpressionStatement)body).getExpression()), false);
|
||||
}
|
||||
else if (body instanceof PsiThrowStatement) {
|
||||
PsiExpression exception = ((PsiThrowStatement)body).getException();
|
||||
if (exception != null) {
|
||||
myAnchor = RefactoringUtil.getParentStatement(RefactoringUtil.ensureCodeBlock(exception), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void highlight(PsiVariable var) {
|
||||
@@ -264,10 +252,13 @@ class VariableExtractor {
|
||||
return firstOccurrence;
|
||||
}
|
||||
}
|
||||
if (anchor.getParent() instanceof PsiSwitchLabeledRuleStatement) {
|
||||
return ExpressionUtils.getTopLevelExpression(expr);
|
||||
}
|
||||
if (anchor instanceof PsiSwitchLabelStatement) {
|
||||
PsiSwitchStatement statement = ((PsiSwitchLabelStatement)anchor).getEnclosingSwitchStatement();
|
||||
if (statement != null) {
|
||||
return statement;
|
||||
PsiSwitchBlock block = ((PsiSwitchLabelStatement)anchor).getEnclosingSwitchBlock();
|
||||
if (block instanceof PsiSwitchStatement) {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
if (RefactoringUtil.isLoopOrIf(anchor.getParent())) return anchor;
|
||||
|
||||
@@ -43,10 +43,20 @@ class EnsureCodeBlockImpl {
|
||||
(parent instanceof PsiForStatement &&
|
||||
PsiTreeUtil.isAncestor(((PsiForStatement)parent).getInitialization(), expression, true) &&
|
||||
hasNameCollision(((PsiForStatement)parent).getInitialization(), grandParent))) {
|
||||
expression = replace(expression, parent, (old, copy) -> {
|
||||
PsiBlockStatement blockStatement = (PsiBlockStatement)old.replace(factory.createStatementFromText("{}", old));
|
||||
return blockStatement.getCodeBlock().add(copy);
|
||||
});
|
||||
boolean addBreak = parent instanceof PsiExpressionStatement && grandParent instanceof PsiSwitchLabeledRuleStatement &&
|
||||
((PsiSwitchLabeledRuleStatement)grandParent).getEnclosingSwitchBlock() instanceof PsiSwitchExpression;
|
||||
if (addBreak) {
|
||||
expression = replace(expression, parent, (old, copy) -> {
|
||||
PsiBlockStatement block = (PsiBlockStatement)old.replace(factory.createStatementFromText("{break a;}", old));
|
||||
PsiExpression copyExpression = ((PsiBreakStatement)block.getCodeBlock().getStatements()[0]).getExpression();
|
||||
return Objects.requireNonNull(copyExpression).replace(((PsiExpressionStatement)copy).getExpression());
|
||||
});
|
||||
} else {
|
||||
expression = replace(expression, parent, (old, copy) -> {
|
||||
PsiBlockStatement blockStatement = (PsiBlockStatement)old.replace(factory.createStatementFromText("{}", old));
|
||||
return blockStatement.getCodeBlock().add(copy);
|
||||
});
|
||||
}
|
||||
parent = RefactoringUtil.getParentStatement(expression, false);
|
||||
}
|
||||
}
|
||||
@@ -81,24 +91,6 @@ class EnsureCodeBlockImpl {
|
||||
if (parent instanceof PsiReturnStatement && condition != null) {
|
||||
return replace(expression, parent, (oldParent, copy) -> splitReturn((PsiReturnStatement)oldParent, condition, operand));
|
||||
}
|
||||
if (parent instanceof PsiSwitchLabeledRuleStatement) {
|
||||
PsiStatement body = ((PsiSwitchLabeledRuleStatement)parent).getBody();
|
||||
boolean addBreak = ((PsiSwitchLabeledRuleStatement)parent).getEnclosingSwitchBlock() instanceof PsiSwitchExpression;
|
||||
if (body instanceof PsiExpressionStatement && addBreak) {
|
||||
PsiExpression resultExpression = ((PsiExpressionStatement)body).getExpression();
|
||||
return replace(expression, body, (old, copy) -> {
|
||||
PsiBlockStatement block = (PsiBlockStatement)old.replace(factory.createStatementFromText("{break a;}", resultExpression));
|
||||
PsiExpression copyExpression = ((PsiBreakStatement)block.getCodeBlock().getStatements()[0]).getExpression();
|
||||
return Objects.requireNonNull(copyExpression).replace(((PsiExpressionStatement)copy).getExpression());
|
||||
});
|
||||
}
|
||||
else if (body instanceof PsiThrowStatement || body != null && !addBreak) {
|
||||
return replace(expression, body, (old, copy) -> {
|
||||
PsiBlockStatement block = (PsiBlockStatement)old.replace(factory.createStatementFromText("{}", body));
|
||||
return block.getCodeBlock().add(copy);
|
||||
});
|
||||
}
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ public class RefactoringUtil {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
PsiElement parentStatement = parent;
|
||||
while (parent instanceof PsiStatement) {
|
||||
while (parent instanceof PsiStatement && !(parent instanceof PsiSwitchLabeledRuleStatement)) {
|
||||
if (!skipScopingStatements && ((parent instanceof PsiForStatement && parentStatement == ((PsiForStatement)parent).getBody()) || (
|
||||
parent instanceof PsiForeachStatement && parentStatement == ((PsiForeachStatement)parent).getBody()) || (
|
||||
parent instanceof PsiWhileStatement && parentStatement == ((PsiWhileStatement)parent).getBody()) || (
|
||||
|
||||
Reference in New Issue
Block a user