use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.
the class ControlFlowUtils method blockCompletesWithStatement.
public static boolean blockCompletesWithStatement(@NotNull GrBlockStatement body, @NotNull GrStatement statement) {
GrStatement statementToCheck = statement;
while (true) {
if (statementToCheck == null) {
return false;
}
final PsiElement container = statementToCheck.getParent();
if (container == null) {
return false;
}
if (container instanceof GrLoopStatement) {
return false;
} else if (container instanceof GrCaseSection) {
final GrCaseSection caseSection = (GrCaseSection) container;
if (!statementIsLastInBlock(caseSection, statementToCheck))
return false;
final PsiElement parent = container.getParent();
assert parent instanceof GrSwitchStatement;
final GrSwitchStatement switchStatement = (GrSwitchStatement) parent;
final GrCaseSection[] sections = switchStatement.getCaseSections();
if (ArrayUtil.getLastElement(sections) != caseSection) {
return false;
}
} else if (container instanceof GrOpenBlock) {
final GrOpenBlock block = (GrOpenBlock) container;
if (!statementIsLastInBlock(block, statementToCheck))
return false;
final PsiElement parent = block.getParent();
if (parent instanceof GrBlockStatement) {
final GrBlockStatement blockStatement = (GrBlockStatement) parent;
if (blockStatement == body)
return true;
}
} else if (container instanceof GrClosableBlock) {
return false;
}
statementToCheck = getContainingStatement(statementToCheck);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method isLastStatementInCaseSection.
private static boolean isLastStatementInCaseSection(GrCaseSection caseSection, GrSwitchStatement switchStatement) {
final GrCaseSection[] sections = switchStatement.getCaseSections();
final int i = ArrayUtilRt.find(sections, caseSection);
if (i == sections.length - 1) {
return true;
}
for (int j = i + 1; j < sections.length; j++) {
GrCaseSection section = sections[j];
for (GrStatement statement : section.getStatements()) {
if (!(statement instanceof GrBreakStatement)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method findStatementsInRange.
@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
if (!(file instanceof GroovyFileBase))
return PsiElement.EMPTY_ARRAY;
Language language = GroovyLanguage.INSTANCE;
PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
if (element1 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element1)) {
startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element2)) {
endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
if (element1 == null || element2 == null)
return PsiElement.EMPTY_ARRAY;
PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
if (parent == null)
return PsiElement.EMPTY_ARRAY;
while (true) {
if (parent instanceof GrCodeBlock)
break;
if (parent instanceof GroovyFileBase)
break;
if (parent instanceof GrCaseSection)
break;
if (parent instanceof GrStatement) {
parent = parent.getParent();
break;
}
if (parent == null)
return PsiElement.EMPTY_ARRAY;
final PsiElement prev = parent;
parent = parent.getParent();
if (parent instanceof GrCodeBlock && prev instanceof LeafPsiElement) {
//braces
parent = parent.getParent();
}
}
if (!parent.equals(element1)) {
while (!parent.equals(element1.getParent())) {
element1 = element1.getParent();
}
}
if (startOffset != element1.getTextRange().getStartOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (!parent.equals(element2)) {
while (!parent.equals(element2.getParent())) {
element2 = element2.getParent();
}
}
if (endOffset != element2.getTextRange().getEndOffset() && strict)
return PsiElement.EMPTY_ARRAY;
if (parent instanceof GrCodeBlock && parent.getParent() instanceof GrBlockStatement && element1 == ((GrCodeBlock) parent).getLBrace() && element2 == ((GrCodeBlock) parent).getRBrace()) {
return new PsiElement[] { parent.getParent() };
}
// calculate children
PsiElement[] children = PsiElement.EMPTY_ARRAY;
PsiElement psiChild = parent.getFirstChild();
if (psiChild != null) {
List<PsiElement> result = new ArrayList<>();
while (psiChild != null) {
result.add(psiChild);
psiChild = psiChild.getNextSibling();
}
children = PsiUtilCore.toPsiElementArray(result);
}
ArrayList<PsiElement> possibleStatements = new ArrayList<>();
boolean flag = false;
for (PsiElement child : children) {
if (child == element1) {
flag = true;
}
if (flag) {
possibleStatements.add(child);
}
if (child == element2) {
break;
}
}
for (PsiElement element : possibleStatements) {
if (!(element instanceof GrStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment || TokenSets.SEPARATORS.contains(element.getNode().getElementType()))) {
return PsiElement.EMPTY_ARRAY;
}
}
return PsiUtilCore.toPsiElementArray(possibleStatements);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method isCertainlyReturnStatement.
/**
* check whether statement is return (the statement which provides return value) statement of method or closure.
*
* @param st
* @return
*/
public static boolean isCertainlyReturnStatement(GrStatement st) {
final PsiElement parent = st.getParent();
if (parent instanceof GrOpenBlock) {
if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
return false;
PsiElement pparent = parent.getParent();
if (pparent instanceof GrMethod) {
return true;
}
if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
pparent = pparent.getParent();
}
if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
return isCertainlyReturnStatement((GrStatement) pparent);
}
} else if (parent instanceof GrClosableBlock) {
return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
} else if (parent instanceof GroovyFileBase) {
return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
} else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
return isCertainlyReturnStatement((GrStatement) parent);
} else if (parent instanceof GrCaseSection) {
final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
final GrStatement last = ArrayUtil.getLastElement(statements);
final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
return isCertainlyReturnStatement(switchStatement);
} else if (st == last) {
if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
return isCertainlyReturnStatement(switchStatement);
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.
the class ControlFlowUtils method switchStatementMayReturnNormally.
private static boolean switchStatementMayReturnNormally(@NotNull GrSwitchStatement switchStatement) {
if (statementIsBreakTarget(switchStatement)) {
return true;
}
final GrCaseSection[] caseClauses = switchStatement.getCaseSections();
if (ContainerUtil.find(caseClauses, section -> section.isDefault()) == null) {
return true;
}
final GrCaseSection lastClause = caseClauses[caseClauses.length - 1];
final GrStatement[] statements = lastClause.getStatements();
if (statements.length == 0) {
return true;
}
return statementMayCompleteNormally(statements[statements.length - 1]);
}
Aggregations