Search in sources :

Example 1 with PyModuleType

use of com.jetbrains.python.psi.types.PyModuleType in project intellij-community by JetBrains.

the class PyDebugProcess method typeToPosition.

@Nullable
private static XSourcePosition typeToPosition(PyType pyType) {
    final PyClassType classType = PyUtil.as(pyType, PyClassType.class);
    if (classType != null) {
        return XDebuggerUtil.getInstance().createPositionByElement(classType.getPyClass());
    }
    final PyModuleType moduleType = PyUtil.as(pyType, PyModuleType.class);
    if (moduleType != null) {
        return XDebuggerUtil.getInstance().createPositionByElement(moduleType.getModule());
    }
    return null;
}
Also used : PyClassType(com.jetbrains.python.psi.types.PyClassType) PyModuleType(com.jetbrains.python.psi.types.PyModuleType) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyModuleType

use of com.jetbrains.python.psi.types.PyModuleType in project intellij-community by JetBrains.

the class ImportFromToImportIntention method isAvailable.

public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!(file instanceof PyFile)) {
        return false;
    }
    InfoHolder info = InfoHolder.collect(getElementFromEditor(editor, file));
    info.myModuleReference = null;
    final PsiElement position = file.findElementAt(editor.getCaretModel().getOffset());
    info.myFromImportStatement = PsiTreeUtil.getParentOfType(position, PyFromImportStatement.class);
    PyPsiUtils.assertValid(info.myFromImportStatement);
    if (info.myFromImportStatement != null && !info.myFromImportStatement.isFromFuture()) {
        info.myRelativeLevel = info.myFromImportStatement.getRelativeLevel();
        info.myModuleReference = info.myFromImportStatement.getImportSource();
        if (info.myRelativeLevel > 0) {
            // make sure we aren't importing a module from the relative path
            for (PyImportElement import_element : info.myFromImportStatement.getImportElements()) {
                PyReferenceExpression ref = import_element.getImportReferenceExpression();
                PyPsiUtils.assertValid(ref);
                if (ref != null) {
                    PsiElement target = ref.getReference().resolve();
                    final TypeEvalContext context = TypeEvalContext.codeAnalysis(file.getProject(), file);
                    if (target instanceof PyExpression && context.getType((PyExpression) target) instanceof PyModuleType) {
                        return false;
                    }
                }
            }
        }
    }
    if (info.myModuleReference != null) {
        info.myModuleName = PyPsiUtils.toPath(info.myModuleReference);
    }
    if (info.myModuleReference != null && info.myModuleName != null && info.myFromImportStatement != null) {
        setText(info.getText());
        return true;
    }
    return false;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyModuleType(com.jetbrains.python.psi.types.PyModuleType)

Example 3 with PyModuleType

use of com.jetbrains.python.psi.types.PyModuleType in project intellij-community by JetBrains.

