use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.GrStatement in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method addClosureUsages.
private static void addClosureUsages(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrStatement first, final GrStatement last, GrControlFlowOwner flowOwner) {
flowOwner.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
addUsagesInClosure(imap, omap, closure, first, last);
super.visitClosure(closure);
}
private void addUsagesInClosure(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrClosableBlock closure, final GrStatement first, final GrStatement last) {
closure.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
if (refExpr.isQualified()) {
return;
}
PsiElement resolved = refExpr.resolve();
if (!(resolved instanceof GrVariable)) {
return;
}
GrVariable variable = (GrVariable) resolved;
if (PsiTreeUtil.isAncestor(closure, variable, true)) {
return;
}
if (variable instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(closure, ((ClosureSyntheticParameter) variable).getClosure(), false)) {
return;
}
String name = variable.getName();
if (!(variable instanceof GrField)) {
if (!isInFragment(first, last, resolved)) {
if (isInFragment(first, last, closure)) {
addVariable(name, imap, variable.getManager(), variable.getType());
}
} else {
if (!isInFragment(first, last, closure)) {
addVariable(name, omap, variable.getManager(), variable.getType());
}
}
}
}
});
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class PsiImplUtil method replaceExpression.
@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
PsiElement oldParent = oldExpr.getParent();
if (oldParent == null)
throw new PsiInvalidElementAccessException(oldExpr);
if (!(oldExpr instanceof GrApplicationStatement)) {
newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
}
// Remove unnecessary parentheses
if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
}
//regexes cannot be after identifier , try to replace it with simple string
if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
final PsiElement copy = newExpr.copy();
final GrLiteral regex = getRegexAtTheBeginning(copy);
LOG.assertTrue(regex != null);
final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
if (regex == copy) {
return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
} else {
regex.replace(stringLiteral);
return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
}
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
if (oldParent instanceof GrStringInjection) {
if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
} else {
GrClosableBlock block = factory.createClosureFromText("{foo}");
oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
GrStatement[] statements = block.getStatements();
return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
}
}
if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
GrStringUtil.wrapInjection(stringInjection);
assert stringInjection != null;
final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
return (GrExpression) replaced;
}
//check priorities
if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
if (newExpr != addedParenth) {
return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
}
}
//we should add the expression in arg list
if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
}
newExpr = (GrExpression) oldExpr.replace(newExpr);
// to find target parenthesised expression.
if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
int parentCount = 0;
PsiElement element = oldParent;
while (element != null && !(element instanceof GrCommandArgumentList)) {
if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
break;
if (element instanceof PsiFile)
break;
final PsiElement parent = element.getParent();
if (parent == null)
break;
if (!isFirstChild(element))
break;
element = parent;
parentCount++;
}
if (element instanceof GrCommandArgumentList) {
final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
final PsiElement parent = commandArgList.getParent();
LOG.assertTrue(parent instanceof GrApplicationStatement);
final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
PsiElement result = newCall.getArgumentList().getAllArguments()[0];
for (int i = 0; i < parentCount; i++) {
result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
}
LOG.assertTrue(result instanceof GrParenthesizedExpression);
return (GrExpression) result;
}
}
return newExpr;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GrIfStatementImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
GrStatement elseBranch = getElseBranch();
if (elseBranch != null && child == elseBranch.getNode()) {
PsiElement elseKeywordElement = findChildByType(GroovyTokenTypes.kELSE);
if (elseKeywordElement != null) {
super.deleteChildInternal(elseKeywordElement.getNode());
}
}
super.deleteChildInternal(child);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GrStringUtil method checkGStringInjectionForUnnecessaryBraces.
public static boolean checkGStringInjectionForUnnecessaryBraces(GrStringInjection injection) {
final GrClosableBlock block = injection.getClosableBlock();
if (block == null)
return false;
final GrStatement[] statements = block.getStatements();
if (statements.length != 1)
return false;
if (!(statements[0] instanceof GrReferenceExpression))
return false;
return checkBraceIsUnnecessary(statements[0], injection.getNextSibling());
}
Aggregations