Search in sources :

Example 1 with PyFunction

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

the class PyUserSkeletonsClassMembersProvider method findClassMember.

public static PsiElement findClassMember(@NotNull PyClass cls, @NotNull String name, boolean isDefinition) {
    final PyFunction function = cls.findMethodByName(name, false, null);
    if (function != null) {
        final PyUtil.MethodFlags methodFlags = PyUtil.MethodFlags.of(function);
        final boolean instanceMethod = methodFlags == null || methodFlags.isInstanceMethod();
        if (isDefinition ^ instanceMethod) {
            return function;
        }
    }
    if (!isDefinition) {
        final PyTargetExpression instanceAttribute = cls.findInstanceAttribute(name, false);
        if (instanceAttribute != null) {
            return instanceAttribute;
        }
    }
    final PyTargetExpression classAttribute = cls.findClassAttribute(name, false, null);
    if (classAttribute != null) {
        return classAttribute;
    }
    return null;
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyFunction(com.jetbrains.python.psi.PyFunction) PyUtil(com.jetbrains.python.psi.PyUtil)

Example 2 with PyFunction

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

the class PyUserSkeletonsClassMembersProvider method getClassMembers.

public static Collection<PyCustomMember> getClassMembers(@NotNull PyClass cls, boolean isDefinition) {
    final List<PyCustomMember> result = new ArrayList<>();
    for (PyFunction function : cls.getMethods()) {
        final String name = function.getName();
        final PyUtil.MethodFlags methodFlags = PyUtil.MethodFlags.of(function);
        final boolean instanceMethod = methodFlags == null || methodFlags.isInstanceMethod();
        if (name != null && (isDefinition ^ instanceMethod)) {
            result.add(new PyCustomMember(name, function));
        }
    }
    if (!isDefinition) {
        for (PyTargetExpression attribute : cls.getInstanceAttributes()) {
            final String name = attribute.getName();
            if (name != null) {
                result.add(new PyCustomMember(name, attribute));
            }
        }
    }
    for (PyTargetExpression attribute : cls.getClassAttributes()) {
        final String name = attribute.getName();
        if (name != null) {
            result.add(new PyCustomMember(name, attribute));
        }
    }
    return result;
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyFunction(com.jetbrains.python.psi.PyFunction) PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) PyUtil(com.jetbrains.python.psi.PyUtil)

Example 3 with PyFunction

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

the class PyCallSignatureTypeProvider method getReturnType.

@Override
public Ref<PyType> getReturnType(@NotNull final PyCallable callable, @NotNull TypeEvalContext context) {
    if (callable instanceof PyFunction) {
        PyFunction function = (PyFunction) callable;
        PySignature signature = PySignatureCacheManager.getInstance(function.getProject()).findSignature(function);
        if (signature != null && signature.getReturnType() != null) {
            final String typeName = signature.getReturnType().getTypeQualifiedName();
            if (typeName != null) {
                final PyType type = PyTypeParser.getTypeByName(function, typeName);
                if (type != null) {
                    return Ref.create(PyDynamicallyEvaluatedType.create(type));
                }
            }
        }
    }
    return null;
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction)

Example 4 with PyFunction

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

the class PyCallHierarchyTest method doTestCallHierarchy.

private void doTestCallHierarchy(String... fileNames) throws Exception {
    configureByFiles(fileNames);
    final PsiElement targetElement = TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED);
    assert targetElement != null : "Cannot find referenced element";
    assert targetElement instanceof PyFunction : "Referenced element is not PyFunction";
    PyFunction function = (PyFunction) targetElement;
    checkHierarchyTreeStructure(function);
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PyFunction

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

the class PyChangeSignatureTest method changeSignature.

private void changeSignature(@Nullable String newName, @Nullable List<PyParameterInfo> parameters) {
    final PyChangeSignatureHandler changeSignatureHandler = new PyChangeSignatureHandler();
    final PyFunction function = (PyFunction) changeSignatureHandler.findTargetMember(myFixture.getFile(), myFixture.getEditor());
    assertNotNull(function);
    final PyFunction newFunction = PyChangeSignatureHandler.getSuperMethod(function);
    assertNotNull(newFunction);
    final PyMethodDescriptor method = new PyMethodDescriptor(newFunction);
    final TestPyChangeSignatureDialog dialog = new TestPyChangeSignatureDialog(newFunction.getProject(), method);
    try {
        if (newName != null) {
            dialog.setNewName(newName);
        }
        if (parameters != null) {
            dialog.setParameterInfos(parameters);
        }
        final String validationError = dialog.validateAndCommitData();
        assertTrue(validationError, validationError == null);
        final BaseRefactoringProcessor baseRefactoringProcessor = dialog.createRefactoringProcessor();
        assert baseRefactoringProcessor instanceof PyChangeSignatureProcessor;
        final PyChangeSignatureProcessor processor = (PyChangeSignatureProcessor) baseRefactoringProcessor;
        processor.run();
    } finally {
        Disposer.dispose(dialog.getDisposable());
    }
}
Also used : BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) PyFunction(com.jetbrains.python.psi.PyFunction)

Aggregations

PyFunction (com.jetbrains.python.psi.PyFunction)61 PyClass (com.jetbrains.python.psi.PyClass)33 PsiElement (com.intellij.psi.PsiElement)24 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)10 PyFile (com.jetbrains.python.psi.PyFile)9 ArrayList (java.util.ArrayList)8 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)7 PsiFile (com.intellij.psi.PsiFile)5 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 ASTNode (com.intellij.lang.ASTNode)2 ItemPresentation (com.intellij.navigation.ItemPresentation)2 Pair (com.intellij.openapi.util.Pair)2