Search in sources :

Example 11 with PyClass

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

the class PyPushDownTest method testMultiFileImports.

// Tests that pushing down methods moves imports as well (PY-10963)
public void testMultiFileImports() {
    final String[] modules = { "child_module", "parent_module" };
    configureMultiFile(ArrayUtil.mergeArrays(modules, "shared_module"));
    final PyClass parentClass = findClass("Parent");
    final PyMemberInfo<PyElement> methodToMove = MembersManager.findMember(parentClass, findMember("Parent", ".should_be_pushed"));
    moveViaProcessor(myFixture.getProject(), new PyPushDownProcessor(myFixture.getProject(), Collections.singletonList(methodToMove), parentClass));
    checkMultiFile(modules);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyElement(com.jetbrains.python.psi.PyElement)

Example 12 with PyClass

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

the class CreateTestAction method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PyFunction srcFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    final PyClass srcClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    if (srcClass == null && srcFunction == null)
        return;
    final PsiDirectory dir = element.getContainingFile().getContainingDirectory();
    final CreateTestDialog d = new CreateTestDialog(project);
    if (srcClass != null) {
        d.setClassName("Test" + StringUtil.capitalize(srcClass.getName()));
        d.setFileName("test_" + StringUtil.decapitalize(srcClass.getName()) + ".py");
        if (dir != null)
            d.setTargetDir(dir.getVirtualFile().getPath());
        if (srcFunction != null) {
            d.methodsSize(1);
            d.addMethod("test_" + srcFunction.getName(), 0);
        } else {
            final List<PyFunction> methods = Lists.newArrayList();
            srcClass.visitMethods(pyFunction -> {
                if (pyFunction.getName() != null && !pyFunction.getName().startsWith("__"))
                    methods.add(pyFunction);
                return true;
            }, false, null);
            d.methodsSize(methods.size());
            int i = 0;
            for (PyFunction f : methods) {
                d.addMethod("test_" + f.getName(), i);
                ++i;
            }
        }
    } else {
        d.setClassName("Test" + StringUtil.capitalize(srcFunction.getName()));
        d.setFileName("test_" + StringUtil.decapitalize(srcFunction.getName()) + ".py");
        if (dir != null)
            d.setTargetDir(dir.getVirtualFile().getPath());
        d.methodsSize(1);
        d.addMethod("test_" + srcFunction.getName(), 0);
    }
    if (!d.showAndGet()) {
        return;
    }
    CommandProcessor.getInstance().executeCommand(project, () -> {
        PsiFile e = PyTestCreator.generateTestAndNavigate(project, d);
        final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        documentManager.commitAllDocuments();
    }, CodeInsightBundle.message("intention.create.test"), this);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 13 with PyClass

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

the class PyJavaSuperMethodsSearchExecutor method execute.

public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
    PyFunction func = queryParameters.getDerivedMethod();
    PyClass containingClass = func.getContainingClass();
    if (containingClass != null) {
        for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject()))) {
            if (type instanceof PyJavaClassType) {
                final PsiClass psiClass = ((PyJavaClassType) type).getPsiClass();
                PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
                // the Python method actually does override/implement all of Java super methods with the same name
                if (!ContainerUtil.process(methods, consumer))
                    return false;
            }
        }
    }
    return true;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType)

Example 14 with PyClass

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

the class PyCustomMember method resolve.

@Nullable
public PsiElement resolve(@NotNull final PsiElement context) {
    if (myTarget != null) {
        return myTarget;
    }
    PyClass targetClass = null;
    if (myTypeName != null) {
        final ParameterizedCachedValueProvider<PyClass, PsiElement> provider = new ParameterizedCachedValueProvider<PyClass, PsiElement>() {

            @Nullable
            @Override
            public CachedValueProvider.Result<PyClass> compute(final PsiElement param) {
                final PyClass result = PyPsiFacade.getInstance(param.getProject()).createClassByQName(myTypeName, param);
                return CachedValueProvider.Result.create(result, PsiModificationTracker.MODIFICATION_COUNT);
            }
        };
        targetClass = CachedValuesManager.getManager(context.getProject()).getParameterizedCachedValue(this, RESOLVE, provider, false, context);
    }
    final PsiElement resolveTarget = findResolveTarget(context);
    if (resolveTarget instanceof PyFunction && !myAlwaysResolveToCustomElement) {
        return resolveTarget;
    }
    if (resolveTarget != null || targetClass != null) {
        return new MyInstanceElement(targetClass, context, resolveTarget);
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement) ASTWrapperPsiElement(com.intellij.extapi.psi.ASTWrapperPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with PyClass

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

the class PyChangeSignatureQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PyFunction function = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyFunction.class);
    if (function == null)
        return;
    final PyClass cls = function.getContainingClass();
    assert cls != null;
    final String functionName = function.getName();
    final String complementaryName = PyNames.NEW.equals(functionName) ? PyNames.INIT : PyNames.NEW;
    final TypeEvalContext context = TypeEvalContext.userInitiated(project, descriptor.getEndElement().getContainingFile());
    final PyFunction complementaryMethod = myOverridenMethod ? (PyFunction) PySuperMethodsSearch.search(function, context).findFirst() : cls.findMethodByName(complementaryName, true, null);
    assert complementaryMethod != null;
    final PyMethodDescriptor methodDescriptor = new PyMethodDescriptor(function) {

        @Override
        public List<PyParameterInfo> getParameters() {
            final List<PyParameterInfo> parameterInfos = super.getParameters();
            final int paramLength = function.getParameterList().getParameters().length;
            final int complementaryParamLength = complementaryMethod.getParameterList().getParameters().length;
            if (complementaryParamLength > paramLength)
                parameterInfos.add(new PyParameterInfo(-1, "**kwargs", "", false));
            return parameterInfos;
        }
    };
    final PyChangeSignatureDialog dialog = new PyChangeSignatureDialog(project, methodDescriptor);
    dialog.show();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyChangeSignatureDialog(com.jetbrains.python.refactoring.changeSignature.PyChangeSignatureDialog) PyFunction(com.jetbrains.python.psi.PyFunction) PyMethodDescriptor(com.jetbrains.python.refactoring.changeSignature.PyMethodDescriptor) PyParameterInfo(com.jetbrains.python.refactoring.changeSignature.PyParameterInfo) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Aggregations

PyClass (com.jetbrains.python.psi.PyClass)88 PyFunction (com.jetbrains.python.psi.PyFunction)32 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)17 ArrayList (java.util.ArrayList)15 Nullable (org.jetbrains.annotations.Nullable)15 PyElement (com.jetbrains.python.psi.PyElement)12 PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)9 PyFile (com.jetbrains.python.psi.PyFile)8 PsiFile (com.intellij.psi.PsiFile)7 Project (com.intellij.openapi.project.Project)6 Editor (com.intellij.openapi.editor.Editor)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 MultiMap (com.intellij.util.containers.MultiMap)5 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)4 PyMemberInfoStorage (com.jetbrains.python.refactoring.classes.PyMemberInfoStorage)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 PyHierarchyNodeDescriptor (com.jetbrains.python.hierarchy.PyHierarchyNodeDescriptor)3 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)2