the class AddFunctionQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        final PsiElement problemElement = descriptor.getPsiElement();
        if (!(problemElement instanceof PyQualifiedExpression))
            return;
        final PyExpression qualifier = ((PyQualifiedExpression) problemElement).getQualifier();
        if (qualifier == null)
            return;
        final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier);
        if (!(type instanceof PyModuleType))
            return;
        final PyFile file = ((PyModuleType) type).getModule();
        sure(file);
        sure(FileModificationService.getInstance().preparePsiElementForWrite(file));
        // try to at least match parameter count
        // TODO: get parameter style from code style
        PyFunctionBuilder builder = new PyFunctionBuilder(myIdentifier, problemElement);
        PsiElement problemParent = problemElement.getParent();
        if (problemParent instanceof PyCallExpression) {
            PyArgumentList arglist = ((PyCallExpression) problemParent).getArgumentList();
            if (arglist == null)
                return;
            final PyExpression[] args = arglist.getArguments();
            for (PyExpression arg : args) {
                if (arg instanceof PyKeywordArgument) {
                    // foo(bar) -> def foo(bar_1)
                    builder.parameter(((PyKeywordArgument) arg).getKeyword());
                } else if (arg instanceof PyReferenceExpression) {
                    PyReferenceExpression refex = (PyReferenceExpression) arg;
                    builder.parameter(refex.getReferencedName());
                } else {
                    // use a boring name
                    builder.parameter("param");
                }
            }
        } else if (problemParent != null) {
            for (PyInspectionExtension extension : Extensions.getExtensions(PyInspectionExtension.EP_NAME)) {
                List<String> params = extension.getFunctionParametersFromUsage(problemElement);
                if (params != null) {
                    for (String param : params) {
                        builder.parameter(param);
                    }
                    break;
                }
            }
        }
        // else: no arglist, use empty args
        PyFunction function = builder.buildFunction(project, LanguageLevel.forElement(file));
        // add to the bottom
        function = (PyFunction) file.add(function);
        showTemplateBuilder(function, file);
    } catch (IncorrectOperationException ignored) {
        // we failed. tell about this
        PyUtil.showBalloon(project, PyBundle.message("QFIX.failed.to.add.function"), MessageType.ERROR);
    }
}
Also used : PyInspectionExtension(com.jetbrains.python.inspections.PyInspectionExtension) PyType(com.jetbrains.python.psi.types.PyType) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) PyModuleType(com.jetbrains.python.psi.types.PyModuleType)

Example 4 with PyModuleType

use of com.jetbrains.python.psi.types.PyModuleType in project intellij-community by JetBrains.

the class PyStarImportElementImpl method calculateMultiResolveName.

@NotNull
private List<RatedResolveResult> calculateMultiResolveName(@NotNull String name) {
    if (PyUtil.isClassPrivateName(name)) {
        return Collections.emptyList();
    }
    final PsiElement parent = getParentByStub();
    if (parent instanceof PyFromImportStatement) {
        PyFromImportStatement fromImportStatement = (PyFromImportStatement) parent;
        final List<PsiElement> importedFiles = fromImportStatement.resolveImportSourceCandidates();
        for (PsiElement importedFile : new HashSet<>(importedFiles)) {
            // resolver gives lots of duplicates
            final PyFile sourceFile = as(PyUtil.turnDirIntoInit(importedFile), PyFile.class);
            if (sourceFile != null && PyUtil.isStarImportableFrom(name, sourceFile)) {
                final PyModuleType moduleType = new PyModuleType(sourceFile);
                final List<? extends RatedResolveResult> results = moduleType.resolveMember(name, null, AccessDirection.READ, PyResolveContext.defaultContext());
                if (results != null && !results.isEmpty()) {
                    return Lists.newArrayList(results);
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) PyModuleType(com.jetbrains.python.psi.types.PyModuleType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PyModuleType

use of com.jetbrains.python.psi.types.PyModuleType in project intellij-community by JetBrains.

the class ResolveImportUtil method resolveModuleMember.

@NotNull
private static List<RatedResolveResult> resolveModuleMember(@NotNull PyFile file, @NotNull String referencedName) {
    final PyModuleType moduleType = new PyModuleType(file);
    final PyResolveContext resolveContext = PyResolveContext.defaultContext();
    final List<? extends RatedResolveResult> results = moduleType.resolveMember(referencedName, null, AccessDirection.READ, resolveContext);
    if (results == null) {
        return Collections.emptyList();
    }
    return Lists.newArrayList(results);
}
Also used : PyModuleType(com.jetbrains.python.psi.types.PyModuleType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyModuleType (com.jetbrains.python.psi.types.PyModuleType)5 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashSet (com.intellij.util.containers.HashSet)1 PyInspectionExtension (com.jetbrains.python.inspections.PyInspectionExtension)1 PyFunctionBuilder (com.jetbrains.python.psi.impl.PyFunctionBuilder)1 PyClassType (com.jetbrains.python.psi.types.PyClassType)1 PyType (com.jetbrains.python.psi.types.PyType)1 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1