use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class SpockUtils method createVariableMap.
// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block == null)
return Collections.emptyMap();
PsiElement elementUnderLabel = null;
PsiElement elementAfterLabel = null;
main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof GrLabeledStatement) {
GrLabeledStatement l = (GrLabeledStatement) e;
elementAfterLabel = l.getNextSibling();
while (true) {
GrStatement statement = l.getStatement();
if ("where".equals(l.getName())) {
elementUnderLabel = statement;
break main;
}
if (statement instanceof GrLabeledStatement) {
l = (GrLabeledStatement) statement;
continue;
}
break;
}
}
}
if (elementUnderLabel == null)
return Collections.emptyMap();
Map<String, SpockVariableDescriptor> res = new HashMap<>();
PsiElement e = elementUnderLabel;
while (e != null) {
if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
GrBinaryExpression shift = (GrBinaryExpression) e;
GrExpression leftOperand = shift.getLeftOperand();
GrExpression rightOperand = shift.getRightOperand();
if (leftOperand instanceof GrReferenceExpression) {
String name = getNameByReference(leftOperand);
if (name != null) {
SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
descriptor.addExpressionOfCollection(rightOperand);
res.put(name, descriptor);
}
} else if (leftOperand instanceof GrListOrMap) {
GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
if (rightOperand instanceof GrListOrMap) {
for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
if (expression instanceof GrListOrMap) {
add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
} else {
for (SpockVariableDescriptor variable : variables) {
if (variable != null) {
variable.addExpressionOfCollection(expression);
}
}
}
}
}
}
} else if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
GrExpression lValue = assExpr.getLValue();
String name = getNameByReference(lValue);
if (name != null) {
res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
}
} else if (isOrStatement(e)) {
// See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
List<GrExpression> variableDefinitions = new ArrayList<>();
splitOr(variableDefinitions, (GrExpression) e);
SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
List<GrExpression> row = new ArrayList<>();
PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
while (isOrStatement(rowElement)) {
row.clear();
splitOr(row, (GrExpression) rowElement);
add(variables, row);
rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
}
e = rowElement;
continue;
}
e = getNext(e, elementUnderLabel, elementAfterLabel);
}
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ControlFlowUtils method openBlockCompletesWithStatement.
public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
GroovyPsiElement elementToCheck = statement;
while (true) {
if (elementToCheck == null)
return false;
final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
if (container == null)
return false;
if (isLoop(container))
return false;
if (container instanceof GrCaseSection) {
final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
final GrCaseSection[] sections = switchStatement.getCaseSections();
if (container == sections[sections.length - 1])
return false;
}
if (container instanceof GrCodeBlock) {
if (elementToCheck instanceof GrStatement) {
final GrCodeBlock codeBlock = (GrCodeBlock) container;
if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
return false;
}
}
if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
if (container.equals(body)) {
return true;
}
elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
} else {
elementToCheck = container;
}
} else {
elementToCheck = container;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ControlFlowUtils method tryStatementMayReturnNormally.
private static boolean tryStatementMayReturnNormally(@NotNull GrTryCatchStatement tryStatement) {
final GrFinallyClause finallyBlock = tryStatement.getFinallyClause();
if (finallyBlock != null) {
if (!openBlockMayCompleteNormally(finallyBlock.getBody())) {
return false;
}
}
final GrOpenBlock tryBlock = tryStatement.getTryBlock();
if (openBlockMayCompleteNormally(tryBlock)) {
return true;
}
for (GrCatchClause catchClause : tryStatement.getCatchClauses()) {
if (openBlockMayCompleteNormally(catchClause.getBody())) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock 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.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class EquivalenceChecker method blockStatementsAreEquivalent.
private static boolean blockStatementsAreEquivalent(@NotNull GrBlockStatement statement1, @NotNull GrBlockStatement statement2) {
final GrOpenBlock block1 = statement1.getBlock();
final GrOpenBlock block2 = statement2.getBlock();
return openBlocksAreEquivalent(block1, block2);
}
Aggregations