Search in sources :

Example 1 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class IdReferenceProvider method getReferencesByElement.

@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
    if (element instanceof XmlAttributeValue) {
        final XmlExtension extension = XmlExtension.getExtensionByElement(element);
        if (extension != null && extension.hasDynamicComponents(element)) {
            return PsiReference.EMPTY_ARRAY;
        }
        final PsiElement parentElement = element.getParent();
        if (!(parentElement instanceof XmlAttribute))
            return PsiReference.EMPTY_ARRAY;
        final String name = ((XmlAttribute) parentElement).getName();
        final String ns = ((XmlAttribute) parentElement).getParent().getNamespace();
        final boolean jsfNs = Arrays.asList(XmlUtil.JSF_CORE_URIS).contains(ns) || Arrays.asList(XmlUtil.JSF_HTML_URIS).contains(ns);
        if (FOR_ATTR_NAME.equals(name)) {
            return new PsiReference[] { jsfNs && element.getText().indexOf(':') == -1 ? new IdRefReference(element) : new IdRefReference(element) {

                @Override
                public boolean isSoft() {
                    final XmlAttributeDescriptor descriptor = ((XmlAttribute) parentElement).getDescriptor();
                    return descriptor != null && !descriptor.hasIdRefType();
                }
            } };
        } else {
            final boolean allowReferences = !ourNamespacesWithoutNameReference.contains(ns);
            if (ID_ATTR_NAME.equals(name) && allowReferences || STYLE_ID_ATTR_NAME.equals(name) || NAME_ATTR_NAME.equals(name) && allowReferences) {
                final AttributeValueSelfReference attributeValueSelfReference;
                if (jsfNs) {
                    attributeValueSelfReference = new AttributeValueSelfReference(element);
                } else {
                    if (hasOuterLanguageElement(element))
                        return PsiReference.EMPTY_ARRAY;
                    attributeValueSelfReference = new GlobalAttributeValueSelfReference(element, true);
                }
                return new PsiReference[] { attributeValueSelfReference };
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlExtension(com.intellij.xml.XmlExtension) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiReference

use of com.intellij.psi.PsiReference in project kotlin by JetBrains.

the class ConstantEvaluator method isArrayLiteral.

/**
     * Returns true if the node is pointing to a an array literal
     */
public static boolean isArrayLiteral(@Nullable PsiElement node) {
    if (node instanceof PsiReference) {
        PsiElement resolved = ((PsiReference) node).resolve();
        if (resolved instanceof PsiField) {
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return isArrayLiteral(field.getInitializer());
            }
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(node, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return false;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        for (PsiElement element : ((PsiDeclarationStatement) prev).getDeclaredElements()) {
                            if (variable.equals(element)) {
                                return isArrayLiteral(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return isArrayLiteral(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    } else if (node instanceof PsiNewExpression) {
        PsiNewExpression creation = (PsiNewExpression) node;
        if (creation.getArrayInitializer() != null) {
            return true;
        }
        PsiType type = creation.getType();
        if (type instanceof PsiArrayType) {
            return true;
        }
    } else if (node instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) node;
        PsiExpression expression = parenthesizedExpression.getExpression();
        if (expression != null) {
            return isArrayLiteral(expression);
        }
    } else if (node instanceof PsiTypeCastExpression) {
        PsiTypeCastExpression castExpression = (PsiTypeCastExpression) node;
        PsiExpression operand = castExpression.getOperand();
        if (operand != null) {
            return isArrayLiteral(operand);
        }
    }
    return false;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiReference(com.intellij.psi.PsiReference) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiArrayType(com.intellij.psi.PsiArrayType) PsiTypeCastExpression(com.intellij.psi.PsiTypeCastExpression) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 3 with PsiReference

use of com.intellij.psi.PsiReference in project kotlin by JetBrains.

the class ResourceEvaluator method getResource.

/**
     * Evaluates the given node and returns the resource reference (type and name) it
     * points to, if any
     *
     * @param element the node to compute the constant value for
     * @return the corresponding constant value - a String, an Integer, a Float, and so on
     */
@Nullable
public ResourceUrl getResource(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof PsiConditionalExpression) {
        PsiConditionalExpression expression = (PsiConditionalExpression) element;
        Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
        if (known == Boolean.TRUE && expression.getThenExpression() != null) {
            return getResource(expression.getThenExpression());
        } else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
            return getResource(expression.getElseExpression());
        }
    } else if (element instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) element;
        return getResource(parenthesizedExpression.getExpression());
    } else if (element instanceof PsiMethodCallExpression && mAllowDereference) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) element;
        PsiReferenceExpression expression = call.getMethodExpression();
        PsiMethod method = call.resolveMethod();
        if (method != null && method.getContainingClass() != null) {
            String qualifiedName = method.getContainingClass().getQualifiedName();
            String name = expression.getReferenceName();
            if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
                PsiExpression[] args = call.getArgumentList().getExpressions();
                if (args.length > 0) {
                    return getResource(args[0]);
                }
            }
        }
    } else if (element instanceof PsiReference) {
        ResourceUrl url = getResourceConstant(element);
        if (url != null) {
            return url;
        }
        PsiElement resolved = ((PsiReference) element).resolve();
        if (resolved instanceof PsiField) {
            url = getResourceConstant(resolved);
            if (url != null) {
                return url;
            }
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return getResource(field.getInitializer());
            }
            return null;
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return null;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        PsiDeclarationStatement prevStatement = (PsiDeclarationStatement) prev;
                        for (PsiElement e : prevStatement.getDeclaredElements()) {
                            if (variable.equals(e)) {
                                return getResource(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return getResource(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    }
    return null;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiReference(com.intellij.psi.PsiReference) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiElement(com.intellij.psi.PsiElement) Nullable(com.android.annotations.Nullable)

Example 4 with PsiReference

use of com.intellij.psi.PsiReference in project kotlin by JetBrains.

the class ResourceEvaluator method getResourceTypes.

/**
     * Evaluates the given node and returns the resource types applicable to the
     * node, if any.
     *
     * @param element the element to compute the types for
     * @return the corresponding resource types
     */
@Nullable
public EnumSet<ResourceType> getResourceTypes(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof PsiConditionalExpression) {
        PsiConditionalExpression expression = (PsiConditionalExpression) element;
        Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
        if (known == Boolean.TRUE && expression.getThenExpression() != null) {
            return getResourceTypes(expression.getThenExpression());
        } else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
            return getResourceTypes(expression.getElseExpression());
        } else {
            EnumSet<ResourceType> left = getResourceTypes(expression.getThenExpression());
            EnumSet<ResourceType> right = getResourceTypes(expression.getElseExpression());
            if (left == null) {
                return right;
            } else if (right == null) {
                return left;
            } else {
                EnumSet<ResourceType> copy = EnumSet.copyOf(left);
                copy.addAll(right);
                return copy;
            }
        }
    } else if (element instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) element;
        return getResourceTypes(parenthesizedExpression.getExpression());
    } else if (element instanceof PsiMethodCallExpression && mAllowDereference) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) element;
        PsiReferenceExpression expression = call.getMethodExpression();
        PsiMethod method = call.resolveMethod();
        if (method != null && method.getContainingClass() != null) {
            EnumSet<ResourceType> types = getTypesFromAnnotations(method);
            if (types != null) {
                return types;
            }
            String qualifiedName = method.getContainingClass().getQualifiedName();
            String name = expression.getReferenceName();
            if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
                PsiExpression[] args = call.getArgumentList().getExpressions();
                if (args.length > 0) {
                    types = getResourceTypes(args[0]);
                    if (types != null) {
                        return types;
                    }
                }
            }
        }
    } else if (element instanceof PsiReference) {
        ResourceUrl url = getResourceConstant(element);
        if (url != null) {
            return EnumSet.of(url.type);
        }
        PsiElement resolved = ((PsiReference) element).resolve();
        if (resolved instanceof PsiField) {
            url = getResourceConstant(resolved);
            if (url != null) {
                return EnumSet.of(url.type);
            }
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return getResourceTypes(field.getInitializer());
            }
            return null;
        } else if (resolved instanceof PsiParameter) {
            return getTypesFromAnnotations((PsiParameter) resolved);
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return null;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        PsiDeclarationStatement prevStatement = (PsiDeclarationStatement) prev;
                        for (PsiElement e : prevStatement.getDeclaredElements()) {
                            if (variable.equals(e)) {
                                return getResourceTypes(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return getResourceTypes(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    }
    return null;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) EnumSet(java.util.EnumSet) PsiReference(com.intellij.psi.PsiReference) ResourceType(com.android.resources.ResourceType) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiParameter(com.intellij.psi.PsiParameter) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiElement(com.intellij.psi.PsiElement) Nullable(com.android.annotations.Nullable)

Example 5 with PsiReference

use of com.intellij.psi.PsiReference in project kotlin by JetBrains.

the class TypeEvaluator method evaluate.

/**
     * Returns the inferred type of the given node
     */
@Nullable
public PsiType evaluate(@Nullable PsiElement node) {
    if (node == null) {
        return null;
    }
    PsiElement resolved = null;
    if (node instanceof PsiReference) {
        resolved = ((PsiReference) node).resolve();
    }
    if (resolved instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) resolved;
        if (method.isConstructor()) {
            PsiClass containingClass = method.getContainingClass();
            if (containingClass != null && mContext != null) {
                return mContext.getEvaluator().getClassType(containingClass);
            }
        } else {
            return method.getReturnType();
        }
    }
    if (resolved instanceof PsiField) {
        PsiField field = (PsiField) resolved;
        if (field.getInitializer() != null) {
            PsiType type = evaluate(field.getInitializer());
            if (type != null) {
                return type;
            }
        }
        return field.getType();
    } else if (resolved instanceof PsiLocalVariable) {
        PsiLocalVariable variable = (PsiLocalVariable) resolved;
        PsiStatement statement = PsiTreeUtil.getParentOfType(node, PsiStatement.class, false);
        if (statement != null) {
            PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
            String targetName = variable.getName();
            if (targetName == null) {
                return null;
            }
            while (prev != null) {
                if (prev instanceof PsiDeclarationStatement) {
                    for (PsiElement element : ((PsiDeclarationStatement) prev).getDeclaredElements()) {
                        if (variable.equals(element)) {
                            return evaluate(variable.getInitializer());
                        }
                    }
                } else if (prev instanceof PsiExpressionStatement) {
                    PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                    if (expression instanceof PsiAssignmentExpression) {
                        PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                        PsiExpression lhs = assign.getLExpression();
                        if (lhs instanceof PsiReferenceExpression) {
                            PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                            if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                return evaluate(assign.getRExpression());
                            }
                        }
                    }
                }
                prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
            }
        }
        return variable.getType();
    } else if (node instanceof PsiExpression) {
        PsiExpression expression = (PsiExpression) node;
        return expression.getType();
    }
    return null;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(com.android.annotations.Nullable)

Aggregations

PsiReference (com.intellij.psi.PsiReference)564 PsiElement (com.intellij.psi.PsiElement)327 NotNull (org.jetbrains.annotations.NotNull)97 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)54 PsiFile (com.intellij.psi.PsiFile)52 ArrayList (java.util.ArrayList)46 Test (org.junit.Test)40 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)36 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)32 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)25 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)23 XmlTag (com.intellij.psi.xml.XmlTag)22 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)20 PsiClass (com.intellij.psi.PsiClass)17 XmlAttribute (com.intellij.psi.xml.XmlAttribute)17 LinkedList (java.util.LinkedList)17 LookupElement (com.intellij.codeInsight.lookup.LookupElement)16 Project (com.intellij.openapi.project.Project)16