Search in sources :

Example 1 with GrLightVariable

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable in project intellij-community by JetBrains.

the class GroovyCodeFragmentFactory method externalParameters.

public static Pair<Map<String, String>, GroovyFile> externalParameters(String text, @NotNull final PsiElement context) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
    final GroovyFile toEval = factory.createGroovyFile(text, false, context);
    final GrClosableBlock closure = PsiTreeUtil.getParentOfType(context, GrClosableBlock.class);
    final Map<String, String> parameters = new THashMap<>();
    final Map<GrExpression, String> replacements = new HashMap<>();
    toEval.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (PsiUtil.isThisReference(referenceExpression) || PsiUtil.isSuperReference(referenceExpression)) {
                replaceWithReference(referenceExpression, "delegate");
                return;
            }
            PsiElement resolved = referenceExpression.resolve();
            if (resolved instanceof PsiMember && (resolved instanceof PsiClass || ((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))) {
                String qName = com.intellij.psi.util.PsiUtil.getMemberQualifiedName((PsiMember) resolved);
                if (qName != null && qName.contains(".") && !referenceExpression.isQualified()) {
                    replaceWithReference(referenceExpression, qName);
                    return;
                }
            }
            if (shouldDelegate(referenceExpression, resolved)) {
                replaceWithReference(referenceExpression, "delegate." + referenceExpression.getReferenceName());
                return;
            }
            if (resolved instanceof GrVariable && !(resolved instanceof GrField) && !PsiTreeUtil.isAncestor(toEval, resolved, false)) {
                final String name = ((GrVariable) resolved).getName();
                if (resolved instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(toEval, ((ClosureSyntheticParameter) resolved).getClosure(), false)) {
                    return;
                }
                if (resolved instanceof GrBindingVariable && !PsiTreeUtil.isAncestor(resolved.getContainingFile(), toEval, false)) {
                    return;
                }
                String value;
                if (closure != null && PsiTreeUtil.findCommonParent(resolved, closure) != closure && !(resolved instanceof ClosureSyntheticParameter)) {
                    // Evaluating inside closure for outer variable definitions
                    // All non-local variables are accessed by references
                    value = "this." + name;
                } else {
                    value = name;
                }
                parameters.put(name, value);
                return;
            }
            if (resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter && !(resolved instanceof GrParameter)) {
                String name = referenceExpression.getReferenceName();
                parameters.put(name, name);
            }
        }

        private boolean shouldDelegate(GrReferenceExpression referenceExpression, @Nullable PsiElement resolved) {
            if (referenceExpression.isQualified()) {
                return false;
            }
            if (resolved instanceof GrField) {
                return true;
            }
            if (resolved instanceof PsiMethod) {
                String methodName = ((PsiMethod) resolved).getName();
                if (closure != null && "getDelegate".equals(methodName) || "call".equals(methodName)) {
                    return true;
                }
            }
            return closure != null && resolved instanceof GrLightVariable && "owner".equals(((GrLightVariable) resolved).getName());
        }

        private void replaceWithReference(GrExpression expr, final String exprText) {
            replacements.put(expr, exprText);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            super.visitCodeReferenceElement(refElement);
            if (refElement.getQualifier() == null) {
                PsiElement resolved = refElement.resolve();
                if (resolved instanceof PsiClass) {
                    String qName = ((PsiClass) resolved).getQualifiedName();
                    if (qName != null) {
                        int dotIndex = qName.lastIndexOf(".");
                        if (dotIndex < 0)
                            return;
                        String packageName = qName.substring(0, dotIndex);
                        refElement.setQualifier(factory.createReferenceElementFromText(packageName));
                    }
                }
            }
        }
    });
    for (GrExpression expression : replacements.keySet()) {
        expression.replaceWithExpression(factory.createExpressionFromText(replacements.get(expression)), false);
    }
    return Pair.create(parameters, toEval);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) THashMap(gnu.trove.THashMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) THashMap(gnu.trove.THashMap) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 2 with GrLightVariable

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable in project intellij-community by JetBrains.

the class GrClosableBlockImpl method getOwner.

private PsiVariable getOwner() {
    return CachedValuesManager.getCachedValue(this, () -> {
        final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiType type = null;
        if (context instanceof GrTypeDefinition) {
            type = factory.createType((PsiClass) context);
        } else if (context instanceof GrClosableBlock) {
            type = GrClosureType.create((GrClosableBlock) context, true);
        } else if (context instanceof GroovyFile) {
            final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
            if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
                type = factory.createType(scriptClass);
        }
        if (type == null) {
            type = TypesUtil.getJavaLangObject(this);
        }
        PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
        return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 3 with GrLightVariable

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable in project android by JetBrains.

the class AndroidDslContributor method resolveToMethodWithClosure.

private static void resolveToMethodWithClosure(PsiElement place, PsiElement resolveToElement, String closureTypeFqcn, PsiScopeProcessor processor, ResolveState state) {
    if (place.getParent() instanceof GrMethodCallExpression) {
        GrLightMethodBuilder methodWithClosure = GradleResolverUtil.createMethodWithClosure(place.getText(), closureTypeFqcn, null, place);
        if (methodWithClosure != null) {
            processor.execute(methodWithClosure, state);
            methodWithClosure.setNavigationElement(resolveToElement);
        }
    } else if (place.getParent() instanceof GrReferenceExpression) {
        GrLightVariable variable = new GrLightVariable(place.getManager(), place.getText(), closureTypeFqcn, place);
        processor.execute(variable, state);
    }
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 4 with GrLightVariable

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable in project intellij-community by JetBrains.

the class GroovyShellCodeFragment method addVariable.

public void addVariable(String name, GrExpression expr) {
    PsiType type = expr.getType();
    if (type instanceof GrClassReferenceType) {
        final PsiClassType.ClassResolveResult resolveResult = ((GrClassReferenceType) type).resolveGenerics();
        final PsiClass psiClass = resolveResult.getElement();
        type = psiClass == null ? null : new PsiImmediateClassType(psiClass, resolveResult.getSubstitutor());
    }
    if (type != null) {
        myVariables.put(name, new GrLightVariable(getManager(), name, type, this));
    }
}
Also used : PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) GrClassReferenceType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable)

Aggregations

GrLightVariable (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable)4 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 THashMap (gnu.trove.THashMap)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1 GrClassReferenceType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType)1 ClosureSyntheticParameter (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter)1 GrBindingVariable (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable)1 GrLightMethodBuilder (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)1