Search in sources :

Example 1 with PsiPolyVariantReference

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

the class NestedTest method testReference.

public void testReference() {
    myFixture.configureByFiles("reference.ex", "suffix.ex", "nested.ex");
    PsiElement alias = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent().getPrevSibling();
    assertInstanceOf(alias, QualifiedAlias.class);
    PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) alias.getReference();
    assertNotNull(polyVariantReference);
    ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
    assertEquals(2, resolveResults.length);
    // alias
    assertEquals("alias Prefix.Suffix, as: As", resolveResults[0].getElement().getParent().getParent().getParent().getParent().getParent().getText());
    // defmodule
    assertEquals("defmodule Prefix.Suffix.Nested do\nend", resolveResults[1].getElement().getText());
}
Also used : ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference)

Example 2 with PsiPolyVariantReference

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

the class ImportTest method testImportModule.

/*
     * Tests
     */
public void testImportModule() {
    myFixture.configureByFiles("import_module.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)

Example 3 with PsiPolyVariantReference

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

the class ImportTest method testImportModuleOnlyNameArity.

public void testImportModuleOnlyNameArity() {
    myFixture.configureByFiles("import_module_only_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)

Example 4 with PsiPolyVariantReference

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

the class PyTypingTypeProvider method tryResolving.

@NotNull
private static List<PsiElement> tryResolving(@NotNull PyExpression expression, @NotNull TypeEvalContext context) {
    final List<PsiElement> elements = Lists.newArrayList();
    if (expression instanceof PyReferenceExpression) {
        final PyReferenceExpression referenceExpr = (PyReferenceExpression) expression;
        final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
        final PsiPolyVariantReference reference = referenceExpr.getReference(resolveContext);
        final List<PsiElement> resolved = PyUtil.multiResolveTopPriority(reference);
        for (PsiElement element : resolved) {
            if (element instanceof PyFunction) {
                final PyFunction function = (PyFunction) element;
                if (PyUtil.isInit(function)) {
                    final PyClass cls = function.getContainingClass();
                    if (cls != null) {
                        elements.add(cls);
                        continue;
                    }
                }
            }
            final String name = element != null ? getQualifiedName(element) : null;
            if (name != null && OPAQUE_NAMES.contains(name)) {
                elements.add(element);
                continue;
            }
            if (element instanceof PyTargetExpression) {
                final PyTargetExpression targetExpr = (PyTargetExpression) element;
                // XXX: Requires switching from stub to AST
                final PyExpression assignedValue = targetExpr.findAssignedValue();
                if (assignedValue != null) {
                    elements.add(assignedValue);
                    continue;
                }
            }
            if (element != null) {
                elements.add(element);
            }
        }
    }
    return !elements.isEmpty() ? elements : Collections.singletonList(expression);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiPolyVariantReference

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

the class PyAddExceptionSuperClassQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyCallExpression) {
        PyExpression callee = ((PyCallExpression) element).getCallee();
        if (callee instanceof PyReferenceExpression) {
            final PsiPolyVariantReference reference = ((PyReferenceExpression) callee).getReference();
            PsiElement psiElement = reference.resolve();
            if (psiElement instanceof PyClass) {
                final PyElementGenerator generator = PyElementGenerator.getInstance(project);
                final PyArgumentList list = ((PyClass) psiElement).getSuperClassExpressionList();
                if (list != null) {
                    final PyExpression exception = generator.createExpressionFromText(LanguageLevel.forElement(element), "Exception");
                    list.addArgument(exception);
                } else {
                    final PyArgumentList expressionList = generator.createFromText(LanguageLevel.forElement(element), PyClass.class, "class A(Exception): pass").getSuperClassExpressionList();
                    assert expressionList != null;
                    final ASTNode nameNode = ((PyClass) psiElement).getNameNode();
                    assert nameNode != null;
                    psiElement.addAfter(expressionList, nameNode.getPsi());
                }
            }
        }
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) 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