use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class ConvertClosureArgToItIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrClosableBlock closure = (GrClosableBlock) element;
final GrParameterList parameterList = closure.getParameterList();
final GrParameter parameter = parameterList.getParameters()[0];
final Set<GrReferenceExpression> referencesToChange = new HashSet<>();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
if (!referenceExpression.getText().equals(parameter.getName())) {
return;
}
final PsiElement referent = referenceExpression.resolve();
if (parameter.equals(referent)) {
referencesToChange.add(referenceExpression);
}
}
};
closure.accept(visitor);
parameter.delete();
for (GrReferenceExpression referenceExpression : referencesToChange) {
PsiImplUtil.replaceExpression("it", referenceExpression);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class CreateMethodFromUsageFix method invokeImpl.
@Override
protected void invokeImpl(Project project, @NotNull PsiClass targetClass) {
final JVMElementFactory factory = JVMElementFactories.getFactory(targetClass.getLanguage(), targetClass.getProject());
assert factory != null;
PsiMethod method = factory.createMethod(getMethodName(), PsiType.VOID);
final GrReferenceExpression ref = getRefExpr();
if (GrStaticChecker.isInStaticContext(ref, targetClass)) {
method.getModifierList().setModifierProperty(PsiModifier.STATIC, true);
}
PsiType[] argTypes = getArgumentTypes();
assert argTypes != null;
ChooseTypeExpression[] paramTypesExpressions = setupParams(method, argTypes, factory);
TypeConstraint[] constraints = getReturnTypeConstraints();
final PsiGenerationInfo<PsiMethod> info = OverrideImplementUtil.createGenerationInfo(method);
info.insert(targetClass, findInsertionAnchor(info, targetClass), false);
method = info.getPsiMember();
if (shouldBeAbstract(targetClass)) {
method.getBody().delete();
if (!targetClass.isInterface()) {
method.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, true);
}
}
final PsiElement context = PsiTreeUtil.getParentOfType(ref, PsiClass.class, PsiMethod.class, PsiFile.class);
IntentionUtils.createTemplateForMethod(argTypes, paramTypesExpressions, method, targetClass, constraints, false, context);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class CreateSetterFromUsageFix method getArgumentTypes.
@Override
protected PsiType[] getArgumentTypes() {
final GrReferenceExpression ref = getRefExpr();
assert PsiUtil.isLValue(ref);
PsiType initializer = TypeInferenceHelper.getInitializerTypeFor(ref);
if (initializer == null || initializer == PsiType.NULL) {
initializer = TypesUtil.getJavaLangObject(ref);
}
return new PsiType[] { initializer };
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class ConvertGStringToStringIntention method convertGStringLiteralToStringLiteral.
public static String convertGStringLiteralToStringLiteral(GrLiteral literal) {
PsiElement child = literal.getFirstChild();
if (child == null)
return literal.getText();
String text;
ArrayList<String> list = new ArrayList<>();
PsiElement prevSibling = null;
PsiElement nextSibling;
do {
text = child.getText();
nextSibling = child.getNextSibling();
if (child instanceof GrStringInjection) {
if (((GrStringInjection) child).getClosableBlock() != null) {
text = prepareClosableBlock(((GrStringInjection) child).getClosableBlock());
} else if (((GrStringInjection) child).getExpression() != null) {
text = prepareExpression(((GrStringInjection) child).getExpression());
} else {
text = child.getText();
}
} else {
text = prepareText(text, prevSibling == null, nextSibling == null, nextSibling instanceof GrClosableBlock || nextSibling instanceof GrReferenceExpression);
}
if (text != null) {
list.add(text);
}
prevSibling = child;
child = child.getNextSibling();
} while (child != null);
StringBuilder builder = new StringBuilder(literal.getTextLength() * 2);
if (list.isEmpty())
return "''";
builder.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
builder.append(" + ").append(list.get(i));
}
return builder.toString();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class MyPredicate method getParameterByArgument.
@Nullable
private static GrParameter getParameterByArgument(GrExpression arg) {
PsiElement parent = PsiUtil.skipParentheses(arg.getParent(), true);
if (!(parent instanceof GrArgumentList))
return null;
final GrArgumentList argList = (GrArgumentList) parent;
parent = parent.getParent();
if (!(parent instanceof GrMethodCall))
return null;
final GrMethodCall methodCall = (GrMethodCall) parent;
final GrExpression expression = methodCall.getInvokedExpression();
if (!(expression instanceof GrReferenceExpression))
return null;
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
if (resolveResult == null)
return null;
GrClosableBlock[] closures = methodCall.getClosureArguments();
final Map<GrExpression, Pair<PsiParameter, PsiType>> mapToParams = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, arg, false, false, argList.getNamedArguments(), argList.getExpressionArguments(), closures);
if (mapToParams == null)
return null;
final Pair<PsiParameter, PsiType> parameterPair = mapToParams.get(arg);
final PsiParameter parameter = parameterPair == null ? null : parameterPair.getFirst();
return parameter instanceof GrParameter ? ((GrParameter) parameter) : null;
}
Aggregations