use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class InvertIfIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement)) {
throw new IncorrectOperationException("Not invoked on an if");
}
GrIfStatement parentIf = (GrIfStatement) parent;
GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
GrExpression condition = parentIf.getCondition();
if (condition == null) {
throw new IncorrectOperationException("Invoked on an if with empty condition");
}
GrExpression negatedCondition = null;
if (condition instanceof GrUnaryExpression) {
GrUnaryExpression unaryCondition = (GrUnaryExpression) condition;
if ("!".equals(unaryCondition.getOperationToken().getText())) {
negatedCondition = stripParenthesis(unaryCondition.getOperand());
}
}
if (negatedCondition == null) {
// Now check whether this is a simple expression
condition = stripParenthesis(condition);
String negatedExpressionText;
if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
negatedExpressionText = "!" + condition.getText();
} else {
negatedExpressionText = "!(" + condition.getText() + ")";
}
negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
}
GrStatement thenBranch = parentIf.getThenBranch();
final boolean thenIsNotEmpty = isNotEmpty(thenBranch);
String newIfText = "if (" + negatedCondition.getText() + ") {}";
if (thenIsNotEmpty) {
newIfText += " else {}";
}
GrIfStatement newIf = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
generateElseBranchTextAndRemoveTailStatements(parentIf, newIf);
if (thenIsNotEmpty) {
final GrStatement elseBranch = newIf.getElseBranch();
assert elseBranch != null;
elseBranch.replaceWithStatement(thenBranch);
}
parentIf.replace(newIf);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterObjectDelegate method createNewParameterInitializerAtCallSite.
@Override
public PsiElement createNewParameterInitializerAtCallSite(PsiElement callExpression, IntroduceParameterObjectClassDescriptor descriptor, List<? extends ParameterInfo> oldMethodParameters, Object substitutor) {
if (callExpression instanceof GrCallExpression) {
final GrArgumentList list = ((GrCallExpression) callExpression).getArgumentList();
if (list == null) {
return null;
}
final GrExpression[] args = list.getExpressionArguments();
final String qualifiedName = StringUtil.getQualifiedName(descriptor.getPackageName(), descriptor.getClassName());
String newExpression = "new " + qualifiedName + '(' + JavaIntroduceParameterObjectDelegate.getMergedArgs(descriptor, oldMethodParameters, args) + ')';
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(callExpression.getProject());
return factory.createExpressionFromText(newExpression, callExpression);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class GroovyMethodInliner method getConflicts.
@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
PsiElement element = reference.getElement();
if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression)) {
final MultiMap<PsiElement, String> map = new MultiMap<>();
map.putValue(element, GroovyRefactoringBundle.message("cannot.inline.reference.0", element.getText()));
return map;
}
GrCallExpression call = (GrCallExpression) element.getParent();
Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(myMethod);
return collectConflicts(call, infos);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class MultipleRepositoryUrlsInspection method findUrlCallExpressions.
@NotNull
static List<GrCallExpression> findUrlCallExpressions(@NotNull GrClosableBlock closure) {
GrCallExpression[] applicationStatements = PsiTreeUtil.getChildrenOfType(closure, GrCallExpression.class);
if (applicationStatements == null)
return Collections.emptyList();
List<GrCallExpression> statements = ContainerUtil.newArrayList();
for (GrCallExpression statement : applicationStatements) {
GrReferenceExpression[] referenceExpressions = PsiTreeUtil.getChildrenOfType(statement, GrReferenceExpression.class);
if (referenceExpressions == null)
continue;
for (GrReferenceExpression expression : referenceExpressions) {
String expressionText = expression.getText();
if ("url".equals(expressionText) || "setUrl".equals(expressionText)) {
statements.add(statement);
}
}
}
return statements;
}
Aggregations