Search in sources :

Example 11 with PsiPolyVariantReference

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

the class PyCallExpressionHelper method getCallType.

public static PyType getCallType(@NotNull PyCallExpression call, @NotNull TypeEvalContext context) {
    if (!TypeEvalStack.mayEvaluate(call)) {
        return null;
    }
    try {
        PyExpression callee = call.getCallee();
        if (callee instanceof PyReferenceExpression) {
            // hardwired special cases
            if (PyNames.SUPER.equals(callee.getText())) {
                final Maybe<PyType> superCallType = getSuperCallType(call, context);
                if (superCallType.isDefined()) {
                    return superCallType.value();
                }
            }
            if ("type".equals(callee.getText())) {
                final PyExpression[] args = call.getArguments();
                if (args.length == 1) {
                    final PyExpression arg = args[0];
                    final PyType argType = context.getType(arg);
                    if (argType instanceof PyClassType) {
                        final PyClassType classType = (PyClassType) argType;
                        if (!classType.isDefinition()) {
                            final PyClass cls = classType.getPyClass();
                            return context.getType(cls);
                        }
                    } else {
                        return null;
                    }
                }
            }
            // normal cases
            final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
            final PsiPolyVariantReference reference = ((PyReferenceExpression) callee).getReference(resolveContext);
            final List<PyType> members = new ArrayList<>();
            for (PsiElement target : PyUtil.multiResolveTopPriority(reference)) {
                PyUtil.verboseOnly(() -> PyPsiUtils.assertValid(target));
                if (target != null) {
                    final Ref<? extends PyType> typeRef = getCallTargetReturnType(call, target, context);
                    if (typeRef != null) {
                        members.add(typeRef.get());
                    }
                }
            }
            if (!members.isEmpty()) {
                return PyUnionType.union(members);
            }
        }
        if (callee == null) {
            return null;
        } else {
            final PyType type = context.getType(callee);
            if (type instanceof PyCallableType) {
                final PyCallableType callableType = (PyCallableType) type;
                return callableType.getCallType(context, call);
            }
            if (type instanceof PyUnionType) {
                return getCallResultTypeFromUnion(call, context, (PyUnionType) type);
            }
            return null;
        }
    } finally {
        TypeEvalStack.evaluated(call);
    }
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PsiPolyVariantReference

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

the class RngHighlightingTest method testResolveIncludedRef2.

public void testResolveIncludedRef2() throws Throwable {
    myTestFixture.copyFileToProject("include.rng");
    final PsiReference ref = myTestFixture.getReferenceAtCaretPositionWithAssertion("resolve-include-ref-2.rng");
    assertTrue("PolyVariantRef", ref instanceof PsiPolyVariantReference);
    final PsiElement element = ref.resolve();
    assertNull(element);
    final ResolveResult[] results = ((PsiPolyVariantReference) ref).multiResolve(false);
    assertEquals(2, results.length);
    for (ResolveResult result : results) {
        PsiElement e = result.getElement();
        assertTrue(e instanceof XmlTag);
        final int contentLength = ((XmlTag) e).getSubTags().length;
        if (e.getContainingFile() == ref.getElement().getContainingFile()) {
            assertEquals(1, contentLength);
        } else {
            assertEquals(0, contentLength);
        }
    }
}
Also used : PsiReference(com.intellij.psi.PsiReference) ResolveResult(com.intellij.psi.ResolveResult) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 13 with PsiPolyVariantReference

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

the class DependencyVisitor method visitPyReferenceExpression.

@Override
public void visitPyReferenceExpression(final PyReferenceExpression node) {
    final PsiPolyVariantReference reference = node.getReference();
    if (reference.isReferenceTo(myElementToFind)) {
        myDependencyFound = true;
        return;
    }
    // TODO: This step is member-type specific. Move to MemberManagers?
    if (myElementToFind instanceof PyAssignmentStatement) {
        final PyExpression[] targets = ((PyAssignmentStatement) myElementToFind).getTargets();
        if (targets.length != 1) {
            return;
        }
        final PyExpression expression = targets[0];
        if (reference.isReferenceTo(expression)) {
            myDependencyFound = true;
            return;
        }
        if (node.getText().equals(expression.getText())) {
            // Check by name also
            myDependencyFound = true;
        }
        return;
    }
    final PsiElement declaration = reference.resolve();
    myDependencyFound = PsiTreeUtil.findFirstParent(declaration, new PsiElementCondition()) != null;
}
Also used : PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PsiPolyVariantReference

use of com.intellij.psi.PsiPolyVariantReference in project kotlin by JetBrains.

the class BuiltInsReferenceResolverTest method doTest.

private void doTest() throws Exception {
    PsiPolyVariantReference reference = (PsiPolyVariantReference) myFixture.getReferenceAtCaretPosition(getTestName(true) + ".kt");
    assert reference != null;
    PsiElement resolved = reference.resolve();
    assertNotNull(resolved);
    assertEquals(1, reference.multiResolve(false).length);
    String text = myFixture.getFile().getText();
    String expectedBinaryFile = InTextDirectivesUtils.findStringWithPrefixes(text, "// BINARY:");
    String expectedSourceFile = InTextDirectivesUtils.findStringWithPrefixes(text, "// SRC:");
    String expectedTarget = InTextDirectivesUtils.findStringWithPrefixes(text, "// TARGET:");
    assertEquals(expectedBinaryFile, getFileWithDir(resolved));
    assertEquals(expectedTarget, renderAsGotoImplementation(resolved));
    PsiElement srcElement = resolved.getNavigationElement();
    Assert.assertNotEquals(srcElement, resolved);
    assertEquals(expectedSourceFile, getFileWithDir(srcElement));
    assertEquals(expectedTarget, renderAsGotoImplementation(srcElement));
}
Also used : PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiPolyVariantReference

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

the class ImportTest method testImportModuleExceptNameArity.

public void testImportModuleExceptNameArity() {
    myFixture.configureByFiles("import_module_except_name_arity.ex", "imported.ex");
    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    assertNotNull(elementAtCaret);
    PsiElement maybeCall = elementAtCaret.getParent().getParent();
    assertInstanceOf(maybeCall, Call.class);
    Call call = (Call) maybeCall;
    assertEquals("imported", call.functionName());
    assertEquals(0, call.resolvedFinalArity());
    PsiReference reference = call.getReference();
    assertNotNull(reference);
    assertInstanceOf(reference, PsiPolyVariantReference.class);
    PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
    ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
    assertEquals(2, resolveResults.length);
}
Also used : Call(org.elixir_lang.psi.call.Call) PsiReference(com.intellij.psi.PsiReference) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference)

Aggregations

PsiPolyVariantReference (com.intellij.psi.PsiPolyVariantReference)20 PsiElement (com.intellij.psi.PsiElement)17 ResolveResult (com.intellij.psi.ResolveResult)10 PsiReference (com.intellij.psi.PsiReference)7 Call (org.elixir_lang.psi.call.Call)3 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)2 ASTNode (com.intellij.lang.ASTNode)1 JSFile (com.intellij.lang.javascript.psi.JSFile)1 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1