Search in sources :

Example 1 with PyMakeMethodTopLevelProcessor

use of com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor 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 2 with PyMakeMethodTopLevelProcessor

use of com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor in project intellij-community by JetBrains.

the class PyMoveSymbolDelegate method doMove.

public void doMove(@NotNull Project project, @NotNull List<PyElement> elements) {
    final PsiElement firstElement = elements.get(0);
    final String initialPath = StringUtil.notNullize(PyPsiUtils.getContainingFilePath(firstElement));
    final BaseRefactoringProcessor processor;
    if (isMovableLocalFunctionOrMethod(firstElement)) {
        final PyFunction function = (PyFunction) firstElement;
        final PyMakeFunctionTopLevelDialog dialog = new PyMakeFunctionTopLevelDialog(project, function, initialPath, initialPath);
        if (!dialog.showAndGet()) {
            return;
        }
        if (function.getContainingClass() != null) {
            processor = new PyMakeMethodTopLevelProcessor(function, dialog.getTargetPath());
        } else {
            processor = new PyMakeLocalFunctionTopLevelProcessor(function, dialog.getTargetPath());
        }
        processor.setPreviewUsages(dialog.isPreviewUsages());
    } else {
        final List<PsiNamedElement> initialElements = Lists.newArrayList();
        for (PsiElement element : elements) {
            final PsiNamedElement e = PyMoveModuleMembersHelper.extractNamedElement(element);
            if (e == null) {
                return;
            }
            initialElements.add(e);
        }
        final PyMoveModuleMembersDialog dialog = new PyMoveModuleMembersDialog(project, initialElements, initialPath, initialPath);
        if (!dialog.showAndGet()) {
            return;
        }
        final PsiNamedElement[] selectedElements = ContainerUtil.findAllAsArray(dialog.getSelectedTopLevelSymbols(), PsiNamedElement.class);
        processor = new PyMoveModuleMembersProcessor(selectedElements, dialog.getTargetPath());
        processor.setPreviewUsages(dialog.isPreviewUsages());
    }
    try {
        processor.run();
    } catch (IncorrectOperationException e) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw e;
        }
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, project);
    }
}
Also used : PyMoveModuleMembersDialog(com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersDialog) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) PyMoveModuleMembersProcessor(com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersProcessor) PyMakeFunctionTopLevelDialog(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeFunctionTopLevelDialog) PyMakeLocalFunctionTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeLocalFunctionTopLevelProcessor) PyMakeMethodTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 PyMakeLocalFunctionTopLevelProcessor (com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeLocalFunctionTopLevelProcessor)2 PyMakeMethodTopLevelProcessor (com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 BaseRefactoringProcessor (com.intellij.refactoring.BaseRefactoringProcessor)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyMakeFunctionTopLevelDialog (com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeFunctionTopLevelDialog)1 PyMoveModuleMembersDialog (com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersDialog)1 PyMoveModuleMembersProcessor (com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersProcessor)1