use of com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersProcessor 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);
}
}
use of com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersProcessor in project intellij-community by JetBrains.
the class PyMoveTest method doMoveSymbolsTest.
private void doMoveSymbolsTest(@NotNull String toFileName, String... symbolNames) {
String root = "/refactoring/move/" + getTestName(true);
String rootBefore = root + "/before/src";
String rootAfter = root + "/after/src";
VirtualFile dir1 = myFixture.copyDirectoryToProject(rootBefore, "");
PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
final PsiNamedElement[] symbols = ContainerUtil.map2Array(symbolNames, PsiNamedElement.class, name -> {
final PsiNamedElement found = findFirstNamedElement(name);
assertNotNull("Symbol '" + name + "' does not exist", found);
return found;
});
VirtualFile toVirtualFile = dir1.findFileByRelativePath(toFileName);
String path = toVirtualFile != null ? toVirtualFile.getPath() : (dir1.getPath() + "/" + toFileName);
new PyMoveModuleMembersProcessor(symbols, path).run();
VirtualFile dir2 = getVirtualFileByName(PythonTestUtil.getTestDataPath() + rootAfter);
try {
PlatformTestUtil.assertDirectoriesEqual(dir2, dir1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations