Search in sources :

Example 66 with PsiReference

use of com.intellij.psi.PsiReference 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 67 with PsiReference

use of com.intellij.psi.PsiReference in project smali by JesusFreke.

the class MethodReferenceTest method testSmaliReferenceFromJava.

/**
     * Test a reference to a smali method from a java class
     */
public void testSmaliReferenceFromJava() throws Exception {
    createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" + ".method public static blort(ILjava/lang/String;)V\n" + "    .locals 0\n" + "    return-void\n" + ".end method\n");
    String text = "public class blah { public static void something() {" + "blarg.bl<ref>ort(10, \"bob\");" + "}}";
    PsiReference methodReference = configureByFileText(text, "blah.java");
    Assert.assertNotNull(methodReference);
    PsiMethod resolvedMethod = (PsiMethod) methodReference.resolve();
    Assert.assertNotNull(resolvedMethod);
    Assert.assertEquals("blort", resolvedMethod.getName());
    Assert.assertNotNull(resolvedMethod.getContainingClass());
    Assert.assertEquals("blarg", resolvedMethod.getContainingClass().getQualifiedName());
    Assert.assertEquals(2, resolvedMethod.getParameterList().getParametersCount());
    Assert.assertEquals("int", resolvedMethod.getParameterList().getParameters()[0].getType().getCanonicalText());
    Assert.assertEquals("java.lang.String", resolvedMethod.getParameterList().getParameters()[1].getType().getCanonicalText());
    Assert.assertNotNull(resolvedMethod.getReturnType());
    Assert.assertEquals("void", resolvedMethod.getReturnType().getCanonicalText());
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiReference(com.intellij.psi.PsiReference)

Example 68 with PsiReference

use of com.intellij.psi.PsiReference in project smali by JesusFreke.

the class ClassReferenceTest method testSmaliReferenceFromJava.

/**
     * Test a reference to a smali class from a java class
     */
public void testSmaliReferenceFromJava() throws Exception {
    createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;");
    PsiReference reference = configureByFileText("public class blah extends bla<ref>rg { }", "blah.java");
    SmaliClass smaliClass = (SmaliClass) reference.resolve();
    Assert.assertNotNull(smaliClass);
    Assert.assertEquals("blarg", smaliClass.getQualifiedName());
}
Also used : SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiReference(com.intellij.psi.PsiReference)

Example 69 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-elixir by KronicDeth.

the class Issue480Test method assertResolvedNameArityRange.

private void assertResolvedNameArityRange(@NotNull PsiElement element, @NotNull String name, int arity) {
    PsiReference reference = element.getReference();
    assertNotNull(reference);
    PsiElement resolved = reference.resolve();
    assertNotNull(resolved);
    assertInstanceOf(resolved, ElixirIdentifier.class);
    PsiElement maybeDefMaybeCall = resolved.getParent().getParent().getParent();
    assertInstanceOf(maybeDefMaybeCall, Call.class);
    Call maybeDefCall = (Call) maybeDefMaybeCall;
    assertTrue(CallDefinitionClause.is(maybeDefCall));
    assertEquals(pair(name, new IntRange(arity)), nameArityRange(maybeDefCall));
}
Also used : Call(org.elixir_lang.psi.call.Call) IntRange(org.apache.commons.lang.math.IntRange) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 70 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-elixir by KronicDeth.

the class Issue480Test method assertUnresolvableReferenceNameArityRange.

/*
     * Private Instance Methods
     */
private void assertUnresolvableReferenceNameArityRange(@NotNull String name, int arity) {
    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    assertNotNull(elementAtCaret);
    PsiElement grandParent = elementAtCaret.getParent().getParent();
    assertNotNull(grandParent);
    assertInstanceOf(grandParent, Call.class);
    Call grandParentCall = (Call) grandParent;
    assertEquals(name, grandParentCall.functionName());
    assertEquals(arity, grandParentCall.resolvedFinalArity());
    PsiReference reference = grandParent.getReference();
    assertNotNull(reference);
    PsiElement resolved = reference.resolve();
    assertNull(resolved);
}
Also used : Call(org.elixir_lang.psi.call.Call) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

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