From 5f176262f2dae5a0b27e341ef1a86c3983cab55c Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Tue, 19 Apr 2016 16:45:34 +0300 Subject: [PATCH] vcs: Implemented "Delete" action for unversioned files and changes in commit dialog --- .../openapi/vcs/changes/ChangesUtil.java | 40 ++++++++++++------- .../vcs/changes/ui/ChangesBrowserBase.java | 16 +++++++- .../changes/ui/MultipleChangeListBrowser.java | 6 +++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/ChangesUtil.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/ChangesUtil.java index f6bc3daa636d..2b21d3a1730d 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/ChangesUtil.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/ChangesUtil.java @@ -38,6 +38,7 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.*; +import java.util.stream.Collectors; /** * @author max @@ -169,22 +170,31 @@ public class ChangesUtil { return result; } - public static VirtualFile[] getFilesFromChanges(final Collection changes) { - ArrayList files = new ArrayList(); - for (Change change : changes) { - final ContentRevision afterRevision = change.getAfterRevision(); - if (afterRevision != null) { - FilePath filePath = afterRevision.getFile(); - VirtualFile file = filePath.getVirtualFile(); - if (file == null || !file.isValid()) { - file = LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath.getPath()); - } - if (file != null && file.isValid()) { - files.add(file); - } - } + @NotNull + public static VirtualFile[] getFilesFromChanges(@NotNull Collection changes) { + return VfsUtilCore.toVirtualFileArray(getAfterRevisionsFiles(changes)); + } + + @NotNull + public static List getAfterRevisionsFiles(@NotNull Collection changes) { + return changes.stream() + .map(Change::getAfterRevision) + .filter(Objects::nonNull) + .map(ContentRevision::getFile) + .map(ChangesUtil::refreshAndFind) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + @Nullable + private static VirtualFile refreshAndFind(@NotNull FilePath filePath) { + VirtualFile file = filePath.getVirtualFile(); + + if (file == null || !file.isValid()) { + file = LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath.getPath()); } - return VfsUtilCore.toVirtualFileArray(files); + + return file != null && file.isValid() ? file : null; } public static Navigatable[] getNavigatableArray(final Project project, final VirtualFile[] selectedFiles) { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowserBase.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowserBase.java index bfef44a6b947..0c11fefc7a24 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowserBase.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowserBase.java @@ -17,11 +17,13 @@ package com.intellij.openapi.vcs.changes.ui; import com.intellij.diff.DiffDialogHints; import com.intellij.diff.util.DiffUserDataKeysEx; +import com.intellij.ide.DeleteProvider; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.ex.CheckboxAction; import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.fileChooser.actions.VirtualFileDeleteProvider; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.AbstractVcs; @@ -31,6 +33,7 @@ import com.intellij.openapi.vcs.VcsDataKeys; import com.intellij.openapi.vcs.changes.*; import com.intellij.openapi.vcs.changes.actions.diff.ShowDiffAction; import com.intellij.openapi.vcs.changes.actions.diff.ShowDiffContext; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ContainerUtil; @@ -73,6 +76,7 @@ public abstract class ChangesBrowserBase extends JPanel implements TypeSafeDa public static DataKey DATA_KEY = DataKey.create("com.intellij.openapi.vcs.changes.ui.ChangesBrowser"); private AnAction myDiffAction; private final VirtualFile myToSelect; + @NotNull private final DeleteProvider myDeleteProvider = new VirtualFileDeleteProvider(); public void setChangesToDisplay(final List changes) { myChangesToDisplay = changes; @@ -220,6 +224,9 @@ public abstract class ChangesBrowserBase extends JPanel implements TypeSafeDa else if (UNVERSIONED_FILES_DATA_KEY.equals(key)) { sink.put(UNVERSIONED_FILES_DATA_KEY, getVirtualFiles(myViewer.getSelectionPaths(), ChangesBrowserNode.UNVERSIONED_FILES_TAG)); } + else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.equals(key)) { + sink.put(PlatformDataKeys.DELETE_ELEMENT_PROVIDER, myDeleteProvider); + } } public void select(List changes) { @@ -439,9 +446,14 @@ public abstract class ChangesBrowserBase extends JPanel implements TypeSafeDa @NotNull public abstract List getAllChanges(); + @NotNull private VirtualFile[] getSelectedFiles() { - final List changes = getSelectedChanges(); - return ChangesUtil.getFilesFromChanges(changes); + Set result = ContainerUtil.newHashSet(); + + result.addAll(ChangesUtil.getAfterRevisionsFiles(getSelectedChanges())); + result.addAll(getVirtualFiles(myViewer.getSelectionPaths(), null)); + + return VfsUtilCore.toVirtualFileArray(result); } public AnAction getDiffAction() { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/MultipleChangeListBrowser.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/MultipleChangeListBrowser.java index 0c5dc455fb74..f293a6cdf67d 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/MultipleChangeListBrowser.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/MultipleChangeListBrowser.java @@ -87,6 +87,7 @@ public class MultipleChangeListBrowser extends ChangesBrowserBase { private void setupRebuildListForActions() { ActionManager actionManager = ActionManager.getInstance(); final AnAction moveAction = actionManager.getAction(IdeActions.MOVE_TO_ANOTHER_CHANGE_LIST); + final AnAction deleteAction = actionManager.getAction("ChangesView.DeleteUnversioned.From.Dialog"); actionManager.addAnActionListener(new AnActionListener.Adapter() { @Override @@ -94,6 +95,10 @@ public class MultipleChangeListBrowser extends ChangesBrowserBase { if (moveAction.equals(action)) { rebuildList(); } + else if (deleteAction.equals(action)) { + ChangeListManager.getInstance(myProject) + .invokeAfterUpdate(() -> rebuildList(), InvokeAfterUpdateMode.SYNCHRONOUS_CANCELLABLE, "Delete files", null); + } } }, this); } @@ -310,6 +315,7 @@ public class MultipleChangeListBrowser extends ChangesBrowserBase { }); if (Registry.is("vcs.unversioned.files.in.commit")) { toolBarGroup.add(ActionManager.getInstance().getAction("ChangesView.AddUnversioned.From.Dialog")); + toolBarGroup.add(ActionManager.getInstance().getAction("ChangesView.DeleteUnversioned.From.Dialog")); toolBarGroup.add(ActionManager.getInstance().getAction("ChangesView.Ignore")); } RollbackDialogAction rollback = new RollbackDialogAction();