Search in sources :

Example 16 with GrReferenceExpression

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);
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 17 with GrReferenceExpression

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);
}
Also used : TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 18 with GrReferenceExpression

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 };
}
Also used : GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 19 with GrReferenceExpression

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();
}
Also used : ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 20 with GrReferenceExpression

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;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11