use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyStatementSelectioner method inferBlockRange.
private static TextRange inferBlockRange(PsiElement first, PsiElement last) {
while (true) {
PsiElement prev = first.getPrevSibling();
prev = skipWhitespaceBack(prev);
if (isOneLineFeed(prev))
prev = prev.getPrevSibling();
prev = skipWhitespaceBack(prev);
if (prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mSEMI || prev instanceof GrStatement) {
first = prev;
} else {
break;
}
}
while (true) {
PsiElement next = last.getNextSibling();
next = skipWhitespacesForward(next);
if (isOneLineFeed(next))
next = next.getNextSibling();
next = skipWhitespacesForward(next);
if (next != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI || next instanceof GrStatement) {
last = next;
} else {
break;
}
}
return new TextRange(first.getTextRange().getStartOffset(), last.getTextRange().getEndOffset());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyBlockGenerator method calculateAlignments.
private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
List<GrStatement> currentGroup = null;
boolean spock = true;
for (ASTNode child : children) {
PsiElement psi = child.getPsi();
if (psi instanceof GrLabeledStatement) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = true;
} else if (currentGroup != null && spock && isTablePart(psi)) {
currentGroup.add((GrStatement) psi);
} else if (psi instanceof GrVariableDeclaration) {
GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
if (variables.length > 0) {
if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = false;
}
currentGroup.add((GrStatement) psi);
}
} else {
if (shouldSkip(classLevel, psi))
continue;
alignGroup(currentGroup, spock, classLevel);
currentGroup = null;
}
}
if (currentGroup != null) {
alignGroup(currentGroup, spock, classLevel);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class ForToEachIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrForStatement parentStatement = (GrForStatement) element;
final GrForInClause clause = (GrForInClause) parentStatement.getClause();
final GrVariable var = clause.getDeclaredVariable();
final GrStatement body = parentStatement.getBody();
final String bodyText;
if (body instanceof GrBlockStatement) {
final String text = body.getText();
bodyText = text.substring(1, text.length() - 1);
} else {
bodyText = body.getText();
}
GrExpression collection = clause.getIteratedExpression();
assert collection != null;
@NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
parentStatement.replaceWithStatement(eachExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GrRedundantElseIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) parent;
final GrStatement branch = ifStatement.getThenBranch();
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
for (Instruction instruction : flow) {
if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
for (Instruction pred : instruction.allPredecessors()) {
final PsiElement predElement = pred.getElement();
if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
return false;
}
}
}
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyWhileUnwrapper method doUnwrap.
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
GrStatement body = ((GrWhileStatement) element).getBody();
context.extractFromBlockOrSingleStatement(body, element);
context.delete(element);
}
Aggregations