Search in sources :

Example 16 with PsiReference

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

the class FormPropertyUsageTest method doPropertyUsageTest.

private void doPropertyUsageTest(final String propertyFileName) {
    PropertiesFile propFile = (PropertiesFile) myPsiManager.findFile(myTestProjectRoot.findChild(propertyFileName));
    assertNotNull(propFile);
    final Property prop = (Property) propFile.findPropertyByKey("key");
    assertNotNull(prop);
    final Query<PsiReference> query = ReferencesSearch.search(prop);
    final Collection<PsiReference> result = query.findAll();
    assertEquals(1, result.size());
    verifyReference(result, 0, "form.form", 960);
}
Also used : PsiReference(com.intellij.psi.PsiReference) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Property(com.intellij.lang.properties.psi.Property)

Example 17 with PsiReference

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

the class PyCallExpressionHelper method getSuperCallType.

@NotNull
private static Maybe<PyType> getSuperCallType(@NotNull PyCallExpression call, TypeEvalContext context) {
    final PyExpression callee = call.getCallee();
    if (callee instanceof PyReferenceExpression) {
        PsiElement must_be_super_init = ((PyReferenceExpression) callee).getReference().resolve();
        if (must_be_super_init instanceof PyFunction) {
            PyClass must_be_super = ((PyFunction) must_be_super_init).getContainingClass();
            if (must_be_super == PyBuiltinCache.getInstance(call).getClass(PyNames.SUPER)) {
                final PyArgumentList argumentList = call.getArgumentList();
                if (argumentList != null) {
                    final PyClass containingClass = PsiTreeUtil.getParentOfType(call, PyClass.class);
                    PyExpression[] args = argumentList.getArguments();
                    if (args.length > 1) {
                        PyExpression first_arg = args[0];
                        if (first_arg instanceof PyReferenceExpression) {
                            final PyReferenceExpression firstArgRef = (PyReferenceExpression) first_arg;
                            final PyExpression qualifier = firstArgRef.getQualifier();
                            if (qualifier != null && PyNames.__CLASS__.equals(firstArgRef.getReferencedName())) {
                                final PsiReference qRef = qualifier.getReference();
                                final PsiElement element = qRef == null ? null : qRef.resolve();
                                if (element instanceof PyParameter) {
                                    final PyParameterList parameterList = PsiTreeUtil.getParentOfType(element, PyParameterList.class);
                                    if (parameterList != null && element == parameterList.getParameters()[0]) {
                                        return new Maybe<>(getSuperCallTypeForArguments(context, containingClass, args[1]));
                                    }
                                }
                            }
                            PsiElement possible_class = firstArgRef.getReference().resolve();
                            if (possible_class instanceof PyClass && ((PyClass) possible_class).isNewStyleClass(context)) {
                                final PyClass first_class = (PyClass) possible_class;
                                return new Maybe<>(getSuperCallTypeForArguments(context, first_class, args[1]));
                            }
                        }
                    } else if ((call.getContainingFile() instanceof PyFile) && ((PyFile) call.getContainingFile()).getLanguageLevel().isPy3K() && (containingClass != null)) {
                        return new Maybe<>(getSuperClassUnionType(containingClass, context));
                    }
                }
            }
        }
    }
    return new Maybe<>();
}
Also used : Maybe(com.jetbrains.python.toolbox.Maybe) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PsiReference

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

the class VariableInlineHandler method invoke.

public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
    final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).getType(variable);
    final Project project = variable.getProject();
    final XmlTag tag = ((XsltElement) variable).getTag();
    final String expression = tag.getAttributeValue("select");
    if (expression == null) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
        return;
    }
    final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
    if (references.size() == 0) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, null);
        return;
    }
    boolean hasExternalRefs = false;
    if (XsltSupport.isTopLevelElement(tag)) {
        final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false);
        hasExternalRefs = !query.forEach(new Processor<PsiReference>() {

            int allRefs = 0;

            public boolean process(PsiReference psiReference) {
                if (++allRefs > references.size()) {
                    return false;
                } else if (!references.contains(psiReference)) {
                    return false;
                }
                return true;
            }
        });
    }
    final HighlightManager highlighter = HighlightManager.getInstance(project);
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    final PsiReference[] psiReferences = references.toArray(new PsiReference[references.size()]);
    TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> {
        final PsiElement psiElement = s.getElement();
        final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true);
        if (psiElement instanceof XPathElement && context != null) {
            return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement) psiElement).cutOut(s.getRangeInElement());
        }
        return psiElement.getTextRange().cutOut(s.getRangeInElement());
    });
    final Editor e = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    for (TextRange range : ranges) {
        final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes();
        final Color color = getScrollmarkColor(textAttributes);
        highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color);
    }
    highlighter.addOccurrenceHighlights(e, new PsiElement[] { ((XsltVariable) variable).getNameIdentifier() }, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
    if (!hasExternalRefs) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
            return;
        }
    } else {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
            return;
        }
    }
    final boolean hasRefs = hasExternalRefs;
    new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {

        @Override
        protected void run() throws Throwable {
            try {
                for (PsiReference psiReference : references) {
                    final PsiElement element = psiReference.getElement();
                    if (element instanceof XPathElement) {
                        final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression);
                        element.replace(newExpr);
                    } else {
                        assert false;
                    }
                }
                if (!hasRefs) {
                    tag.delete();
                }
            } catch (IncorrectOperationException e) {
                Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) ArrayList(java.util.ArrayList) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiElement(com.intellij.psi.PsiElement) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XPathElement(org.intellij.lang.xpath.psi.XPathElement) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 19 with PsiReference

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

the class XsltImplicitUsagesProvider method isImplicitUsage.

public boolean isImplicitUsage(PsiElement element) {
    if (!(element instanceof XmlAttribute)) {
        return false;
    }
    final XmlAttribute attr = (XmlAttribute) element;
    if (!attr.isNamespaceDeclaration()) {
        return false;
    }
    final PsiFile file = attr.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return false;
    }
    // ContextProvider.hasXPathInjections() is an optimization that avoids to run the references search on totally XPath-free XML files
    if (!ContextProvider.hasXPathInjections((XmlFile) file) && !XsltSupport.isXsltFile(file)) {
        return false;
    }
    // This need to catch both prefix references from injected XPathFiles and prefixes from mode declarations/references:
    // <xsl:template match="*" mode="prefix:name" />
    // BTW: Almost the same logic applies to other XML dialects (RELAX-NG).
    // Pull this class into the platform?
    final String prefix = attr.getLocalName();
    final SchemaPrefix target = new SchemaPrefix(attr, TextRange.from("xmlns:".length(), prefix.length()), prefix);
    final Query<PsiReference> q = ReferencesSearch.search(target, new LocalSearchScope(attr.getParent()));
    return !q.forEach(psiReference -> {
        if (psiReference.getElement() == attr) {
            return true;
        }
        return false;
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltSupport(org.intellij.lang.xpath.xslt.XsltSupport) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Query(com.intellij.util.Query) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile)

Example 20 with PsiReference

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

the class XsltVariableContext method isReferenceTo.

public boolean isReferenceTo(PsiElement element, XPathVariableReference reference) {
    if (element instanceof XsltParameter) {
        final XsltTemplate template = XsltCodeInsightUtil.getTemplate(element, false);
        if (template == null || template.getMatchExpression() == null)
            return false;
        final XPathVariable t = reference.resolve();
        final PsiReference[] references = element.getReferences();
        for (PsiReference r : references) {
            if (r.isReferenceTo(t))
                return true;
        }
    }
    return false;
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) PsiReference(com.intellij.psi.PsiReference) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate)

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