use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method inlineMethodSettings.
@Nullable
public static InlineHandler.Settings inlineMethodSettings(GrMethod method, Editor editor, boolean invokedOnReference) {
final Project project = method.getProject();
if (method.isConstructor()) {
String message = GroovyRefactoringBundle.message("refactoring.cannot.be.applied.to.constructors", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
if (invokedOnReference) {
PsiReference reference = editor != null ? TargetElementUtil.findReference(editor, editor.getCaretModel().getOffset()) : null;
if (reference == null)
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
PsiElement element = reference.getElement();
if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression)) {
String message = GroovyRefactoringBundle.message("refactoring.is.available.only.for.method.calls", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
GrCallExpression call = (GrCallExpression) element.getParent();
if (PsiTreeUtil.getParentOfType(element, GrParameter.class) != null) {
String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.in.parameter.initializers", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
GroovyRefactoringUtil.highlightOccurrences(project, editor, new GrExpression[] { call });
if (hasBadReturns(method) && !isTailMethodCall(call)) {
String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
} else {
if (hasBadReturns(method)) {
String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
}
if (method.getBlock() == null) {
String message = method.hasModifierProperty(PsiModifier.ABSTRACT) ? GroovyRefactoringBundle.message("refactoring.cannot.be.applied.to.abstract.methods", REFACTORING_NAME) : GroovyRefactoringBundle.message("refactoring.cannot.be.applied.no.sources.attached", REFACTORING_NAME);
showErrorMessage(message, project, editor);
return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
}
return inlineMethodDialogResult(method, project, invokedOnReference);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class GroovyMethodInliner method inlineUsage.
@Override
public void inlineUsage(@NotNull UsageInfo usage, @NotNull PsiElement referenced) {
PsiElement element = usage.getElement();
if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression))
return;
final Editor editor = getCurrentEditorIfApplicable(element);
GrCallExpression call = (GrCallExpression) element.getParent();
RangeMarker marker = inlineReferenceImpl(call, myMethod, isOnExpressionOrReturnPlace(call), GroovyInlineMethodUtil.isTailMethodCall(call), editor);
// highlight replaced result
if (marker != null) {
Project project = referenced.getProject();
TextRange range = TextRange.create(marker);
GroovyRefactoringUtil.highlightOccurrencesByRanges(project, editor, new TextRange[] { range });
WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
if (editor != null) {
editor.getCaretModel().moveToOffset(marker.getEndOffset());
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project android by JetBrains.
the class GradleFilePsiMerger method mergePsi.
private static void mergePsi(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
Set<PsiElement> destinationChildren = new HashSet<>();
destinationChildren.addAll(Arrays.asList(toRoot.getChildren()));
// If both toRoot and fromRoot are call expressions
if (toRoot instanceof GrCallExpression && fromRoot instanceof GrCallExpression) {
PsiElement[] fromArguments = fromRoot.getLastChild().getChildren();
PsiElement[] toArguments = toRoot.getLastChild().getChildren();
// and both have only one argument and that argument is a literal
if (toArguments.length == 1 && fromArguments.length == 1 && toArguments[0] instanceof GrLiteral && fromArguments[0] instanceof GrLiteral) {
// End this branch by replacing the old literal with the new
toArguments[0].replace(fromArguments[0]);
return;
}
}
// Do an element-wise (disregarding order) child comparison
for (PsiElement child : fromRoot.getChildren()) {
PsiElement destination = findEquivalentElement(destinationChildren, child);
if (destination == null) {
if (destinationChildren.isEmpty()) {
toRoot.add(child);
} else {
toRoot.addBefore(child, toRoot.getLastChild());
}
// And we're done for this branch
} else if (child.getFirstChild() != null && child.getFirstChild().getText().equalsIgnoreCase(GradleFileMergers.DEPENDENCIES) && destination.getFirstChild() != null && destination.getFirstChild().getText().equalsIgnoreCase(GradleFileMergers.DEPENDENCIES)) {
// Special case dependencies
// The last child of the dependencies method call is the closable block
mergeDependencies(child.getLastChild(), destination.getLastChild(), project, supportLibVersionFilter);
} else {
mergePsi(child, destination, project, supportLibVersionFilter);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class MultipleRepositoryUrlsFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
if (statements.size() <= 1)
return;
statements.remove(0);
List<PsiElement> elements = new ArrayList<>(statements);
for (GrCallExpression statement : statements) {
PsiElement newLineCandidate = statement.getNextSibling();
if (PsiUtil.isNewLine(newLineCandidate)) {
elements.add(newLineCandidate);
}
}
myClosure.removeElements(elements.toArray(new PsiElement[elements.size()]));
GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
if (closableBlock == null)
return;
GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
for (GrCallExpression statement : statements) {
closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression in project intellij-community by JetBrains.
the class ResolveUtil method getCallVariants.
public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
final PsiElement parent = place.getParent();
GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
if (parent instanceof GrCallExpression) {
variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
} else if (parent instanceof GrConstructorInvocation) {
final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
if (clazz != null) {
final PsiMethod[] constructors = clazz.getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (parent instanceof GrAnonymousClassDefinition) {
final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
if (element instanceof PsiClass) {
final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (place instanceof GrReferenceExpression) {
variants = ((GrReferenceExpression) place).getSameNameVariants();
}
return variants;
}
Aggregations