svn: Refactored "LocalChangesPromptTask", "Intersection" - code simplified

This commit is contained in:
Konstantin Kolosovsky
2016-11-09 23:53:59 +03:00
parent 9f9c1a14de
commit 3b271e2255
4 changed files with 45 additions and 39 deletions
@@ -91,7 +91,7 @@ public abstract class BaseMergeTask extends TaskDescriptor {
: new LookForBranchOriginTask(myMergeProcess, true, copyPoint ->
next(new MergeAllWithBranchCopyPointTask(myMergeProcess, copyPoint, supportsMergeInfo)));
return ar(new LocalChangesPromptTask(myMergeProcess), mergeAllTask);
return ar(new LocalChangesPromptTask(myMergeProcess, null), mergeAllTask);
}
protected void runChangeListsMerge(@NotNull List<SvnChangeList> lists, @NotNull String title) {
@@ -16,36 +16,46 @@
package org.jetbrains.idea.svn.integrate;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.util.containers.MultiMap;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Konstantin Kolosovsky.
*/
import static com.intellij.util.ObjectUtils.notNull;
import static com.intellij.util.containers.ContainerUtil.*;
public class Intersection {
private final Map<String, String> myLists;
private final MultiMap<String, Change> myChangesSubset;
public Intersection() {
myLists = new HashMap<>();
myChangesSubset = new MultiMap<>();
@NotNull private final Map<String, String> myListComments = newHashMap();
@NotNull private final Map<String, List<Change>> myChangesByLists = newHashMap();
public void add(@NotNull LocalChangeList list, @NotNull Change change) {
myChangesByLists.computeIfAbsent(list.getName(), key -> newArrayList()).add(change);
myListComments.put(list.getName(), notNull(list.getComment(), list.getName()));
}
public void add(@NotNull final String listName, @Nullable final String comment, final Change change) {
myChangesSubset.putValue(listName, change);
final String commentToPut = comment == null ? listName : comment;
myLists.put(listName, commentToPut);
@NotNull
public String getComment(@NotNull String listName) {
return myListComments.get(listName);
}
public String getComment(final String listName) {
return myLists.get(listName);
@NotNull
public Map<String, List<Change>> getChangesByLists() {
return myChangesByLists;
}
public MultiMap<String, Change> getChangesSubset() {
return myChangesSubset;
public boolean isEmpty() {
return myChangesByLists.isEmpty();
}
@NotNull
public List<Change> getAllChanges() {
return concat(myChangesByLists.values());
}
public static boolean isEmpty(@Nullable Intersection intersection) {
return intersection == null || intersection.isEmpty();
}
}
@@ -28,7 +28,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.history.SvnChangeList;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@@ -38,6 +37,8 @@ import static com.intellij.openapi.util.text.StringUtil.isEmptyOrSpaces;
import static com.intellij.openapi.vcs.changes.ChangesUtil.*;
import static com.intellij.util.containers.ContainerUtil.sorted;
import static java.util.stream.Collectors.toSet;
import static org.jetbrains.idea.svn.integrate.Intersection.isEmpty;
import static org.jetbrains.idea.svn.integrate.LocalChangesAction.continueMerge;
import static org.tmatesoft.svn.core.internal.util.SVNPathUtil.append;
import static org.tmatesoft.svn.core.internal.util.SVNPathUtil.getRelativePath;
@@ -45,12 +46,7 @@ public class LocalChangesPromptTask extends BaseMergeTask {
@Nullable private final List<SvnChangeList> myChangeListsToMerge;
public LocalChangesPromptTask(@NotNull QuickMerge mergeProcess) {
super(mergeProcess, "local changes intersection check", Where.AWT);
myChangeListsToMerge = null;
}
public LocalChangesPromptTask(@NotNull QuickMerge mergeProcess, @NotNull List<SvnChangeList> changeListsToMerge) {
public LocalChangesPromptTask(@NotNull QuickMerge mergeProcess, @Nullable List<SvnChangeList> changeListsToMerge) {
super(mergeProcess, "local changes intersection check", Where.AWT);
myChangeListsToMerge = changeListsToMerge;
}
@@ -70,14 +66,16 @@ public class LocalChangesPromptTask extends BaseMergeTask {
? getChangesIntersection(localChangeLists, myChangeListsToMerge)
: getAllChangesIntersection(localChangeLists);
if (intersection != null && !intersection.getChangesSubset().isEmpty()) {
processIntersection(intersection);
}
processIntersection(intersection);
}
private void processIntersection(@NotNull Intersection intersection) {
//noinspection EnumSwitchStatementWhichMissesCases
switch (myInteraction.selectLocalChangesAction(myChangeListsToMerge == null)) {
private void processIntersection(@Nullable Intersection intersection) {
boolean mergeAll = myChangeListsToMerge == null;
LocalChangesAction nextAction = !isEmpty(intersection) ? myInteraction.selectLocalChangesAction(mergeAll) : continueMerge;
switch (nextAction) {
case continueMerge:
break;
case shelve:
next(new ShelveLocalChangesTask(myMergeProcess, intersection));
break;
@@ -85,9 +83,9 @@ public class LocalChangesPromptTask extends BaseMergeTask {
end();
break;
case inspect:
// here's cast is due to generic's bug
@SuppressWarnings("unchecked") Collection<Change> changes = (Collection<Change>)intersection.getChangesSubset().values();
myInteraction.showIntersectedLocalPaths(sorted(getPaths(changes), FilePathByPathComparator.getInstance()));
List<FilePath> intersectedPaths = sorted(getPaths(intersection.getAllChanges()), FilePathByPathComparator.getInstance());
myInteraction.showIntersectedLocalPaths(intersectedPaths);
end();
break;
}
@@ -96,7 +94,6 @@ public class LocalChangesPromptTask extends BaseMergeTask {
@Nullable
private Intersection getChangesIntersection(@NotNull List<LocalChangeList> localChangeLists,
@NotNull List<SvnChangeList> changeListsToMerge) {
Set<FilePath> pathsToMerge = collectPaths(changeListsToMerge);
return !changeListsToMerge.isEmpty() ? getChangesIntersection(localChangeLists, change -> hasPathToMerge(change, pathsToMerge)) : null;
@@ -124,7 +121,7 @@ public class LocalChangesPromptTask extends BaseMergeTask {
for (LocalChangeList changeList : changeLists) {
for (Change change : changeList.getChanges()) {
if (filter.value(change)) {
result.add(changeList.getName(), changeList.getComment(), change);
result.add(changeList, change);
}
}
}
@@ -27,7 +27,6 @@ import com.intellij.util.continuation.Where;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -58,7 +57,7 @@ public class ShelveLocalChangesTask extends BaseMergeTask {
List<VirtualFile> changedFiles = newArrayList();
ShelveChangesManager shelveManager = ShelveChangesManager.getInstance(myMergeContext.getProject());
for (Map.Entry<String, Collection<Change>> entry : myIntersection.getChangesSubset().entrySet()) {
for (Map.Entry<String, List<Change>> entry : myIntersection.getChangesByLists().entrySet()) {
try {
// TODO: Could this be done once before for loop?
saveAllDocuments();