Search in sources :

Example 11 with PsiReferenceExpression

use of com.intellij.psi.PsiReferenceExpression in project oxy-template-support-plugin by mutant-industries.

the class LiteralJsMacroReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
            PsiMethodCallExpression callExpression;
            PsiNewExpression newMacroCallExpression;
            if (!(literalExpression.getValue() instanceof String)) {
                return PsiReference.EMPTY_ARRAY;
            }
            if ((newMacroCallExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiNewExpressionImpl.class)) != null && newMacroCallExpression.getClassReference() != null) {
                if (MacroCall.class.getName().equals(newMacroCallExpression.getClassReference().getQualifiedName())) {
                    return new MacroReferenceSet(literalExpression).getAllReferences();
                }
            } else if ((callExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiMethodCallExpression.class)) != null) {
                PsiExpression[] parameters = callExpression.getArgumentList().getExpressions();
                PsiReferenceExpression expression = callExpression.getMethodExpression();
                String callText = expression.getText();
                if (// TODO type check...
                callText.contains("ageUpdater.update")) {
                    if (parameters.length > 0 && literalExpression.isEquivalentTo(parameters[0])) {
                    // TODO template path reference
                    } else if (parameters.length > 1 && literalExpression.isEquivalentTo(parameters[1])) {
                        return new MacroReferenceSet(literalExpression).getAllReferences();
                    }
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) MacroReferenceSet(ool.intellij.plugin.psi.reference.MacroReferenceSet) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) NotNull(org.jetbrains.annotations.NotNull) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiReferenceProvider(com.intellij.psi.PsiReferenceProvider) PsiNewExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiNewExpressionImpl) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PsiReferenceExpression

use of com.intellij.psi.PsiReferenceExpression in project Just-Another-Android-App by athkalia.

the class AndroidLogDetector method visitMethod.

@Override
public void visitMethod(@Nonnull JavaContext javaContext, JavaElementVisitor visitor, @Nonnull PsiMethodCallExpression call, PsiMethod method) {
    PsiReferenceExpression methodExpression = call.getMethodExpression();
    String fullyQualifiedMethodName = methodExpression.getQualifiedName();
    if (fullyQualifiedMethodName.startsWith("android.util.Log.")) {
        javaContext.report(ISSUE, javaContext.getLocation(methodExpression), ISSUE.getBriefDescription(TextFormat.TEXT));
    }
}
Also used : PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression)

Example 13 with PsiReferenceExpression

use of com.intellij.psi.PsiReferenceExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method visitMethod.

@Override
public void visitMethod(JavaContext context, JavaElementVisitor visitor, PsiMethodCallExpression call, PsiMethod method) {
    PsiReferenceExpression methodExpression = call.getMethodExpression();
    String fullyQualifiedMethodName = methodExpression.getQualifiedName();
    if ("java.lang.String.format".equals(fullyQualifiedMethodName)) {
        checkNestedStringFormat(context, call);
        return;
    }
    if (fullyQualifiedMethodName.startsWith("timber.log.Timber.tag")) {
        checkTagLength(context, call);
        return;
    }
    if (fullyQualifiedMethodName.startsWith("android.util.Log.")) {
        context.report(ISSUE_LOG, methodExpression, context.getLocation(methodExpression), "Using 'Log' instead of 'Timber'");
        return;
    }
    // Handles Timber.X(..) and Timber.tag(..).X(..) where X in (v|d|i|w|e|wtf).
    if (fullyQualifiedMethodName.startsWith("timber.log.Timber.")) {
        checkMethodArguments(context, call);
        checkFormatArguments(context, call);
        checkExceptionLogging(context, call);
        return;
    }
}
Also used : PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression)

Example 14 with PsiReferenceExpression

use of com.intellij.psi.PsiReferenceExpression in project android by JetBrains.

the class AndroidColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String tagName = tag.getName();
        if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
            DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
                String value = tag.getValue().getText().trim();
                annotateXml(element, holder, value);
            }
        } else if (TAG_ITEM.equals(tagName)) {
            XmlTagValue value = tag.getValue();
            String text = value.getText();
            annotateXml(element, holder, text);
        }
    } else if (element instanceof XmlAttributeValue) {
        XmlAttributeValue v = (XmlAttributeValue) element;
        String value = v.getValue();
        if (value == null || value.isEmpty()) {
            return;
        }
        annotateXml(element, holder, value);
    } else if (element instanceof PsiReferenceExpression) {
        ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
        if (referenceType != ResourceReferenceType.NONE) {
            // (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
            // same expression, which would result in both elements getting annotated and the icon showing up
            // in the gutter twice. Instead we only count the outer one.
            ResourceType type = AndroidPsiUtils.getResourceType(element);
            if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
                String name = AndroidPsiUtils.getResourceName(element);
                annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
            }
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) XmlTagValue(com.intellij.psi.xml.XmlTagValue) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ResourceReferenceType(com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 15 with PsiReferenceExpression

use of com.intellij.psi.PsiReferenceExpression in project android by JetBrains.

the class AndroidJavaCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet) {
    super.fillCompletionVariants(parameters, resultSet);
    final PsiElement position = parameters.getPosition();
    final AndroidFacet facet = AndroidFacet.getInstance(position);
    if (facet == null) {
        return;
    }
    if (AndroidMavenUtil.isMavenizedModule(facet.getModule())) {
        resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

            @Override
            public void consume(CompletionResult result) {
                final Object obj = result.getLookupElement().getObject();
                if (obj instanceof PsiClass) {
                    final String qName = ((PsiClass) obj).getQualifiedName();
                    if (qName != null && !isAllowedInAndroid(qName)) {
                        return;
                    }
                }
                resultSet.passResult(result);
            }
        });
    }
    // Filter out private resources when completing R.type.name expressions, if any
    if (position.getParent() instanceof PsiReferenceExpression) {
        PsiReferenceExpression ref = (PsiReferenceExpression) position.getParent();
        if (ref.getQualifierExpression() != null && ref.getQualifierExpression() instanceof PsiReferenceExpression) {
            PsiReferenceExpression ref2 = (PsiReferenceExpression) ref.getQualifierExpression();
            if (ref2.getQualifierExpression() instanceof PsiReferenceExpression) {
                PsiReferenceExpression ref3 = (PsiReferenceExpression) ref2.getQualifierExpression();
                if (ref3.getQualifierExpression() == null && R_CLASS.equals(ref3.getReferenceName())) {
                    filterPrivateResources(parameters, resultSet, facet);
                }
            }
        }
    }
}
Also used : CompletionResult(com.intellij.codeInsight.completion.CompletionResult) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)25 PsiElement (com.intellij.psi.PsiElement)15 PsiExpression (com.intellij.psi.PsiExpression)13 PsiField (com.intellij.psi.PsiField)9 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)9 PsiReference (com.intellij.psi.PsiReference)7 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 Nullable (com.android.annotations.Nullable)5 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)5 PsiClass (com.intellij.psi.PsiClass)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)5 PsiMethod (com.intellij.psi.PsiMethod)5 ResourceType (com.android.resources.ResourceType)4 PsiStatement (com.intellij.psi.PsiStatement)4 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)3 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)3 PsiParenthesizedExpression (com.intellij.psi.PsiParenthesizedExpression)3 NonNls (org.jetbrains.annotations.NonNls)3