Search in sources :

Example 1 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project intellij-community by JetBrains.

the class MavenDomTestCase method assertNoReferences.

protected void assertNoReferences(VirtualFile file, Class refClass) throws IOException {
    PsiReference ref = getReferenceAtCaret(file);
    if (ref == null)
        return;
    PsiReference[] refs = ref instanceof PsiMultiReference ? ((PsiMultiReference) ref).getReferences() : new PsiReference[] { ref };
    for (PsiReference each : refs) {
        assertFalse(each.toString(), refClass.isInstance(each));
    }
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)

Example 2 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project intellij-community by JetBrains.

the class PyBaseElementImpl method findReferenceAt.

/**
   * Overrides the findReferenceAt() logic in order to provide a resolve context with origin file for returned references.
   * The findReferenceAt() is usually invoked from UI operations, and it helps to be able to do deeper analysis in the
   * current file for such operations.
   *
   * @param offset the offset to find the reference at
   * @return the reference or null.
   */
@Override
public PsiReference findReferenceAt(int offset) {
    // copy/paste from SharedPsiElementImplUtil
    PsiElement element = findElementAt(offset);
    if (element == null || element instanceof OuterLanguageElement)
        return null;
    offset = getTextRange().getStartOffset() + offset - element.getTextRange().getStartOffset();
    List<PsiReference> referencesList = new ArrayList<>();
    final PsiFile file = element.getContainingFile();
    final PyResolveContext resolveContext = file != null ? PyResolveContext.defaultContext().withTypeEvalContext(TypeEvalContext.codeAnalysis(file.getProject(), file)) : PyResolveContext.defaultContext();
    while (element != null) {
        addReferences(offset, element, referencesList, resolveContext);
        offset = element.getStartOffsetInParent() + offset;
        if (element instanceof PsiFile)
            break;
        element = element.getParent();
    }
    if (referencesList.isEmpty())
        return null;
    if (referencesList.size() == 1)
        return referencesList.get(0);
    return new PsiMultiReference(referencesList.toArray(new PsiReference[referencesList.size()]), referencesList.get(referencesList.size() - 1).getElement());
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) ArrayList(java.util.ArrayList) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)

Example 3 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project intellij-community by JetBrains.

the class JavaFxPropertyRenameHandler method getReferences.

@NotNull
private static PsiReference[] getReferences(DataContext dataContext) {
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (file == null && editor != null && ApplicationManager.getApplication().isUnitTestMode()) {
        final Project project = editor.getProject();
        if (project != null) {
            file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        }
    }
    if (editor != null && file instanceof XmlFile && JavaFxFileTypeFactory.isFxml(file)) {
        final int offset = editor.getCaretModel().getOffset();
        final PsiReference reference = file.findReferenceAt(offset);
        if (reference instanceof PsiMultiReference) {
            return ((PsiMultiReference) reference).getReferences();
        }
        if (isKnown(reference))
            return new PsiReference[] { reference };
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project intellij-community by JetBrains.

the class XmlLocationCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiReference reference = parameters.getPosition().getContainingFile().findReferenceAt(parameters.getOffset());
    if (reference instanceof URLReference) {
        if (((URLReference) reference).isSchemaLocation()) {
            Object[] objects = completeSchemaLocation(reference.getElement());
            result.addAllElements(ContainerUtil.map(objects, MAPPING));
            return;
        }
        Object[] objects = completeNamespace(reference.getElement());
        result.addAllElements(ContainerUtil.map(objects, MAPPING));
        return;
    }
    if (reference instanceof PsiMultiReference)
        reference = ((PsiMultiReference) reference).getReferences()[0];
    if (reference instanceof DependentNSReference) {
        MultiMap<String, String> map = ExternalResourceManagerEx.getInstanceEx().getUrlsByNamespace(parameters.getOriginalFile().getProject());
        String namespace = ((DependentNSReference) reference).getNamespaceReference().getCanonicalText();
        Collection<String> strings = map.get(namespace);
        for (String string : strings) {
            if (!namespace.equals(string)) {
                // exclude namespaces from location urls
                result.consume(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(string), 100));
            }
        }
        if (!strings.isEmpty())
            result.stopHere();
    }
}
Also used : DependentNSReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference) URLReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)

Example 5 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project WebStormRequireJsPlugin by Fedott.

the class ReferenceTest method testReference.

public void testReference() {
    PsiReference reference;
    PsiElement referenceElement;
    // referenceNotFound
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(1, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'app/as'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // referenceTrue
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(2, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // referenceNotFound2
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(3, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof PsiMultiReference);
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertEquals("'bl'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // referenceDirectory
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(4, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/childBlocks'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // referenceWithTwoDot
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 46));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'../blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // referenceWithTwoDotTwoDir
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(6, 49));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'../blocks/childBlocks/childBlock'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("childBlock.js", ((JSFile) referenceElement).getName());
    // referenceWithOneDot
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(7, 44));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'./block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // referenceWithOneDotTwoDir
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(8, 50));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'./childBlocks/childBlock'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("childBlock.js", ((JSFile) referenceElement).getName());
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) JSFile(com.intellij.lang.javascript.psi.JSFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)15 PsiReference (com.intellij.psi.PsiReference)13 PsiElement (com.intellij.psi.PsiElement)7 JSFile (com.intellij.lang.javascript.psi.JSFile)4 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)4 NotNull (org.jetbrains.annotations.NotNull)3 TextRange (com.intellij.openapi.util.TextRange)2 XmlFile (com.intellij.psi.xml.XmlFile)2 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 PsiDynaReference (com.intellij.openapi.paths.PsiDynaReference)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 DependentNSReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference)1 URLReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference)1 OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1