Search in sources :

Example 31 with PsiReference

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

the class MavenFilteredPropertyPsiReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (!shouldAddReference(element)) {
        // Add reference to element with one child or leaf element only to avoid performance problem.
        return PsiReference.EMPTY_ARRAY;
    }
    if (!MavenDomUtil.isFilteredResourceFile(element))
        return PsiReference.EMPTY_ARRAY;
    String text = element.getText();
    if (StringUtil.isEmptyOrSpaces(text))
        return PsiReference.EMPTY_ARRAY;
    MavenProject mavenProject = MavenDomUtil.findContainingProject(element);
    if (mavenProject == null)
        return PsiReference.EMPTY_ARRAY;
    List<PsiReference> res = null;
    Pattern pattern = getDelimitersPattern(mavenProject);
    Matcher matcher = pattern.matcher(text);
    int groupCount = matcher.groupCount();
    while (matcher.find()) {
        String propertyName = null;
        int from = 0;
        for (int i = 0; i < groupCount; i++) {
            propertyName = matcher.group(i + 1);
            if (propertyName != null) {
                from = matcher.start(i + 1);
                break;
            }
        }
        assert propertyName != null;
        if (res == null) {
            res = new ArrayList<>();
        }
        TextRange range = TextRange.from(from, propertyName.length());
        res.add(new MavenFilteredPropertyPsiReference(mavenProject, element, propertyName, range));
    }
    return res == null ? PsiReference.EMPTY_ARRAY : res.toArray(new PsiReference[res.size()]);
}
Also used : Pattern(java.util.regex.Pattern) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Matcher(java.util.regex.Matcher) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with PsiReference

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

the class XsltResolveTest method doVariableResolveTest.

private XsltVariable doVariableResolveTest(boolean global) throws Throwable {
    final PsiReference reference = findInjectedReferenceAtCaret();
    final PsiElement element = reference.resolve();
    assertTrue(element instanceof XsltVariable);
    final XsltVariable var = (XsltVariable) element;
    assertEquals(var.getName(), ((XPathVariableReference) reference).getReferencedName());
    assertEquals(global, XsltSupport.isTopLevelElement(var.getTag()));
    return var;
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 33 with PsiReference

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

the class XsltValidator method checkUnusedVariable.

public static void checkUnusedVariable(XsltVariable variable, ProblemsHolder holder) {
    if (variable instanceof XsltParameter) {
        if (((XsltParameter) variable).isAbstract()) {
            return;
        }
    } else {
        if (variable.isVoid()) {
            return;
        }
    }
    final XmlTag tag = variable.getTag();
    final XmlTag templateTag = XsltCodeInsightUtil.getTemplateTag(tag, false);
    if (templateTag == null) {
        return;
    }
    final XmlAttribute attribute = tag.getAttribute("name");
    if (attribute == null) {
        return;
    }
    final PsiElement token = XsltSupport.getAttValueToken(attribute);
    if (token == null) {
        return;
    }
    final SearchScope scope = new LocalSearchScope(templateTag);
    final Query<PsiReference> refs = ReferencesSearch.search(variable, scope, false);
    if (isUnused(variable, refs)) {
        final String name = variable.getName();
        assert name != null;
        final LocalQuickFix[] fixes;
        if (variable instanceof XsltParameter) {
            fixes = new LocalQuickFix[] { new DeleteUnusedParameterFix(name, (XsltParameter) variable) };
        } else {
            fixes = new LocalQuickFix[] { new DeleteUnusedVariableFix(name, variable) };
        }
        holder.registerProblem(token, ((DeleteUnusedElementBase) fixes[0]).getType() + " '" + name + "' is never used", ProblemHighlightType.LIKE_UNUSED_SYMBOL, fixes);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DeleteUnusedVariableFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedVariableFix) PsiReference(com.intellij.psi.PsiReference) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) DeleteUnusedParameterFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedParameterFix) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) DeleteUnusedElementBase(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedElementBase) XmlTag(com.intellij.psi.xml.XmlTag)

Example 34 with PsiReference

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

the class XsltValidator method isUnused.

private static boolean isUnused(PsiElement obj, Query<PsiReference> query) {
    if (obj instanceof XsltParameter) {
        final Collection<PsiReference> references = query.findAll();
        int n = references.size();
        for (PsiReference reference : references) {
            final PsiElement element = reference.getElement();
            if (element instanceof XmlAttributeValue) {
                final XmlAttribute parent = (XmlAttribute) element.getParent();
                if ("name".equals(parent.getName())) {
                    final XmlTag tag = parent.getParent();
                    if (tag != null && "with-param".equals(tag.getLocalName())) {
                        n--;
                    }
                }
            }
        }
        return n == 0;
    } else {
        return query.findFirst() == null;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 35 with PsiReference

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

the class XsltRefactoringTest method findVariable.

@Nullable
private XPathVariable findVariable() {
    final PsiElement elementAtCaret = myFixture.getElementAtCaret();
    XPathVariableReference ref = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariableReference.class, false);
    if (ref != null) {
        return ref.resolve();
    }
    XPathVariable variable = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariable.class, false);
    if (variable != null) {
        return variable;
    }
    final PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        final PsiElement var = reference.resolve();
        if (var instanceof XPathVariable) {
            return (XPathVariable) var;
        }
    }
    return null;
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.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