Search in sources :

Example 6 with PyClassType

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

the class PyOperatorReference method resolveMember.

@NotNull
private List<RatedResolveResult> resolveMember(@Nullable PyExpression object, @Nullable String name) {
    final ArrayList<RatedResolveResult> results = new ArrayList<>();
    if (object != null && name != null) {
        final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
        PyType type = typeEvalContext.getType(object);
        typeEvalContext.trace("Side text is %s, type is %s", object.getText(), type);
        if (type instanceof PyClassLikeType) {
            if (((PyClassLikeType) type).isDefinition()) {
                type = ((PyClassLikeType) type).getMetaClassType(typeEvalContext, true);
            }
        }
        if (type != null) {
            List<? extends RatedResolveResult> res = type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
            if (res != null && res.size() > 0) {
                results.addAll(res);
            } else if (typeEvalContext.tracing()) {
                VirtualFile vFile = null;
                if (type instanceof PyClassType) {
                    final PyClass pyClass = ((PyClassType) type).getPyClass();
                    vFile = pyClass.getContainingFile().getVirtualFile();
                }
                type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
                typeEvalContext.trace("Could not resolve member %s in type %s from file %s", name, type, vFile);
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PyClassType

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

the class AddFieldQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    // expect the descriptor to point to the unresolved identifier.
    final PsiElement element = descriptor.getPsiElement();
    final PyClassType type = getClassType(element);
    if (type == null)
        return;
    final PyClass cls = type.getPyClass();
    if (!FileModificationService.getInstance().preparePsiElementForWrite(cls))
        return;
    WriteAction.run(() -> {
        PsiElement initStatement;
        if (!type.isDefinition()) {
            initStatement = addFieldToInit(project, cls, myIdentifier, new CreateFieldCallback(project, myIdentifier, myInitializer));
        } else {
            PyStatement field = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyStatement.class, myIdentifier + " = " + myInitializer);
            initStatement = PyUtil.addElementToStatementList(field, cls.getStatementList(), true);
        }
        if (initStatement != null) {
            showTemplateBuilder(initStatement, cls.getContainingFile());
            return;
        }
        // somehow we failed. tell about this
        PyUtil.showBalloon(project, PyBundle.message("QFIX.failed.to.add.field"), MessageType.ERROR);
    });
}
Also used : PyClassType(com.jetbrains.python.psi.types.PyClassType) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PyClassType

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

the class AddMethodQuickFix method getClassType.

private static PyClassType getClassType(@NotNull final PsiElement problemElement) {
    if ((problemElement instanceof PyQualifiedExpression)) {
        final PyExpression qualifier = ((PyQualifiedExpression) problemElement).getQualifier();
        if (qualifier == null)
            return null;
        final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier);
        return type instanceof PyClassType ? (PyClassType) type : null;
    }
    final PyClass pyClass = PsiTreeUtil.getParentOfType(problemElement, PyClass.class);
    return pyClass != null ? new PyClassTypeImpl(pyClass, false) : null;
}
Also used : PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType)

Aggregations

PyClassType (com.jetbrains.python.psi.types.PyClassType)8 PyType (com.jetbrains.python.psi.types.PyType)6 PsiElement (com.intellij.psi.PsiElement)4 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)2 Document (com.intellij.openapi.editor.Document)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 PyStringFormatParser (com.jetbrains.python.inspections.PyStringFormatParser)1 PyFunctionBuilder (com.jetbrains.python.psi.impl.PyFunctionBuilder)1 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)1 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)1 PyModuleType (com.jetbrains.python.psi.types.PyModuleType)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1