use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class ConvertClosureToMethodIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrField field;
if (element.getParent() instanceof GrField) {
field = (GrField) element.getParent();
} else {
final PsiReference ref = element.getReference();
LOG.assertTrue(ref != null);
PsiElement resolved = ref.resolve();
if (resolved instanceof GrAccessorMethod) {
resolved = ((GrAccessorMethod) resolved).getProperty();
}
LOG.assertTrue(resolved instanceof GrField);
field = (GrField) resolved;
}
final HashSet<PsiReference> usages = new HashSet<>();
usages.addAll(ReferencesSearch.search(field).findAll());
final GrAccessorMethod[] getters = field.getGetters();
for (GrAccessorMethod getter : getters) {
usages.addAll(MethodReferencesSearch.search(getter).findAll());
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null) {
usages.addAll(MethodReferencesSearch.search(setter).findAll());
}
final String fieldName = field.getName();
LOG.assertTrue(fieldName != null);
final Collection<PsiElement> fieldUsages = new HashSet<>();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
for (PsiReference usage : usages) {
final PsiElement psiElement = usage.getElement();
if (PsiUtil.isMethodUsage(psiElement))
continue;
if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
} else {
if (psiElement instanceof GrReferenceExpression) {
fieldUsages.add(psiElement);
if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
}
} else if (psiElement instanceof GrArgumentLabel) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
}
}
}
final PsiClass containingClass = field.getContainingClass();
final GrExpression initializer = field.getInitializerGroovy();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
LOG.assertTrue(type instanceof GrClosureType);
final GrSignature signature = ((GrClosureType) type).getSignature();
final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
for (MethodSignature s : signatures) {
final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
if (method != null) {
conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
}
}
if (!conflicts.isEmpty()) {
final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
conflictsDialog.show();
if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
}
execute(field, fieldUsages);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class ConvertJavaStyleArrayIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrClosableBlock block = ((GrMethodCallExpression) element).getClosureArguments()[0];
final String text = block.getText();
int start = block.getLBrace().getStartOffsetInParent() + 1;
int finish = block.getRBrace().getStartOffsetInParent();
String newText = "[" + text.substring(start, finish) + "]";
final GrExpression newExpr = GroovyPsiElementFactory.getInstance(element.getProject()).createExpressionFromText(newText);
((GrMethodCallExpression) element).replaceWithStatement(newExpr);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class ConvertJunitAssertionToAssertStatementIntention method getReplacementStatement.
@Nullable
private static String getReplacementStatement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
PsiClass containingClass = method.getContainingClass();
if (containingClass == null)
return null;
String qualifiedName = containingClass.getQualifiedName();
if (!"junit.framework.Assert".equals(qualifiedName) && !"groovy.util.GroovyTestCase".equals(qualifiedName))
return null;
String[] replacementStatements = ourStatementMap.get(method.getName());
if (replacementStatements == null)
return null;
GrArgumentList argumentList = methodCall.getArgumentList();
if (argumentList.getNamedArguments().length > 0)
return null;
GrExpression[] arguments = argumentList.getExpressionArguments();
if (arguments.length >= replacementStatements.length)
return null;
return replacementStatements[arguments.length];
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class ConvertToDollarSlashRegexIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrLiteral))
return;
String rawText = GrStringUtil.removeQuotes(element.getText());
GrExpression newRegexp = GroovyPsiElementFactory.getInstance(project).createExpressionFromText("$/" + rawText + "/$");
//don't use replaceWithExpression() since it can revert conversion if regexp brakes syntax
element.replace(newRegexp);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrConvertStringToCharIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrExpression cast = GroovyPsiElementFactory.getInstance(project).createExpressionFromText("a as char");
((GrSafeCastExpression) cast).getOperand().replaceWithExpression((GrExpression) element, true);
((GrExpression) element).replaceWithExpression(cast, true);
}
Aggregations