Search in sources :

Example 1 with PyFunctionStub

use of com.jetbrains.python.psi.stubs.PyFunctionStub 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 PyFunctionStub

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

the class PyFunctionImpl method getTypeCommentAnnotation.

@Nullable
@Override
public String getTypeCommentAnnotation() {
    final PyFunctionStub stub = getStub();
    if (stub != null) {
        return stub.getTypeComment();
    }
    final PsiComment comment = getTypeComment();
    if (comment != null) {
        return PyTypingTypeProvider.getTypeCommentValue(comment.getText());
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PyFunctionStub

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

the class PyFunctionImpl method getName.

@Nullable
@Override
public String getName() {
    final PyFunctionStub stub = getStub();
    if (stub != null) {
        return stub.getName();
    }
    ASTNode node = getNameNode();
    return node != null ? node.getText() : null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PyFunctionStub

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

the class PyFunctionImpl method getModifier.

/**
   * Looks for two standard decorators to a function, or a wrapping assignment that closely follows it.
   *
   * @return a flag describing what was detected.
   */
@Nullable
public Modifier getModifier() {
    final String deconame = getClassOrStaticMethodDecorator();
    if (PyNames.CLASSMETHOD.equals(deconame)) {
        return CLASSMETHOD;
    } else if (PyNames.STATICMETHOD.equals(deconame)) {
        return STATICMETHOD;
    }
    // implicit staticmethod __new__
    final PyClass cls = getContainingClass();
    if (cls != null && PyNames.NEW.equals(getName()) && cls.isNewStyleClass(null)) {
        return STATICMETHOD;
    }
    final PyFunctionStub stub = getStub();
    if (stub != null) {
        return getModifierFromStub(stub);
    }
    final String funcName = getName();
    if (funcName != null) {
        PyAssignmentStatement currentAssignment = PsiTreeUtil.getNextSiblingOfType(this, PyAssignmentStatement.class);
        while (currentAssignment != null) {
            final String modifier = currentAssignment.getTargetsToValuesMapping().stream().filter(pair -> pair.getFirst() instanceof PyTargetExpression && funcName.equals(pair.getFirst().getName())).filter(pair -> pair.getSecond() instanceof PyCallExpression).map(pair -> interpretAsModifierWrappingCall((PyCallExpression) pair.getSecond(), this)).filter(interpreted -> interpreted != null && interpreted.getSecond() == this).map(interpreted -> interpreted.getFirst()).filter(wrapperName -> PyNames.CLASSMETHOD.equals(wrapperName) || PyNames.STATICMETHOD.equals(wrapperName)).findAny().orElse(null);
            if (PyNames.CLASSMETHOD.equals(modifier)) {
                return CLASSMETHOD;
            } else if (PyNames.STATICMETHOD.equals(modifier)) {
                return STATICMETHOD;
            }
            currentAssignment = PsiTreeUtil.getNextSiblingOfType(currentAssignment, PyAssignmentStatement.class);
        }
    }
    return null;
}
Also used : QualifiedNameFinder(com.jetbrains.python.psi.resolve.QualifiedNameFinder) PyNames(com.jetbrains.python.PyNames) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) JBIterable(com.intellij.util.containers.JBIterable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) SearchScope(com.intellij.psi.search.SearchScope) PyElementTypes(com.jetbrains.python.PyElementTypes) AtomicReference(java.util.concurrent.atomic.AtomicReference) STATICMETHOD(com.jetbrains.python.psi.PyFunction.Modifier.STATICMETHOD) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PyUtil.as(com.jetbrains.python.psi.PyUtil.as) StringUtil.notNullize(com.intellij.openapi.util.text.StringUtil.notNullize) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiElement(com.intellij.psi.PsiElement) ControlFlowCache(com.jetbrains.python.codeInsight.controlflow.ControlFlowCache) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PythonIcons(icons.PythonIcons) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) DocStringUtil(com.jetbrains.python.documentation.docstrings.DocStringUtil) PyTypingTypeProvider(com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider) PlatformIcons(com.intellij.util.PlatformIcons) Extensions(com.intellij.openapi.extensions.Extensions) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.psi.util(com.intellij.psi.util) CLASSMETHOD(com.jetbrains.python.psi.PyFunction.Modifier.CLASSMETHOD) IStubElementType(com.intellij.psi.stubs.IStubElementType) Key(com.intellij.openapi.util.Key) PsiReference(com.intellij.psi.PsiReference) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyClassStub(com.jetbrains.python.psi.stubs.PyClassStub) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) PyResolveImportUtil(com.jetbrains.python.psi.resolve.PyResolveImportUtil) PyTargetExpressionStub(com.jetbrains.python.psi.stubs.PyTargetExpressionStub) PsiComment(com.intellij.psi.PsiComment) PyCallExpressionHelper.interpretAsModifierWrappingCall(com.jetbrains.python.psi.impl.PyCallExpressionHelper.interpretAsModifierWrappingCall) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) javax.swing(javax.swing) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PyFunctionStub

use of com.jetbrains.python.psi.stubs.PyFunctionStub 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)

Aggregations

PyFunctionStub (com.jetbrains.python.psi.stubs.PyFunctionStub)6 PsiElement (com.intellij.psi.PsiElement)3 StubElement (com.intellij.psi.stubs.StubElement)3 PyClassStub (com.jetbrains.python.psi.stubs.PyClassStub)3 Nullable (org.jetbrains.annotations.Nullable)3 ASTNode (com.intellij.lang.ASTNode)2 PsiComment (com.intellij.psi.PsiComment)2 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 ItemPresentation (com.intellij.navigation.ItemPresentation)1 Extensions (com.intellij.openapi.extensions.Extensions)1 Key (com.intellij.openapi.util.Key)1 Ref (com.intellij.openapi.util.Ref)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 StringUtil.notNullize (com.intellij.openapi.util.text.StringUtil.notNullize)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiReference (com.intellij.psi.PsiReference)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 IStubElementType (com.intellij.psi.stubs.IStubElementType)1