vcs: Implemented "Add to VCS" action for unversioned files in commit dialog

This commit is contained in:
Konstantin Kolosovsky
2016-04-20 18:10:19 +03:00
parent df75607e8f
commit 0cd4e0ebc4
5 changed files with 60 additions and 51 deletions
@@ -30,13 +30,14 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManagerImpl;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import com.intellij.openapi.vcs.changes.ui.ChangesBrowserBase;
import com.intellij.openapi.vcs.changes.ui.ChangesListView;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -56,18 +57,34 @@ public class ScheduleForAdditionAction extends AnAction implements DumbAware {
}
public void actionPerformed(@NotNull AnActionEvent e) {
final List<VirtualFile> unversionedFiles = getUnversionedFiles(e);
if (unversionedFiles.isEmpty()) {
return;
addUnversioned(e.getRequiredData(CommonDataKeys.PROJECT), getUnversionedFiles(e), this::isStatusForAddition,
e.getData(ChangesBrowserBase.DATA_KEY));
}
public static boolean addUnversioned(@NotNull Project project,
@NotNull List<VirtualFile> files,
@NotNull Condition<FileStatus> unversionedFileCondition,
@Nullable ChangesBrowserBase browser) {
boolean result = true;
if (!files.isEmpty()) {
FileDocumentManager.getInstance().saveAllDocuments();
@SuppressWarnings("unchecked")
Consumer<List<Change>> consumer = browser == null ? null : changes -> {
browser.rebuildList();
browser.getViewer().excludeChanges((List)files);
browser.getViewer().includeChanges((List)changes);
};
ChangeListManagerImpl manager = ChangeListManagerImpl.getInstanceImpl(project);
LocalChangeList targetChangeList =
browser == null ? manager.getDefaultChangeList() : (LocalChangeList)browser.getSelectedChangeList();
List<VcsException> exceptions = manager.addUnversionedFiles(targetChangeList, files, unversionedFileCondition, consumer);
result = exceptions.isEmpty();
}
FileDocumentManager.getInstance().saveAllDocuments();
final ChangeListManagerImpl changeListManager = ChangeListManagerImpl.getInstanceImpl(e.getData(CommonDataKeys.PROJECT));
changeListManager.addUnversionedFiles(changeListManager.getDefaultChangeList(), unversionedFiles, new Condition<FileStatus>() {
@Override
public boolean value(FileStatus status) {
return isStatusForAddition(status);
}
}, null);
return result;
}
private boolean thereAreUnversionedFiles(AnActionEvent e) {
@@ -47,6 +47,8 @@ import java.io.File;
import java.util.*;
import java.util.List;
import static com.intellij.openapi.vcs.changes.ui.ChangesListView.*;
public abstract class ChangesBrowserBase<T> extends JPanel implements TypeSafeDataProvider, Disposable {
private static final Logger LOG = Logger.getInstance(ChangesBrowserBase.class);
@@ -215,6 +217,9 @@ public abstract class ChangesBrowserBase<T> extends JPanel implements TypeSafeDa
final List<Change> selectedChanges = getSelectedChanges();
sink.put(VcsDataKeys.SELECTED_CHANGES_IN_DETAILS, selectedChanges.toArray(new Change[selectedChanges.size()]));
}
else if (UNVERSIONED_FILES_DATA_KEY.equals(key)) {
sink.put(UNVERSIONED_FILES_DATA_KEY, getVirtualFiles(myViewer.getSelectionPaths(), ChangesBrowserNode.UNVERSIONED_FILES_TAG));
}
}
public void select(List<T> changes) {
@@ -462,6 +467,6 @@ public abstract class ChangesBrowserBase<T> extends JPanel implements TypeSafeDa
}
static boolean isUnderUnversioned(@NotNull ChangesBrowserNode node) {
return ChangesListView.isUnderTag(new TreePath(node.getPath()), ChangesBrowserNode.UNVERSIONED_FILES_TAG);
return isUnderTag(new TreePath(node.getPath()), ChangesBrowserNode.UNVERSIONED_FILES_TAG);
}
}
@@ -56,6 +56,8 @@ import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author max
@@ -254,17 +256,18 @@ public class ChangesListView extends Tree implements TypeSafeDataProvider, Advan
@NotNull
private List<VirtualFile> getSelectedVirtualFiles(@Nullable Object tag) {
Set<VirtualFile> files = new HashSet<VirtualFile>();
final TreePath[] paths = getSelectionPaths();
if (paths != null) {
for (TreePath path : paths) {
if (isUnderTag(path, tag)) {
ChangesBrowserNode<?> node = (ChangesBrowserNode)path.getLastPathComponent();
files.addAll(node.getAllFilesUnder());
}
}
}
return ContainerUtil.newArrayList(files);
return getVirtualFiles(getSelectionPaths(), tag);
}
@NotNull
static List<VirtualFile> getVirtualFiles(@Nullable TreePath[] paths, @Nullable Object tag) {
return paths == null
? Collections.emptyList()
: Stream.of(paths)
.filter(path -> isUnderTag(path, tag))
.flatMap(path -> ((ChangesBrowserNode<?>)path.getLastPathComponent()).getAllFilesUnder().stream())
.distinct()
.collect(Collectors.toList());
}
static boolean isUnderTag(@NotNull TreePath path, @Nullable Object tag) {
@@ -37,6 +37,7 @@ import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.*;
import com.intellij.openapi.vcs.changes.actions.ScheduleForAdditionAction;
import com.intellij.openapi.vcs.checkin.*;
import com.intellij.openapi.vcs.impl.CheckinHandlersManager;
import com.intellij.openapi.vcs.ui.CommitMessage;
@@ -50,7 +51,6 @@ import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.SplitterWithSecondHideable;
import com.intellij.ui.components.labels.ActionLink;
import com.intellij.util.Alarm;
import com.intellij.util.Consumer;
import com.intellij.util.ObjectUtils;
import com.intellij.util.OnOffListener;
import com.intellij.util.containers.ContainerUtil;
@@ -612,29 +612,9 @@ public class CommitChangeListDialog extends DialogWrapper implements CheckinProj
}
private boolean addUnversionedFiles() {
boolean isSuccessful = true;
final List<VirtualFile> files = myBrowser.getIncludedUnversionedFiles();
if (!files.isEmpty()) {
FileDocumentManager.getInstance().saveAllDocuments();
ChangeListManagerImpl manager = ChangeListManagerImpl.getInstanceImpl(myProject);
LocalChangeList targetChangeList = (LocalChangeList)myBrowser.getSelectedChangeList();
List<VcsException> exceptions =
manager.addUnversionedFiles(targetChangeList, files, ChangeListManagerImpl.getDefaultUnversionedFileCondition(),
new Consumer<List<Change>>() {
@SuppressWarnings("unchecked")
@Override
public void consume(@NotNull List<Change> changes) {
myBrowser.rebuildList();
myBrowser.getViewer().excludeChanges((List)files);
myBrowser.getViewer().includeChanges((List)changes);
}
});
isSuccessful = exceptions.isEmpty();
}
return isSuccessful;
return ScheduleForAdditionAction
.addUnversioned(myProject, myBrowser.getIncludedUnversionedFiles(), ChangeListManagerImpl.getDefaultUnversionedFileCondition(),
myBrowser);
}
@NotNull
@@ -23,6 +23,7 @@ import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.VcsBundle;
@@ -307,6 +308,9 @@ public class MultipleChangeListBrowser extends ChangesBrowserBase<Object> {
rebuildList();
}
});
if (Registry.is("vcs.unversioned.files.in.commit")) {
toolBarGroup.add(ActionManager.getInstance().getAction("ChangesView.AddUnversioned.From.Dialog"));
}
RollbackDialogAction rollback = new RollbackDialogAction();
EmptyAction.setupAction(rollback, IdeActions.CHANGES_VIEW_ROLLBACK, this);
toolBarGroup.add(rollback);