Search in sources :

Example 6 with PyFunction

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

the class PyChangeSignatureTest method doValidationTest.

public void doValidationTest(@Nullable String newName, @Nullable List<PyParameterInfo> parameters, @Nullable String expected) {
    myFixture.configureByFile("refactoring/changeSignature/" + getTestName(true) + ".py");
    final PyChangeSignatureHandler changeSignatureHandler = new PyChangeSignatureHandler();
    final PyFunction function = (PyFunction) changeSignatureHandler.findTargetMember(myFixture.getFile(), myFixture.getEditor());
    assertNotNull(function);
    final PyMethodDescriptor method = new PyMethodDescriptor(function);
    final TestPyChangeSignatureDialog dialog = new TestPyChangeSignatureDialog(function.getProject(), method);
    try {
        if (newName != null) {
            dialog.setNewName(newName);
        }
        if (parameters != null) {
            dialog.setParameterInfos(parameters);
        }
        final String validationError = dialog.validateAndCommitData();
        assertEquals(expected, validationError);
    } finally {
        Disposer.dispose(dialog.getDisposable());
    }
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction)

Example 7 with PyFunction

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

the class PyMakeFunctionTopLevelTest method runRefactoring.

private void runRefactoring(@Nullable String destination, @Nullable String errorMessage) {
    final PyFunction function = assertInstanceOf(myFixture.getElementAtCaret(), PyFunction.class);
    if (destination == null) {
        destination = PyPsiUtils.getContainingFilePath(function);
    } else {
        final VirtualFile srcRoot = ModuleRootManager.getInstance(myFixture.getModule()).getSourceRoots()[0];
        destination = FileUtil.join(srcRoot.getPath(), destination);
    }
    assertNotNull(destination);
    final String finalDestination = destination;
    try {
        WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> {
            if (function.getContainingClass() != null) {
                new PyMakeMethodTopLevelProcessor(function, finalDestination).run();
            } else {
                new PyMakeLocalFunctionTopLevelProcessor(function, finalDestination).run();
            }
        });
    } catch (IncorrectOperationException e) {
        if (errorMessage == null) {
            fail("Refactoring failed unexpectedly with message: " + e.getMessage());
        }
        assertEquals(errorMessage, e.getMessage());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyMakeLocalFunctionTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeLocalFunctionTopLevelProcessor) PyFunction(com.jetbrains.python.psi.PyFunction) PyMakeMethodTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 8 with PyFunction

use of com.jetbrains.python.psi.PyFunction 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 9 with PyFunction

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

the class PyFunctionNameMacro method calculateResult.

@Nullable
@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    PsiElement place = context.getPsiElementAtStartOffset();
    PyFunction pyFunction = PsiTreeUtil.getParentOfType(place, PyFunction.class);
    if (pyFunction == null) {
        return null;
    }
    String name = pyFunction.getName();
    return name == null ? null : new TextResult(name);
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PyFunction

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

the class PyTestUtil method isPyTestClass.

public static boolean isPyTestClass(final PyClass pyClass, @Nullable final TypeEvalContext context) {
    final TypeEvalContext contextToUse = (context != null ? context : TypeEvalContext.codeInsightFallback(pyClass.getProject()));
    for (PyClassLikeType type : pyClass.getAncestorTypes(contextToUse)) {
        if (type != null && PYTHON_TEST_QUALIFIED_CLASSES.contains(type.getClassQName())) {
            return true;
        }
    }
    final String className = pyClass.getName();
    if (className == null)
        return false;
    final String name = className.toLowerCase();
    if (name.startsWith("test")) {
        for (PyFunction cls : pyClass.getMethods()) {
            if (isPyTestFunction(cls)) {
                return true;
            }
        }
    }
    return false;
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

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