Search in sources :

Example 6 with PsiMultiReference

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

the class ReferenceTest method testCompletionInDefine.

public void testCompletionInDefine() {
    myFixture.configureByFiles("public/blocks/fileWithDefine.js");
    PsiReference reference;
    PsiElement referenceElement;
    // 1
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 18));
    reference = myFixture.getReferenceAtCaretPosition();
    assert null != reference;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // 2
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assert null != reference;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'/blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // 3
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 51));
    reference = myFixture.getReferenceAtCaretPosition();
    assert null != reference;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'./blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.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)

Example 7 with PsiMultiReference

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

the class ReferenceTest method testReferenceWithBaseUrl.

public void testReferenceWithBaseUrl() {
    PsiReference reference;
    PsiElement referenceElement;
    myFixture.configureByFiles("public/fileForTestBaseUrlReference.js", "public/mainWithBaseUrl.js");
    Settings.getInstance(getProject()).configFilePath = "mainWithBaseUrl.js";
    // 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);
    // 1
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(2, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // 2
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(3, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assert null != reference;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    // 3
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(4, 33));
    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());
    // 4
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assert null != reference;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'childBlocks'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // 5
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(6, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // 6
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(7, 33));
    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());
    // 7
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(8, 33));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'/childBlocks/childBlock'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    // 8
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(9, 33));
    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());
// 9
// TODO: Add check for parent root web directory
//        myFixture
//                .getEditor()
//                .getCaretModel()
//                .moveToLogicalPosition(new LogicalPosition(10, 33));
//        reference = myFixture.getReferenceAtCaretPosition();
//        assertTrue(reference instanceof RequirejsReference);
//        assertEquals("'../public/blocks/block'", reference.getCanonicalText());
//        referenceElement = reference.resolve();
//        assertNull(referenceElement);
}
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)

Example 8 with PsiMultiReference

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

the class ConfigParseTest method testReference.

public void testReference() {
    Settings.getInstance(getProject()).configFilePath = "mainRequireJs.js";
    PsiReference reference;
    PsiElement referenceElement;
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(1, 36));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'module'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertEquals("rootWebPathConfigTest.js", ((JSFile) referenceElement).getName());
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(2, 36));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'moduleR'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(3, 51));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'moduleRelativeBaseUrlPath'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("childBlock.js", ((JSFile) referenceElement).getName());
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(4, 51));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'moduleAbsolutePath'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5, 51));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'moduleRelativeOneDotPath'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(6, 51));
    reference = myFixture.getReferenceAtCaretPosition();
    assert (reference) != null;
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'moduleRelativeTwoDotPAth'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);
}
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)

Example 9 with PsiMultiReference

use of com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference in project smali by JesusFreke.

the class UsageTypeTest method doTest.

protected void doTest(@NotNull String fileName, @NotNull String text, @NotNull Object... expectedUsageTypes) throws Exception {
    Assert.assertTrue(expectedUsageTypes.length % 2 == 0);
    Map<Integer, UsageType> expectedUsageTypesMap = Maps.newHashMap();
    for (int i = 0; i < expectedUsageTypes.length; i += 2) {
        expectedUsageTypesMap.put((Integer) expectedUsageTypes[i], (UsageType) expectedUsageTypes[i + 1]);
    }
    PsiFile psiFile = createFile(fileName, REF_PATTERN.matcher(text).replaceAll(""));
    Map<Integer, Integer> refIndexMap = getRefIndexes(text);
    for (Map.Entry<Integer, Integer> entry : refIndexMap.entrySet()) {
        int refId = entry.getKey();
        int index = entry.getValue();
        PsiReference reference = psiFile.getFirstChild().findReferenceAt(index);
        Assert.assertNotNull(reference);
        if (reference instanceof PsiMultiReference) {
            // If there are multiple reference parents, the default seems to be the last one,
            // i.e. the highest parent. We actually want the lowest one here.
            reference = ((PsiMultiReference) reference).getReferences()[0];
        }
        UsageType usageType = usageTypeProvider.getUsageType(reference.getElement());
        Assert.assertNotNull(usageType);
        Assert.assertSame(expectedUsageTypesMap.get(refId), usageType);
        expectedUsageTypesMap.remove(refId);
    }
    Assert.assertTrue(expectedUsageTypesMap.isEmpty());
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiFile(com.intellij.psi.PsiFile) UsageType(com.intellij.usages.impl.rules.UsageType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with PsiMultiReference

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

the class LegacyCompletionContributor method processReferences.

public static void processReferences(final CompletionParameters parameters, final CompletionResultSet result, final PairConsumer<PsiReference, CompletionResultSet> consumer) {
    final int startOffset = parameters.getOffset();
    final PsiReference ref = parameters.getPosition().getContainingFile().findReferenceAt(startOffset);
    if (ref instanceof PsiMultiReference) {
        for (final PsiReference reference : CompletionData.getReferences((PsiMultiReference) ref)) {
            processReference(result, startOffset, consumer, reference);
        }
    } else if (ref instanceof PsiDynaReference) {
        for (final PsiReference reference : ((PsiDynaReference<?>) ref).getReferences()) {
            processReference(result, startOffset, consumer, reference);
        }
    } else if (ref != null) {
        processReference(result, startOffset, consumer, ref);
    }
}
Also used : PsiDynaReference(com.intellij.openapi.paths.PsiDynaReference) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)

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