mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-4387 Extract PyBaseConvertRefactoringAction for shared functionality
Also I prohibited calling these "Convert ..." refactorings from editor.
This commit is contained in:
@@ -558,7 +558,8 @@ INSP.NAME.assignment.to.loop.or.with.parameter.display.message=Variable ''{0}''
|
||||
|
||||
# Refactoring
|
||||
refactoring.will.not.be.accessible=Member, you are trying to move depends on ''{0}'' which will not be accessible after this refactoring
|
||||
|
||||
refactoring.error.file.exists=File "{0}" already exists
|
||||
refactoring.error.directory.exists=Directory "{0}" already exists
|
||||
|
||||
# introduce
|
||||
refactoring.introduce.name.error=Incorrect name
|
||||
@@ -641,8 +642,6 @@ refactoring.change.signature.error.wrong.caret.position.method.name=The caret sh
|
||||
|
||||
# convert module to package
|
||||
refactoring.convert.module.to.package.title=Convert Module to Package
|
||||
refactoring.convert.module.to.package.error.file.exists=File "{0}" already exists
|
||||
refactoring.convert.module.to.package.error.directory.exists=Directory "{0}" already exists
|
||||
|
||||
# convert module
|
||||
refactoring.convert.package.to.module.title=Convert Module to Package
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.jetbrains.python.refactoring.convert;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.actions.BaseRefactoringAction;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PythonLanguage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public abstract class PyBaseConvertRefactoringAction extends BaseRefactoringAction {
|
||||
@Override
|
||||
protected final boolean isAvailableInEditorOnly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element,
|
||||
@NotNull Editor editor,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull DataContext context) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isAvailableForLanguage(Language language) {
|
||||
return language.isKindOf(PythonLanguage.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableForFile(PsiFile file) {
|
||||
return isAvailableForLanguage(file.getLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Show standard error dialog containing message about unexpected presense of given file or directory.
|
||||
*
|
||||
* @param file file or directory to warn about
|
||||
* @param id ID of refactoring as {@link CommonRefactoringUtil#showErrorMessage} requires
|
||||
* @param project active project
|
||||
*/
|
||||
protected final void showFileExistsErrorMessage(@NotNull VirtualFile file, @NotNull String id, @NotNull Project project) {
|
||||
final String message;
|
||||
if (file.isDirectory()) {
|
||||
message = PyBundle.message("refactoring.error.directory.exists", file.getName());
|
||||
}
|
||||
else {
|
||||
message = PyBundle.message("refactoring.error.file.exists", file.getName());
|
||||
}
|
||||
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, id, project);
|
||||
}
|
||||
}
|
||||
+3
-30
@@ -1,7 +1,6 @@
|
||||
package com.jetbrains.python.refactoring.convert;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -11,12 +10,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.actions.BaseRefactoringAction;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.PythonLanguage;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.PyUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -27,15 +21,10 @@ import java.io.IOException;
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public class PyConvertModuleToPackageAction extends BaseRefactoringAction {
|
||||
public class PyConvertModuleToPackageAction extends PyBaseConvertRefactoringAction {
|
||||
public static final String ID = "py.refactoring.convert.module.to.package";
|
||||
private static final Logger LOG = Logger.getInstance(PyConvertModuleToPackageAction.class);
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableInEditorOnly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEnabledOnElements(@NotNull PsiElement[] elements) {
|
||||
if (elements.length == 1) {
|
||||
@@ -44,16 +33,6 @@ public class PyConvertModuleToPackageAction extends BaseRefactoringAction {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableForLanguage(Language language) {
|
||||
return language.isKindOf(PythonLanguage.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableForFile(PsiFile file) {
|
||||
return isAvailableForLanguage(file.getLanguage());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected RefactoringActionHandler getHandler(@NotNull DataContext dataContext) {
|
||||
@@ -77,14 +56,8 @@ public class PyConvertModuleToPackageAction extends BaseRefactoringAction {
|
||||
final String newPackageName = vFile.getNameWithoutExtension();
|
||||
final VirtualFile existing = parentDir.findChild(newPackageName);
|
||||
if (existing != null) {
|
||||
final String message;
|
||||
if (existing.isDirectory()) {
|
||||
message = PyBundle.message("refactoring.convert.module.to.package.error.directory.exists", newPackageName);
|
||||
}
|
||||
else {
|
||||
message = PyBundle.message("refactoring.convert.module.to.package.error.file.exists", newPackageName);
|
||||
}
|
||||
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, ID, file.getProject());
|
||||
showFileExistsErrorMessage(existing, ID, file.getProject());
|
||||
return;
|
||||
}
|
||||
WriteCommandAction.runWriteCommandAction(file.getProject(), new Runnable() {
|
||||
public void run() {
|
||||
|
||||
+2
-27
@@ -1,7 +1,6 @@
|
||||
package com.jetbrains.python.refactoring.convert;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -15,11 +14,9 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.actions.BaseRefactoringAction;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.PythonLanguage;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.PyUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -32,15 +29,10 @@ import static com.jetbrains.python.psi.PyUtil.as;
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public class PyConvertPackageToModuleAction extends BaseRefactoringAction {
|
||||
public class PyConvertPackageToModuleAction extends PyBaseConvertRefactoringAction {
|
||||
private static final Logger LOG = Logger.getInstance(PyConvertPackageToModuleAction.class);
|
||||
private static final String ID = "py.refactoring.convert.package.to.module";
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableInEditorOnly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEnabledOnElements(@NotNull PsiElement[] elements) {
|
||||
if (elements.length == 1) {
|
||||
@@ -67,16 +59,6 @@ public class PyConvertPackageToModuleAction extends BaseRefactoringAction {
|
||||
return module == null || (PyUtil.getSourceRoots(module).contains(element.getVirtualFile()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableForLanguage(Language language) {
|
||||
return language.isKindOf(PythonLanguage.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAvailableForFile(PsiFile file) {
|
||||
return isAvailableForLanguage(file.getLanguage());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected RefactoringActionHandler getHandler(@NotNull DataContext dataContext) {
|
||||
@@ -118,14 +100,7 @@ public class PyConvertPackageToModuleAction extends BaseRefactoringAction {
|
||||
final String moduleName = packageName + PyNames.DOT_PY;
|
||||
final VirtualFile existing = parentDirVFile.findChild(moduleName);
|
||||
if (existing != null) {
|
||||
final String message;
|
||||
if (existing.isDirectory()) {
|
||||
message = PyBundle.message("refactoring.convert.module.to.package.error.directory.exists", moduleName);
|
||||
}
|
||||
else {
|
||||
message = PyBundle.message("refactoring.convert.module.to.package.error.file.exists", moduleName);
|
||||
}
|
||||
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, ID, pyPackage.getProject());
|
||||
showFileExistsErrorMessage(existing, ID, pyPackage.getProject());
|
||||
return;
|
||||
}
|
||||
final PsiFile initPy = pyPackage.findFile(PyNames.INIT_DOT_PY);
|
||||
|
||||
Reference in New Issue
Block a user