Search in sources :

Example 1 with CLASSMETHOD

use of com.jetbrains.python.psi.PyFunction.Modifier.CLASSMETHOD 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)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 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 PsiComment (com.intellij.psi.PsiComment)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 IStubElementType (com.intellij.psi.stubs.IStubElementType)1 StubElement (com.intellij.psi.stubs.StubElement)1 com.intellij.psi.util (com.intellij.psi.util)1 ArrayUtil (com.intellij.util.ArrayUtil)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 PlatformIcons (com.intellij.util.PlatformIcons)1