use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement 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.GrForStatement in project intellij-community by JetBrains.
the class GroovySmartEnterProcessor method reformat.
@Override
protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
PsiElement parent = atCaret.getParent();
if (parent instanceof GrCodeBlock) {
final GrCodeBlock block = (GrCodeBlock) parent;
if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
atCaret = block;
}
} else if (parent instanceof GrForStatement || parent instanceof GrSwitchStatement) {
atCaret = parent;
}
super.reformat(atCaret);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement in project intellij-community by JetBrains.
the class GrForBodyFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
GrForStatement forStatement = PsiTreeUtil.getParentOfType(psiElement, GrForStatement.class);
if (forStatement == null)
return;
final Document doc = editor.getDocument();
PsiElement body = forStatement.getBody();
if (body instanceof GrBlockStatement)
return;
if (body != null && startLine(doc, body) == startLine(doc, forStatement))
return;
PsiElement eltToInsertAfter = forStatement.getRParenth();
String text = "{}";
if (eltToInsertAfter == null) {
eltToInsertAfter = forStatement;
text = "){}";
}
doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement in project intellij-community by JetBrains.
the class ForSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrForStatement whileStatement = (GrForStatement) factory.createStatementFromText("for(a in b){\n}", context);
addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
return whileStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement in project intellij-community by JetBrains.
the class ForSurrounder method getSurroundSelectionRange.
@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
assert element instanceof GrForStatement;
GrForClause clause = ((GrForStatement) element).getClause();
int endOffset = element.getTextRange().getEndOffset();
if (clause != null) {
endOffset = clause.getTextRange().getStartOffset();
clause.getParent().getNode().removeChild(clause.getNode());
}
return new TextRange(endOffset, endOffset);
}
Aggregations