mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
batch copy: cleanup api
IDEA-CR-57252 GitOrigin-RevId: 80b3073ec4f9d577a72436f1a716ac211972272f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
878c81e6f1
commit
fb9b6b98ea
@@ -348,8 +348,7 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
final List<PsiFile> createdFiles = new ArrayList<>(fileToClasses.size());
|
||||
int[] choice = fileToClasses.size() > 1 ? new int[]{-1} : null;
|
||||
List<PsiFile> files = new ArrayList<>();
|
||||
try {
|
||||
targetDirectory.putUserData(PsiDirectoryImpl.UPDATE_ADDED_FILE_KEY, false);
|
||||
((PsiDirectoryImpl)targetDirectory).executeWithUpdatingAddedFilesDisabled(() -> {
|
||||
for (final Map.Entry<PsiFile, PsiClass[]> entry : fileToClasses.entrySet()) {
|
||||
final PsiFile psiFile = entry.getKey();
|
||||
final PsiClass[] sources = entry.getValue();
|
||||
@@ -362,7 +361,7 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Map<PsiClass, PsiClass> sourceToDestination = new LinkedHashMap<>();
|
||||
for (final PsiClass destination : ((PsiClassOwner)createdFile).getClasses()) {
|
||||
if (!isSynthetic(destination)) {
|
||||
@@ -375,7 +374,7 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (final Map.Entry<PsiClass, PsiClass> classEntry : sourceToDestination.entrySet()) {
|
||||
final PsiClass copy = copy(classEntry.getKey(), sourceToDestination.size() > 1 ? null : copyClassName);
|
||||
PsiElement newElement = WriteAction.compute(() -> classEntry.getValue().replace(copy));
|
||||
@@ -387,13 +386,10 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
|
||||
files.add(psiFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
targetDirectory.putUserData(PsiDirectoryImpl.UPDATE_ADDED_FILE_KEY, null);
|
||||
}
|
||||
});
|
||||
|
||||
DumbService.getInstance(project).completeJustSubmittedTasks();
|
||||
WriteAction.run(() -> UpdateAddedFileProcessor.updateAddedFiles(createdFiles.toArray(PsiFile.EMPTY_ARRAY)));
|
||||
WriteAction.run(() -> UpdateAddedFileProcessor.updateAddedFiles(createdFiles));
|
||||
|
||||
for (PsiFile file : files) {
|
||||
try {
|
||||
|
||||
@@ -30,16 +30,18 @@ import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.util.ArrayUtilRt;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.ThrowableRunnable;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class PsiDirectoryImpl extends PsiElementBase implements PsiDirectory, Queryable {
|
||||
public static final Key<Boolean> UPDATE_ADDED_FILE_KEY = Key.create("UPDATE_ADDED_FILE_KEY");
|
||||
private static final Key<Boolean> UPDATE_ADDED_FILE_KEY = Key.create("UPDATE_ADDED_FILE_KEY");
|
||||
private static final Logger LOG = Logger.getInstance(PsiDirectoryImpl.class);
|
||||
|
||||
private final PsiManagerImpl myManager;
|
||||
@@ -353,20 +355,36 @@ public class PsiDirectoryImpl extends PsiElementBase implements PsiDirectory, Qu
|
||||
else {
|
||||
copyVFile = VfsUtilCore.copyFile(this, vFile, parent, newName);
|
||||
}
|
||||
|
||||
final PsiFile copyPsi = myManager.findFile(copyVFile);
|
||||
if (copyPsi == null) throw new IncorrectOperationException("Could not find file " + copyVFile + " after copying " + vFile);
|
||||
if (UPDATE_ADDED_FILE_KEY.get(this, true)) {
|
||||
DumbService.getInstance(getProject()).completeJustSubmittedTasks();
|
||||
UpdateAddedFileProcessor.updateAddedFiles(copyPsi);
|
||||
final PsiFile copyPsi = findCopy(copyVFile, vFile);
|
||||
UpdateAddedFileProcessor.updateAddedFiles(Collections.singletonList(copyPsi));
|
||||
return copyPsi;
|
||||
}
|
||||
return copyPsi;
|
||||
return findCopy(copyVFile, vFile);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IncorrectOperationException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PsiFile findCopy(VirtualFile copyVFile, VirtualFile vFile) {
|
||||
final PsiFile copyPsi = myManager.findFile(copyVFile);
|
||||
if (copyPsi == null) throw new IncorrectOperationException("Could not find file " + copyVFile + " after copying " + vFile);
|
||||
return copyPsi;
|
||||
}
|
||||
|
||||
public <T extends Throwable> void executeWithUpdatingAddedFilesDisabled(ThrowableRunnable<T> runnable) throws T {
|
||||
try {
|
||||
putUserData(UPDATE_ADDED_FILE_KEY, false);
|
||||
runnable.run();
|
||||
}
|
||||
finally {
|
||||
putUserData(UPDATE_ADDED_FILE_KEY,null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkCreateFile(@NotNull String name) throws IncorrectOperationException {
|
||||
VirtualFile existingFile = getVirtualFile().findChild(name);
|
||||
@@ -419,7 +437,7 @@ public class PsiDirectoryImpl extends PsiElementBase implements PsiDirectory, Qu
|
||||
|
||||
PsiFile newFile = myManager.findFile(newVFile);
|
||||
if (newFile == null) throw new IncorrectOperationException("Could not find file " + newVFile);
|
||||
UpdateAddedFileProcessor.updateAddedFiles(newFile);
|
||||
UpdateAddedFileProcessor.updateAddedFiles(Collections.singletonList(newFile));
|
||||
return newFile;
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
@@ -31,7 +31,7 @@ public abstract class UpdateAddedFileProcessor {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void updateAddedFiles(@NotNull PsiFile... copyPsis) throws IncorrectOperationException {
|
||||
public static void updateAddedFiles(@NotNull Iterable<PsiFile> copyPsis) throws IncorrectOperationException {
|
||||
for (PsiFile copyPsi : copyPsis) {
|
||||
final UpdateAddedFileProcessor processor = forElement(copyPsi);
|
||||
if (processor != null) {
|
||||
|
||||
+5
-8
@@ -252,9 +252,9 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
|
||||
int[] choice) throws IOException {
|
||||
Project project = targetDirectory.getProject();
|
||||
List<PsiFile> added = new ArrayList<>();
|
||||
try {
|
||||
targetDirectory.putUserData(PsiDirectoryImpl.UPDATE_ADDED_FILE_KEY, false);
|
||||
PsiManager manager = PsiManager.getInstance(project);
|
||||
PsiManager manager = PsiManager.getInstance(project);
|
||||
((PsiDirectoryImpl)targetDirectory).executeWithUpdatingAddedFilesDisabled(() ->
|
||||
{
|
||||
for (VirtualFile file : files) {
|
||||
PsiFileSystemItem item = file.isDirectory() ? manager.findDirectory(file) : manager.findFile(file);
|
||||
if (item == null) {
|
||||
@@ -263,13 +263,10 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
|
||||
}
|
||||
ContainerUtil.addIfNotNull(added, copyToDirectory(item, newName, targetDirectory, choice, title));
|
||||
}
|
||||
}
|
||||
finally {
|
||||
targetDirectory.putUserData(PsiDirectoryImpl.UPDATE_ADDED_FILE_KEY, null);
|
||||
}
|
||||
});
|
||||
|
||||
DumbService.getInstance(project).completeJustSubmittedTasks();
|
||||
WriteAction.run(() -> UpdateAddedFileProcessor.updateAddedFiles(added.toArray(PsiFile.EMPTY_ARRAY)));
|
||||
WriteAction.run(() -> UpdateAddedFileProcessor.updateAddedFiles(added));
|
||||
return added;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user