mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
split write action so dialogs won't be shown from inside
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.actions.OptimizeImportsProcessor;
|
||||
import com.intellij.featureStatistics.FeatureUsageTracker;
|
||||
import com.intellij.ide.util.EditorHelper;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -267,32 +268,26 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
final boolean openInEditor) {
|
||||
final boolean[] result = new boolean[] {false};
|
||||
Runnable command = () -> {
|
||||
final Runnable action = () -> {
|
||||
try {
|
||||
PsiDirectory target;
|
||||
if (targetDirectory instanceof PsiDirectory) {
|
||||
target = (PsiDirectory)targetDirectory;
|
||||
} else {
|
||||
target = ((MoveDestination)targetDirectory).getTargetDirectory(defaultTargetDirectory);
|
||||
}
|
||||
Collection<PsiFile> files = doCopyClasses(classes, map, copyClassName, target, project);
|
||||
if (files != null) {
|
||||
if (openInEditor) {
|
||||
for (PsiFile file : files) {
|
||||
CopyHandler.updateSelectionInActiveProjectView(file, project, selectInActivePanel);
|
||||
}
|
||||
EditorHelper.openFilesInEditor(files.toArray(new PsiFile[files.size()]));
|
||||
PsiDirectory target;
|
||||
if (targetDirectory instanceof PsiDirectory) {
|
||||
target = (PsiDirectory)targetDirectory;
|
||||
} else {
|
||||
target = WriteAction.compute(() -> (MoveDestination)targetDirectory).getTargetDirectory(defaultTargetDirectory);
|
||||
}
|
||||
try {
|
||||
Collection<PsiFile> files = doCopyClasses(classes, map, copyClassName, target, project);
|
||||
if (files != null) {
|
||||
if (openInEditor) {
|
||||
for (PsiFile file : files) {
|
||||
CopyHandler.updateSelectionInActiveProjectView(file, project, selectInActivePanel);
|
||||
}
|
||||
|
||||
result[0] = true;
|
||||
EditorHelper.openFilesInEditor(files.toArray(new PsiFile[files.size()]));
|
||||
}
|
||||
}
|
||||
catch (final IncorrectOperationException ex) {
|
||||
ApplicationManager.getApplication().invokeLater(
|
||||
() -> Messages.showMessageDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
|
||||
}
|
||||
};
|
||||
ApplicationManager.getApplication().runWriteAction(action);
|
||||
}
|
||||
catch (IncorrectOperationException ex) {
|
||||
Messages.showMessageDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon());
|
||||
}
|
||||
};
|
||||
CommandProcessor processor = CommandProcessor.getInstance();
|
||||
processor.executeCommand(project, command, commandName, null);
|
||||
@@ -349,11 +344,11 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
PsiClass source = findByName(sources, destination.getName());
|
||||
if (source != null) {
|
||||
final PsiClass copy = copy(source, copyClassName);
|
||||
newElement = destination.replace(copy);
|
||||
newElement = WriteAction.compute(() -> destination.replace(copy));
|
||||
oldToNewMap.put(source, newElement);
|
||||
}
|
||||
else {
|
||||
destination.delete();
|
||||
WriteAction.run(() -> destination.delete());
|
||||
}
|
||||
}
|
||||
createdFiles.add(createdFile);
|
||||
@@ -368,7 +363,7 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
PsiDirectory finalTarget = targetDirectory;
|
||||
final String relativePath = map != null ? map.get(file) : null;
|
||||
if (relativePath != null && !relativePath.isEmpty()) {
|
||||
finalTarget = buildRelativeDir(targetDirectory, relativePath).findOrCreateTargetDirectory();
|
||||
finalTarget = WriteAction.compute(() -> buildRelativeDir(targetDirectory, relativePath).findOrCreateTargetDirectory());
|
||||
}
|
||||
final PsiFile fileCopy = CopyFilesOrDirectoriesHandler.copyToDirectory(file, getNewFileName(file, copyClassName), finalTarget, choice);
|
||||
if (fileCopy != null) {
|
||||
@@ -380,28 +375,31 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
}
|
||||
}
|
||||
|
||||
final Set<PsiElement> rebindExpressions = new HashSet<>();
|
||||
for (PsiElement element : oldToNewMap.values()) {
|
||||
if (element == null) {
|
||||
LOG.error(oldToNewMap.keySet());
|
||||
continue;
|
||||
WriteAction.run(() -> {
|
||||
final Set<PsiElement> rebindExpressions = new HashSet<>();
|
||||
for (PsiElement element : oldToNewMap.values()) {
|
||||
if (element == null) {
|
||||
LOG.error(oldToNewMap.keySet());
|
||||
continue;
|
||||
}
|
||||
decodeRefs(element, oldToNewMap, rebindExpressions);
|
||||
}
|
||||
decodeRefs(element, oldToNewMap, rebindExpressions);
|
||||
}
|
||||
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
for (PsiFile psiFile : createdFiles) {
|
||||
if (psiFile instanceof PsiJavaFile) {
|
||||
codeStyleManager.removeRedundantImports((PsiJavaFile)psiFile);
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
for (PsiFile psiFile : createdFiles) {
|
||||
if (psiFile instanceof PsiJavaFile) {
|
||||
codeStyleManager.removeRedundantImports((PsiJavaFile)psiFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (PsiElement expression : rebindExpressions) {
|
||||
//filter out invalid elements which are produced by nested elements:
|
||||
//new expressions/type elements, like: List<List<String>>; new Foo(new Foo()), etc
|
||||
if (expression.isValid()){
|
||||
codeStyleManager.shortenClassReferences(expression);
|
||||
for (PsiElement expression : rebindExpressions) {
|
||||
//filter out invalid elements which are produced by nested elements:
|
||||
//new expressions/type elements, like: List<List<String>>; new Foo(new Foo()), etc
|
||||
if (expression.isValid()) {
|
||||
codeStyleManager.shortenClassReferences(expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
new OptimizeImportsProcessor(project, createdFiles.toArray(new PsiFile[createdFiles.size()]), null).run();
|
||||
return createdFiles;
|
||||
}
|
||||
@@ -413,10 +411,10 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
private static PsiFile copy(@NotNull PsiFile file, PsiDirectory directory, String name, String relativePath, int[] choice) {
|
||||
final String fileName = getNewFileName(file, name);
|
||||
if (relativePath != null && !relativePath.isEmpty()) {
|
||||
return buildRelativeDir(directory, relativePath).findOrCreateTargetDirectory().copyFileFrom(fileName, file);
|
||||
return WriteAction.compute(() -> buildRelativeDir(directory, relativePath).findOrCreateTargetDirectory().copyFileFrom(fileName, file));
|
||||
}
|
||||
if (CopyFilesOrDirectoriesHandler.checkFileExist(directory, choice, file, fileName, "Copy")) return null;
|
||||
return directory.copyFileFrom(fileName, file);
|
||||
return WriteAction.compute(() -> directory.copyFileFrom(fileName, file));
|
||||
}
|
||||
|
||||
private static String getNewFileName(PsiFile file, String name) {
|
||||
|
||||
Reference in New Issue
Block a user