Convert Java file to Kotlin: do not ask whether to backup files
Do not backup files #KT-5530 Fixed
This commit is contained in:
@@ -22,7 +22,6 @@ import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -33,13 +32,11 @@ import org.jetbrains.jet.plugin.j2k.J2kPostProcessor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.openapi.ui.Messages.NO;
|
||||
import static com.intellij.openapi.ui.Messages.YES;
|
||||
import static org.jetbrains.jet.plugin.actions.JavaToKotlinActionUtil.*;
|
||||
|
||||
public class JavaToKotlinAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
VirtualFile[] virtualFiles = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
|
||||
assert virtualFiles != null;
|
||||
final Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
|
||||
@@ -48,10 +45,6 @@ public class JavaToKotlinAction extends AnAction {
|
||||
if (selectedJavaFiles.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
final DialogResult userResponse = showDialog(project);
|
||||
if (userResponse == DialogResult.CANCEL) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Converter converter = Converter.OBJECT$.create(project,
|
||||
ConverterSettings.defaultSettings,
|
||||
@@ -64,12 +57,7 @@ public class JavaToKotlinAction extends AnAction {
|
||||
@Override
|
||||
public void run() {
|
||||
List<VirtualFile> newFiles = convertFiles(converter, selectedJavaFiles);
|
||||
if (userResponse == DialogResult.DELETE_FILES) {
|
||||
deleteFiles(selectedJavaFiles);
|
||||
}
|
||||
else if (userResponse == DialogResult.BACKUP_FILES) {
|
||||
renameFiles(selectedJavaFiles);
|
||||
}
|
||||
deleteFiles(selectedJavaFiles);
|
||||
reformatFiles(newFiles, project);
|
||||
for (VirtualFile vf : newFiles) {
|
||||
FileEditorManager.getInstance(project).openFile(vf, true);
|
||||
@@ -81,30 +69,8 @@ public class JavaToKotlinAction extends AnAction {
|
||||
);
|
||||
}
|
||||
|
||||
private static enum DialogResult {
|
||||
BACKUP_FILES,
|
||||
DELETE_FILES,
|
||||
CANCEL
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static DialogResult showDialog(@NotNull Project project) {
|
||||
int result = Messages.showYesNoCancelDialog(project,
|
||||
"Would you like to backup Java files?",
|
||||
"Convert Java to Kotlin",
|
||||
Messages.getQuestionIcon());
|
||||
switch (result) {
|
||||
case YES:
|
||||
return DialogResult.BACKUP_FILES;
|
||||
case NO:
|
||||
return DialogResult.DELETE_FILES;
|
||||
default:
|
||||
return DialogResult.CANCEL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
public void update(@NotNull AnActionEvent e) {
|
||||
boolean enabled = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) != null;
|
||||
e.getPresentation().setVisible(enabled);
|
||||
e.getPresentation().setEnabled(enabled);
|
||||
|
||||
@@ -137,25 +137,4 @@ public class JavaToKotlinActionUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static void renameFiles(@NotNull List<PsiJavaFile> psiFiles) {
|
||||
for (final PsiFile psiFile : psiFiles) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
PsiManager manager = psiFile.getManager();
|
||||
VirtualFile vFile = psiFile.getVirtualFile();
|
||||
if (vFile != null) {
|
||||
vFile.copy(manager, vFile.getParent(), vFile.getNameWithoutExtension() + ".java.old");
|
||||
vFile.delete(manager);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
MessagesEx.error(psiFile.getProject(), e.getMessage()).showLater();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user