use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitUnaryExpression.
@Override
public void visitUnaryExpression(@NotNull GrUnaryExpression expression) {
final boolean postfix = expression.isPostfix();
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
final GrExpression operand = expression.getOperand();
IElementType opType = expression.getOperationTokenType();
if (resolved instanceof PsiMethod) {
if (opType == GroovyTokenTypes.mINC || opType == GroovyTokenTypes.mDEC) {
if (!postfix || expression.getParent() instanceof GrStatementOwner || expression.getParent() instanceof GrControlStatement) {
if (generatePrefixIncDec((PsiMethod) resolved, operand, expression))
return;
}
}
if (operand != null && shouldNotReplaceOperatorWithMethod(operand.getType(), null, expression.getOperationTokenType())) {
writeSimpleUnary(operand, expression, this);
} else {
if (opType == GroovyTokenTypes.mLNOT) {
builder.append('!');
}
invokeMethodOn(((PsiMethod) resolved), operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
}
} else if (operand != null) {
if (postfix) {
operand.accept(this);
builder.append(expression.getOperationToken().getText());
} else {
builder.append(expression.getOperationToken().getText());
operand.accept(this);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GroovyCompletionData method isControlStructure.
private static boolean isControlStructure(PsiElement context) {
final int offset = context.getTextRange().getStartOffset();
PsiElement prevSibling = context.getPrevSibling();
if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
}
if (GroovyCompletionUtil.isNewStatement(context, true)) {
final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
return true;
}
}
if (context.getParent() != null) {
PsiElement parent = context.getParent();
if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
return true;
}
if (parent instanceof GrReferenceExpression) {
PsiElement superParent = parent.getParent();
if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
return true;
}
}
return false;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GrBracesSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
final PsiElement e0 = elements[0];
final PsiElement parent = e0.getParent();
final GrCodeBlock block;
if (parent instanceof GrControlStatement) {
block = factory.createMethodBodyFromText("\n");
final PsiElement prev = e0.getPrevSibling();
if (prev != null && prev.getNode().getElementType().equals(GroovyTokenTypes.mNLS)) {
final ASTNode parentNode = e0.getParent().getNode();
parentNode.addLeaf(TokenType.WHITE_SPACE, " ", prev.getNode());
parentNode.removeChild(prev.getNode());
}
} else {
block = factory.createClosureFromText("{}");
}
GroovyManyStatementsSurrounder.addStatements(block, elements);
return block;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GroovyFix method replaceStatement.
/**
* unwraps surrounding blocks from newStatement.
*/
protected static void replaceStatement(GrStatement oldStatement, GrStatement newStatement) throws IncorrectOperationException {
if (newStatement instanceof GrBlockStatement) {
GrBlockStatement blockStatement = (GrBlockStatement) newStatement;
final GrOpenBlock openBlock = blockStatement.getBlock();
final GrStatement[] statements = openBlock.getStatements();
if (statements.length == 0) {
oldStatement.removeStatement();
} else {
final PsiElement parent = oldStatement.getParent();
if (parent instanceof GrStatementOwner) {
GrStatementOwner statementOwner = (GrStatementOwner) parent;
for (GrStatement statement : statements) {
statementOwner.addStatementBefore(statement, oldStatement);
}
oldStatement.removeStatement();
} else if (parent instanceof GrControlStatement) {
oldStatement.replace(newStatement);
}
}
} else {
oldStatement.replaceWithStatement(newStatement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GenerationUtil method writeStatement.
static void writeStatement(@NotNull StringBuilder codeBlockBuilder, @NotNull StringBuilder statementBuilder, @Nullable GrStatement statement, @Nullable ExpressionContext context) {
final PsiElement parent = statement == null ? null : statement.getParent();
final boolean addParentheses;
if (statement == null) {
addParentheses = context != null && context.shouldInsertCurlyBrackets();
} else {
addParentheses = context != null && (context.shouldInsertCurlyBrackets() || !context.myStatements.isEmpty()) && parent instanceof GrControlStatement;
}
if (addParentheses) {
codeBlockBuilder.append("{\n");
}
if (context != null) {
insertStatementFromContextBefore(codeBlockBuilder, context);
}
codeBlockBuilder.append(statementBuilder);
if (addParentheses) {
codeBlockBuilder.append("}\n");
}
}
Aggregations