Search in sources :

Example 11 with PsiMultiReference

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

the class InjectedReferenceSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, final int cursorOffset, Editor editor) {
    PsiReference ref = e.findReferenceAt(cursorOffset - e.getTextRange().getStartOffset());
    if (ref == null)
        return Collections.emptyList();
    JBIterable<PsiReference> it = ref instanceof PsiMultiReference ? JBIterable.of(((PsiMultiReference) ref).getReferences()) : JBIterable.of(ref);
    return it.transform(ref1 -> {
        TextRange base = ref1.getElement().getTextRange();
        TextRange r = ref1.getRangeInElement().shiftRight(base.getStartOffset());
        return r.containsOffset(cursorOffset) ? r : null;
    }).filter(Conditions.notNull()).toList();
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) TextRange(com.intellij.openapi.util.TextRange)

Example 12 with PsiMultiReference

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

the class FlexCssNavigationTest method findTargetElements.

@NotNull
private PsiElement[] findTargetElements(@NotNull String... filenames) throws Exception {
    String[] fileNamesWithBasePath = new String[filenames.length];
    for (int i = 0, filenamesLength = filenames.length; i < filenamesLength; i++) {
        fileNamesWithBasePath[i] = BASE_PATH + filenames[i];
    }
    configureByFiles(null, fileNamesWithBasePath);
    Collection<PsiElement> targets;
    PsiReference reference = TargetElementUtil.findReference(myEditor);
    if (reference == null) {
        reference = JSTestUtils.findReferenceFromInjected(myEditor, myFile);
    }
    assertNotNull(reference);
    if (reference instanceof PsiMultiReference) {
        targets = new ArrayList<>();
        for (PsiReference ref : ((PsiMultiReference) reference).getReferences()) {
            targets.addAll(TargetElementUtil.getInstance().getTargetCandidates(ref));
        }
    } else {
        targets = TargetElementUtil.getInstance().getTargetCandidates(reference);
    }
    assertTrue("Target elements not found", targets.size() > 0);
    return PsiUtilCore.toPsiElementArray(targets);
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PsiMultiReference

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

the class DartServerCompletionContributor method beforeCompletion.

@Override
public void beforeCompletion(@NotNull final CompletionInitializationContext context) {
    final PsiElement psiElement = context.getFile().findElementAt(context.getStartOffset());
    final PsiElement parent = psiElement != null ? psiElement.getParent() : null;
    if (parent instanceof DartStringLiteralExpression) {
        final PsiElement parentParent = parent.getParent();
        if (parentParent instanceof DartUriElement) {
            final Pair<String, TextRange> uriAndRange = ((DartUriElement) parentParent).getUriStringAndItsRange();
            context.setReplacementOffset(parentParent.getTextRange().getStartOffset() + uriAndRange.second.getEndOffset());
        } else {
            // If replacement context is not set explicitly then com.intellij.codeInsight.completion.CompletionProgressIndicator#duringCompletion
            // implementation looks for the reference at caret and on Tab replaces the whole reference.
            // angular_analyzer_plugin provides angular-specific completion inside Dart string literals. Without the following hack Tab replaces
            // too much useful text. This hack is not ideal though as it may leave a piece of tail not replaced.
            // TODO: use replacementLength received from the server
            context.setReplacementOffset(context.getReplacementOffset());
        }
    } else {
        PsiReference reference = context.getFile().findReferenceAt(context.getStartOffset());
        if (reference instanceof PsiMultiReference && ((PsiMultiReference) reference).getReferences().length > 0) {
            // to ensure that references are sorted by range
            reference.getRangeInElement();
            reference = ((PsiMultiReference) reference).getReferences()[0];
        }
        if (reference instanceof DartNewExpression) {
            // historically DartNewExpression is a reference; it can appear here only in situation like new Foo(o.<caret>);
            // without the following hack closing paren is replaced on Tab. We won't get here if at least one symbol after dot typed.
            context.setReplacementOffset(context.getStartOffset());
        }
    }
}
Also used : DartUriElement(com.jetbrains.lang.dart.psi.DartUriElement) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) TextRange(com.intellij.openapi.util.TextRange) DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) PsiElement(com.intellij.psi.PsiElement) DartNewExpression(com.jetbrains.lang.dart.psi.DartNewExpression)

Example 14 with PsiMultiReference

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

the class RequirejsTestCase method assertReference.

protected void assertReference(PsiReference reference, String expectedText, String expectedFileName) {
    assertNotNull("wrong cursor position", reference);
    if (reference instanceof PsiMultiReference) {
        for (PsiReference ref : ((PsiMultiReference) reference).getReferences()) {
            if (ref instanceof RequirejsReference) {
                reference = ref;
                break;
            }
        }
    }
    if (!expectedText.startsWith("'")) {
        expectedText = "'".concat(expectedText).concat("'");
    }
    assertInstanceOf(reference, RequirejsReference.class);
    assertEquals(expectedText, reference.getCanonicalText());
    PsiElement referenceElement = reference.resolve();
    if (null == expectedFileName) {
        assertNull(referenceElement);
    } else {
        assertNotNull("Not resolved", referenceElement);
        assertInstanceOf(referenceElement, PsiFile.class);
        assertEquals(expectedFileName, ((PsiFile) referenceElement).getName());
    }
}
Also used : PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiMultiReference

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

the class JavaFxEventHandlerReferenceProvider method getReferencesByElement.

@Override
protected PsiReference[] getReferencesByElement(@NotNull PsiClass controllerClass, XmlAttributeValue xmlAttributeValue, ProcessingContext context) {
    final String attValueString = xmlAttributeValue.getValue();
    LOG.assertTrue(attValueString.startsWith("#"));
    final XmlAttribute attribute = (XmlAttribute) xmlAttributeValue.getParent();
    if (attribute == null || !JavaFxPsiUtil.isEventHandlerProperty(attribute))
        return PsiReference.EMPTY_ARRAY;
    final String eventHandlerName = attValueString.substring(1);
    final PsiMethod[] methods = controllerClass.findMethodsByName(eventHandlerName, true);
    final PsiReference[] references = Arrays.stream(methods).filter(psiMethod -> JavaFxEventHandlerReference.isHandlerMethodSignature(psiMethod, controllerClass)).map(psiMethod -> new JavaFxEventHandlerReference(xmlAttributeValue, psiMethod, controllerClass)).toArray(PsiReference.ARRAY_FACTORY::create);
    if (references.length == 1) {
        return references;
    }
    if (references.length > 1) {
        return new PsiReference[] { new PsiMultiReference(references, xmlAttributeValue) };
    }
    if (references.length == 0) {
        final XmlTag rootTag = ((XmlFile) xmlAttributeValue.getContainingFile()).getRootTag();
        if (rootTag == null || FxmlConstants.FX_ROOT.equals(rootTag.getName())) {
            return PsiReference.EMPTY_ARRAY;
        }
    }
    return new PsiReference[] { new JavaFxEventHandlerReference(xmlAttributeValue, null, controllerClass) };
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) Arrays(java.util.Arrays) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiMethod(com.intellij.psi.PsiMethod) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) PsiReference(com.intellij.psi.PsiReference) JavaFxPsiUtil(org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil) PsiClass(com.intellij.psi.PsiClass) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) FxmlConstants(org.jetbrains.plugins.javaFX.fxml.FxmlConstants) XmlTag(com.intellij.psi.xml.XmlTag) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) PsiMethod(com.intellij.psi.PsiMethod) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) XmlTag(com.intellij.psi.xml.XmlTag)

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