vcs: Moved "SortByVcsRoots" logic to "VcsUtil"

Removed "SortByVcsRoots"
This commit is contained in:
Konstantin Kolosovsky
2016-11-16 21:01:56 +03:00
parent 18817e0493
commit 2e6f5b7ac8
4 changed files with 38 additions and 76 deletions
@@ -1,51 +0,0 @@
/*
* Copyright 2000-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.vcs.changes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsRoot;
import com.intellij.util.containers.Convertor;
import com.intellij.util.containers.MultiMap;
import java.util.Collection;
public class SortByVcsRoots<T> {
private final Project myProject;
private final Convertor<T, FilePath> myConvertor;
private ProjectLevelVcsManager myVcsManager;
public static final VcsRoot ourFictiveValue = new VcsRoot(null, null);
public SortByVcsRoots(Project project, final Convertor<T, FilePath> convertor) {
myProject = project;
myVcsManager = ProjectLevelVcsManager.getInstance(project);
myConvertor = convertor;
}
public MultiMap<VcsRoot, T> sort(final Collection<T> in) {
final MultiMap<VcsRoot, T> result = new MultiMap<>();
for (T t : in) {
final VcsRoot root = myVcsManager.getVcsRootObjectFor(myConvertor.convert(t));
if (root != null) {
result.putValue(root, t);
} else {
result.putValue(ourFictiveValue, t);
}
}
return result;
}
}
@@ -41,6 +41,7 @@ import com.intellij.openapi.vcs.roots.VcsRootDetector;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtilRt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -50,6 +51,9 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
import static com.intellij.util.ObjectUtils.notNull;
import static java.util.stream.Collectors.groupingBy;
@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"})
public class VcsUtil {
protected static final char[] ourCharsToBeChopped = new char[]{'/', '\\'};
@@ -58,6 +62,8 @@ public class VcsUtil {
public final static String MAX_VCS_LOADED_SIZE_KB = "idea.max.vcs.loaded.size.kb";
private static final int ourMaxLoadedFileSize = computeLoadedFileSize();
@NotNull private static final VcsRoot FICTIVE_ROOT = new VcsRoot(null, null);
public static int getMaxVcsLoadedFileSize() {
return ourMaxLoadedFileSize;
}
@@ -624,6 +630,15 @@ public class VcsUtil {
return file.getName() + " (" + file.getParent() + ")";
}
@NotNull
public static <T> Map<VcsRoot, List<T>> groupByRoots(@NotNull Project project,
@NotNull Collection<T> items,
@NotNull Function<T, FilePath> filePathMapper) {
ProjectLevelVcsManager manager = ProjectLevelVcsManager.getInstance(project);
return items.stream().collect(groupingBy(item -> notNull(manager.getVcsRootObjectFor(filePathMapper.fun(item)), FICTIVE_ROOT)));
}
@NotNull
public static Collection<VcsDirectoryMapping> findRoots(@NotNull VirtualFile rootDir, @NotNull Project project)
throws IllegalArgumentException {
@@ -22,19 +22,22 @@ import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.VcsOutgoingChangesProvider;
import com.intellij.openapi.vcs.VcsRoot;
import com.intellij.openapi.vcs.changes.*;
import com.intellij.openapi.vcs.changes.BinaryContentRevision;
import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.BeforeAfter;
import com.intellij.util.containers.Convertor;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.*;
import static com.intellij.openapi.vcs.changes.ChangesUtil.getAfterPath;
import static com.intellij.openapi.vcs.changes.ChangesUtil.getBeforePath;
import static com.intellij.util.ObjectUtils.chooseNotNull;
import static com.intellij.vcsUtil.VcsUtil.groupByRoots;
public class IdeaTextPatchBuilder {
private IdeaTextPatchBuilder() {
@@ -42,14 +45,8 @@ public class IdeaTextPatchBuilder {
public static List<BeforeAfter<AirContentRevision>> revisionsConvertor(final Project project, final List<Change> changes) throws VcsException {
final List<BeforeAfter<AirContentRevision>> result = new ArrayList<>(changes.size());
final Convertor<Change, FilePath> beforePrefferingConvertor = new Convertor<Change, FilePath>() {
public FilePath convert(Change o) {
final FilePath before = ChangesUtil.getBeforePath(o);
return before == null ? ChangesUtil.getAfterPath(o) : before;
}
};
final MultiMap<VcsRoot,Change> byRoots = new SortByVcsRoots<>(project, beforePrefferingConvertor).sort(changes);
Map<VcsRoot, List<Change>> byRoots =
groupByRoots(project, changes, change -> chooseNotNull(getBeforePath(change), getAfterPath(change)));
for (VcsRoot root : byRoots.keySet()) {
final Collection<Change> rootChanges = byRoots.get(root);
@@ -20,7 +20,6 @@ import com.intellij.openapi.project.Project;
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.SortByVcsRoots;
import com.intellij.openapi.vcs.checkin.CheckinEnvironment;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
@@ -29,12 +28,14 @@ import com.intellij.util.FilePathByPathComparator;
import com.intellij.util.ObjectUtils;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Convertor;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import static com.intellij.util.Functions.identity;
import static com.intellij.vcsUtil.VcsUtil.groupByRoots;
public class TriggerAdditionOrDeletion {
private final Collection<FilePath> myExisting;
private final Collection<FilePath> myDeleted;
@@ -71,13 +72,11 @@ public class TriggerAdditionOrDeletion {
public void prepare() {
if (myExisting.isEmpty() && myDeleted.isEmpty()) return;
final SortByVcsRoots<FilePath> sortByVcsRoots = new SortByVcsRoots<>(myProject, new Convertor.IntoSelf<>());
if (! myExisting.isEmpty()) {
processAddition(sortByVcsRoots);
processAddition();
}
if (! myDeleted.isEmpty()) {
processDeletion(sortByVcsRoots);
processDeletion();
}
}
@@ -147,8 +146,9 @@ public class TriggerAdditionOrDeletion {
return myAffected;
}
private void processDeletion(SortByVcsRoots<FilePath> sortByVcsRoots) {
final MultiMap<VcsRoot, FilePath> map = sortByVcsRoots.sort(myDeleted);
private void processDeletion() {
Map<VcsRoot, List<FilePath>> map = groupByRoots(myProject, myDeleted, identity());
myPreparedDeletion = new MultiMap<>();
for (VcsRoot vcsRoot : map.keySet()) {
if (vcsRoot != null && vcsRoot.getVcs() != null) {
@@ -175,8 +175,9 @@ public class TriggerAdditionOrDeletion {
}
}
private void processAddition(SortByVcsRoots<FilePath> sortByVcsRoots) {
final MultiMap<VcsRoot, FilePath> map = sortByVcsRoots.sort(myExisting);
private void processAddition() {
Map<VcsRoot, List<FilePath>> map = groupByRoots(myProject, myExisting, identity());
myPreparedAddition = new MultiMap<>();
for (VcsRoot vcsRoot : map.keySet()) {
if (vcsRoot != null && vcsRoot.getVcs() != null) {