Search in sources :

Example 1 with PyClassStub

use of com.jetbrains.python.psi.stubs.PyClassStub in project intellij-community by JetBrains.

the class PyTargetExpressionImpl method getContainingClass.

@Override
public PyClass getContainingClass() {
    final PyTargetExpressionStub stub = getStub();
    if (stub != null) {
        final StubElement parentStub = stub.getParentStub();
        if (parentStub instanceof PyClassStub) {
            return ((PyClassStub) parentStub).getPsi();
        }
        if (parentStub instanceof PyFunctionStub) {
            final StubElement functionParent = parentStub.getParentStub();
            if (functionParent instanceof PyClassStub) {
                return ((PyClassStub) functionParent).getPsi();
            }
        }
        return null;
    }
    final PsiElement parent = PsiTreeUtil.getParentOfType(this, PyFunction.class, PyClass.class);
    if (parent instanceof PyClass) {
        return (PyClass) parent;
    }
    if (parent instanceof PyFunction) {
        return ((PyFunction) parent).getContainingClass();
    }
    return null;
}
Also used : PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) PyTargetExpressionStub(com.jetbrains.python.psi.stubs.PyTargetExpressionStub) StubElement(com.intellij.psi.stubs.StubElement) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyClassStub

use of com.jetbrains.python.psi.stubs.PyClassStub in project intellij-community by JetBrains.

the class PyClassImpl method getSuperClassTypes.

@NotNull
@Override
public List<PyClassLikeType> getSuperClassTypes(@NotNull final TypeEvalContext context) {
    if (PyNames.TYPES_INSTANCE_TYPE.equals(getQualifiedName())) {
        return Collections.emptyList();
    }
    final PyClassStub stub = getStub();
    final List<PyClassLikeType> result = new ArrayList<>();
    // In some cases stub may not provide all information, so we use stubs only if AST access id disabled
    if (!context.maySwitchToAST(this)) {
        fillSuperClassesNoSwitchToAst(context, stub, result);
    } else {
        fillSuperClassesSwitchingToAst(context, result);
    }
    final PyBuiltinCache builtinCache = PyBuiltinCache.getInstance(this);
    PyPsiUtils.assertValid(this);
    if (result.isEmpty() && isValid() && !builtinCache.isBuiltin(this)) {
        final PyClass implicitSuper;
        if (LanguageLevel.forElement(this).isOlderThan(LanguageLevel.PYTHON30) && getMetaClassQName() == null) {
            implicitSuper = as(resolveTopLevelMember(QualifiedName.fromDottedString(PyNames.TYPES_INSTANCE_TYPE), fromFoothold(this)), PyClass.class);
        } else {
            implicitSuper = builtinCache.getClass(PyNames.OBJECT);
        }
        if (implicitSuper != null) {
            final PyType type = context.getType(implicitSuper);
            if (type instanceof PyClassLikeType) {
                result.add((PyClassLikeType) type);
            }
        }
    }
    return result;
}
Also used : PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyClassStub

use of com.jetbrains.python.psi.stubs.PyClassStub in project intellij-community by JetBrains.

the class PyClassImpl method getClassChildren.

protected <T extends PsiElement> T[] getClassChildren(TokenSet elementTypes, ArrayFactory<T> factory) {
    // TODO: gather all top-level functions, maybe within control statements
    final PyClassStub classStub = getStub();
    if (classStub != null) {
        return classStub.getChildrenByType(elementTypes, factory);
    }
    List<T> result = new ArrayList<>();
    final PyStatementList statementList = getStatementList();
    for (PsiElement element : statementList.getChildren()) {
        if (elementTypes.contains(element.getNode().getElementType())) {
            //noinspection unchecked
            result.add((T) element);
        }
    }
    return result.toArray(factory.create(result.size()));
}
Also used : PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub)

Example 4 with PyClassStub

use of com.jetbrains.python.psi.stubs.PyClassStub in project intellij-community by JetBrains.

the class PyFunctionImpl method getContainingClass.

public PyClass getContainingClass() {
    final PyFunctionStub stub = getStub();
    if (stub != null) {
        final StubElement parentStub = stub.getParentStub();
        if (parentStub instanceof PyClassStub) {
            return ((PyClassStub) parentStub).getPsi();
        }
        return null;
    }
    final PsiElement parent = PsiTreeUtil.getParentOfType(this, StubBasedPsiElement.class);
    if (parent instanceof PyClass) {
        return (PyClass) parent;
    }
    return null;
}
Also used : PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) StubElement(com.intellij.psi.stubs.StubElement) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) PsiElement(com.intellij.psi.PsiElement) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement)

Example 5 with PyClassStub

use of com.jetbrains.python.psi.stubs.PyClassStub in project intellij-community by JetBrains.

the class PyClassImpl method processStubProperties.

@Nullable
private Property processStubProperties(@Nullable String name, @Nullable Processor<Property> propertyProcessor) {
    final PyClassStub stub = getStub();
    if (stub != null) {
        for (StubElement subStub : stub.getChildrenStubs()) {
            if (subStub.getStubType() == PyElementTypes.TARGET_EXPRESSION) {
                final PyTargetExpressionStub targetStub = (PyTargetExpressionStub) subStub;
                PropertyStubStorage prop = targetStub.getCustomStub(PropertyStubStorage.class);
                if (prop != null && (name == null || name.equals(targetStub.getName()))) {
                    Maybe<PyCallable> getter = fromPacked(prop.getGetter());
                    Maybe<PyCallable> setter = fromPacked(prop.getSetter());
                    Maybe<PyCallable> deleter = fromPacked(prop.getDeleter());
                    String doc = prop.getDoc();
                    if (getter != NONE || setter != NONE || deleter != NONE) {
                        final PropertyImpl property = new PropertyImpl(targetStub.getName(), getter, setter, deleter, doc, targetStub.getPsi());
                        if (propertyProcessor == null || propertyProcessor.process(property))
                            return property;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PropertyStubStorage(com.jetbrains.python.psi.stubs.PropertyStubStorage) PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) PyTargetExpressionStub(com.jetbrains.python.psi.stubs.PyTargetExpressionStub) StubElement(com.intellij.psi.stubs.StubElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyClassStub (com.jetbrains.python.psi.stubs.PyClassStub)6 StubElement (com.intellij.psi.stubs.StubElement)3 PsiElement (com.intellij.psi.PsiElement)2 PyFunctionStub (com.jetbrains.python.psi.stubs.PyFunctionStub)2 PyTargetExpressionStub (com.jetbrains.python.psi.stubs.PyTargetExpressionStub)2 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 PropertyStubStorage (com.jetbrains.python.psi.stubs.PropertyStubStorage)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1