Search in sources :

Example 31 with QualifiedName

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

the class DocStringTypeReference method bindToElement.

@Nullable
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    if (element.equals(resolve())) {
        return element;
    }
    if (myElement instanceof PyStringLiteralExpression && element instanceof PyClass) {
        final PyStringLiteralExpression e = (PyStringLiteralExpression) myElement;
        final PyClass cls = (PyClass) element;
        QualifiedName qname = QualifiedNameFinder.findCanonicalImportPath(cls, element);
        if (qname != null) {
            qname = qname.append(cls.getName());
            ElementManipulator<PyStringLiteralExpression> manipulator = ElementManipulators.getManipulator(e);
            myType = new PyClassTypeImpl(cls, false);
            return manipulator.handleContentChange(e, myFullRange, qname.toString());
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with QualifiedName

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

the class PythonDocumentationProvider method promptToConfigureDocumentation.

@Override
public void promptToConfigureDocumentation(@NotNull PsiElement element) {
    final Project project = element.getProject();
    final QualifiedName qName = QualifiedNameFinder.findCanonicalImportPath(element, element);
    if (qName != null && qName.getComponentCount() > 0) {
        ApplicationManager.getApplication().invokeLater(() -> {
            final int rc = Messages.showOkCancelDialog(project, "No external documentation URL configured for module " + qName.getComponents().get(0) + ".\nWould you like to configure it now?", "Python External Documentation", Messages.getQuestionIcon());
            if (rc == Messages.OK) {
                ShowSettingsUtilImpl.showSettingsDialog(project, PythonDocumentationConfigurable.ID, "");
            }
        }, ModalityState.NON_MODAL);
    }
}
Also used : Project(com.intellij.openapi.project.Project) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 33 with QualifiedName

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

the class PyReferenceExpressionImpl method getGenericTypeFromTarget.

@Nullable
private static PyType getGenericTypeFromTarget(@NotNull PsiElement target, @NotNull TypeEvalContext context, @NotNull PyReferenceExpression anchor) {
    if (!(target instanceof PyTargetExpression)) {
        // PyTargetExpression will ask about its type itself
        final PyType pyType = getReferenceTypeFromProviders(target, context, anchor);
        if (pyType != null) {
            return pyType;
        }
    }
    if (target instanceof PyTargetExpression) {
        final String name = ((PyTargetExpression) target).getName();
        if (PyNames.NONE.equals(name)) {
            return PyNoneType.INSTANCE;
        }
        if (PyNames.TRUE.equals(name) || PyNames.FALSE.equals(name)) {
            return PyBuiltinCache.getInstance(target).getBoolType();
        }
    }
    if (target instanceof PyFile) {
        return new PyModuleType((PyFile) target);
    }
    if ((target instanceof PyTargetExpression || target instanceof PyNamedParameter) && context.allowDataFlow(anchor)) {
        final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(anchor);
        if (scopeOwner != null && scopeOwner == ScopeUtil.getScopeOwner(target)) {
            final String name = ((PyElement) target).getName();
            if (name != null) {
                final PyType type = getTypeByControlFlow(name, context, anchor, scopeOwner);
                if (type != null) {
                    return type;
                }
            }
        }
    }
    if (target instanceof PyFunction) {
        final PyDecoratorList decoratorList = ((PyFunction) target).getDecoratorList();
        if (decoratorList != null) {
            final PyDecorator propertyDecorator = decoratorList.findDecorator(PyNames.PROPERTY);
            if (propertyDecorator != null) {
                return PyBuiltinCache.getInstance(target).getObjectType(PyNames.PROPERTY);
            }
            for (PyDecorator decorator : decoratorList.getDecorators()) {
                final QualifiedName qName = decorator.getQualifiedName();
                if (qName != null && (qName.endsWith(PyNames.SETTER) || qName.endsWith(PyNames.DELETER) || qName.endsWith(PyNames.GETTER))) {
                    return PyBuiltinCache.getInstance(target).getObjectType(PyNames.PROPERTY);
                }
            }
        }
    }
    if (target instanceof PyTypedElement) {
        return context.getType((PyTypedElement) target);
    }
    if (target instanceof PsiDirectory) {
        final PsiDirectory dir = (PsiDirectory) target;
        final PsiFile file = dir.findFile(PyNames.INIT_DOT_PY);
        if (file != null) {
            return getTypeFromTarget(file, context, anchor);
        }
        if (PyUtil.isPackage(dir, anchor)) {
            final PsiFile containingFile = anchor.getContainingFile();
            if (containingFile instanceof PyFile) {
                final QualifiedName qualifiedName = QualifiedNameFinder.findShortestImportableQName(dir);
                if (qualifiedName != null) {
                    final PyImportedModule module = new PyImportedModule(null, (PyFile) containingFile, qualifiedName);
                    return new PyImportedModuleType(module);
                }
            }
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with QualifiedName

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

the class PyReferenceExpressionImpl method getQualifiedReferenceTypeByControlFlow.

@Nullable
private PyType getQualifiedReferenceTypeByControlFlow(@NotNull TypeEvalContext context) {
    PyExpression qualifier = getQualifier();
    if (context.allowDataFlow(this) && qualifier != null) {
        PyExpression next = qualifier;
        while (next != null) {
            qualifier = next;
            next = qualifier instanceof PyQualifiedExpression ? ((PyQualifiedExpression) qualifier).getQualifier() : null;
        }
        final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(this);
        final QualifiedName qname = asQualifiedName();
        if (qname != null && scopeOwner != null) {
            return getTypeByControlFlow(qname.toString(), context, qualifier, scopeOwner);
        }
    }
    return null;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with QualifiedName

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

the class PyImportReference method resolveInner.

@NotNull
@Override
protected List<RatedResolveResult> resolveInner() {
    //importRef.getParent();
    final PyImportElement parent = PsiTreeUtil.getParentOfType(myElement, PyImportElement.class);
    final QualifiedName qname = myElement.asQualifiedName();
    return qname == null ? Collections.<RatedResolveResult>emptyList() : ResolveImportUtil.resolveNameInImportStatement(parent, qname);
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

QualifiedName (com.intellij.psi.util.QualifiedName)63 Nullable (org.jetbrains.annotations.Nullable)22 NotNull (org.jetbrains.annotations.NotNull)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Project (com.intellij.openapi.project.Project)8 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)7 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFile (com.intellij.psi.PsiFile)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 StringRef (com.intellij.util.io.StringRef)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1