use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class WhileExprSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
GrWhileStatement whileStatement = (GrWhileStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("while(a){4\n}", context);
replaceToOldExpression((GrExpression) whileStatement.getCondition(), expression);
whileStatement = expression.replaceWithStatement(whileStatement);
GrStatement body = whileStatement.getBody();
assert body instanceof GrBlockStatement;
GrStatement[] statements = ((GrBlockStatement) body).getBlock().getStatements();
assert statements.length > 0;
GrStatement statement = statements[0];
int offset = statement.getTextRange().getStartOffset();
statement.getNode().getTreeParent().removeChild(statement.getNode());
return new TextRange(offset, offset);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project android by JetBrains.
the class Dependency method getGroovyElements.
@Override
@NotNull
public List<PsiElement> getGroovyElements(@NotNull GroovyPsiElementFactory factory) {
String extraGroovyCode;
switch(type) {
case EXTERNAL:
if (extraClosure != null) {
extraGroovyCode = "(" + escapeAndQuote(data) + ")";
} else {
extraGroovyCode = " " + escapeAndQuote(data);
}
break;
case MODULE:
if (data instanceof Map) {
extraGroovyCode = " project(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
} else {
extraGroovyCode = " project(" + escapeAndQuote(data) + ")";
}
if (extraClosure != null) {
// If there's a closure with exclusion rules, then we need extra parentheses:
// compile project(':foo') { ... } is not valid Groovy syntax
// compile(project(':foo')) { ... } is correct
extraGroovyCode = "(" + extraGroovyCode.substring(1) + ")";
}
break;
case FILES:
extraGroovyCode = " files(" + escapeAndQuote(data) + ")";
break;
case FILETREE:
extraGroovyCode = " fileTree(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
break;
default:
extraGroovyCode = "";
break;
}
GrStatement statement = factory.createStatementFromText(scope.getGroovyMethodCall() + extraGroovyCode);
if (statement instanceof GrMethodCall && extraClosure != null) {
statement.add(factory.createClosureFromText(extraClosure));
}
return ImmutableList.of((PsiElement) statement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.statements.GrStatement in project intellij-community by JetBrains.
the class GrUnnecessarySemicolonInspection method isSemicolonNecessary.
private static boolean isSemicolonNecessary(@NotNull PsiElement semicolon) {
if (semicolon.getParent() instanceof GrTraditionalForClause)
return true;
PsiElement prevSibling = PsiUtil.skipSet(semicolon, false, BACKWARD_SET);
PsiElement nextSibling = PsiUtil.skipSet(semicolon, true, FORWARD_SET);
return prevSibling instanceof GrStatement && (nextSibling instanceof GrStatement || nextSibling != null && nextSibling.getNextSibling() instanceof GrClosableBlock);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class ConditionalUtils method stripBraces.
public static GrStatement stripBraces(GrStatement branch) {
if (branch instanceof GrBlockStatement) {
final GrBlockStatement block = (GrBlockStatement) branch;
final GrStatement[] statements = block.getBlock().getStatements();
if (statements.length == 1) {
return statements[0];
} else {
return block;
}
} else {
return branch;
}
}
Aggregations