From 2e6f5b7ac8a55afd795cc78efbda90040c42e006 Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Sun, 6 Nov 2016 23:36:18 +0300 Subject: [PATCH] vcs: Moved "SortByVcsRoots" logic to "VcsUtil" Removed "SortByVcsRoots" --- .../openapi/vcs/changes/SortByVcsRoots.java | 51 ------------------- .../src/com/intellij/vcsUtil/VcsUtil.java | 15 ++++++ .../diff/impl/patch/IdeaTextPatchBuilder.java | 27 +++++----- .../formove/TriggerAdditionOrDeletion.java | 21 ++++---- 4 files changed, 38 insertions(+), 76 deletions(-) delete mode 100644 platform/vcs-api/src/com/intellij/openapi/vcs/changes/SortByVcsRoots.java diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/SortByVcsRoots.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/SortByVcsRoots.java deleted file mode 100644 index 83c30ce71bce..000000000000 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/SortByVcsRoots.java +++ /dev/null @@ -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 { - private final Project myProject; - private final Convertor myConvertor; - private ProjectLevelVcsManager myVcsManager; - public static final VcsRoot ourFictiveValue = new VcsRoot(null, null); - - public SortByVcsRoots(Project project, final Convertor convertor) { - myProject = project; - myVcsManager = ProjectLevelVcsManager.getInstance(project); - myConvertor = convertor; - } - - public MultiMap sort(final Collection in) { - final MultiMap 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; - } -} diff --git a/platform/vcs-api/src/com/intellij/vcsUtil/VcsUtil.java b/platform/vcs-api/src/com/intellij/vcsUtil/VcsUtil.java index f606bf50eeca..07c6a07663dd 100644 --- a/platform/vcs-api/src/com/intellij/vcsUtil/VcsUtil.java +++ b/platform/vcs-api/src/com/intellij/vcsUtil/VcsUtil.java @@ -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 Map> groupByRoots(@NotNull Project project, + @NotNull Collection items, + @NotNull Function filePathMapper) { + ProjectLevelVcsManager manager = ProjectLevelVcsManager.getInstance(project); + + return items.stream().collect(groupingBy(item -> notNull(manager.getVcsRootObjectFor(filePathMapper.fun(item)), FICTIVE_ROOT))); + } + @NotNull public static Collection findRoots(@NotNull VirtualFile rootDir, @NotNull Project project) throws IllegalArgumentException { diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java index 066b3c3e1fb9..51ba7caf94a5 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java @@ -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> revisionsConvertor(final Project project, final List changes) throws VcsException { final List> result = new ArrayList<>(changes.size()); - - final Convertor beforePrefferingConvertor = new Convertor() { - public FilePath convert(Change o) { - final FilePath before = ChangesUtil.getBeforePath(o); - return before == null ? ChangesUtil.getAfterPath(o) : before; - } - }; - final MultiMap byRoots = new SortByVcsRoots<>(project, beforePrefferingConvertor).sort(changes); + Map> byRoots = + groupByRoots(project, changes, change -> chooseNotNull(getBeforePath(change), getAfterPath(change))); for (VcsRoot root : byRoots.keySet()) { final Collection rootChanges = byRoots.get(root); diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/TriggerAdditionOrDeletion.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/TriggerAdditionOrDeletion.java index 4a56ee36af1e..ce275bf47ddf 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/TriggerAdditionOrDeletion.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/TriggerAdditionOrDeletion.java @@ -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 myExisting; private final Collection myDeleted; @@ -71,13 +72,11 @@ public class TriggerAdditionOrDeletion { public void prepare() { if (myExisting.isEmpty() && myDeleted.isEmpty()) return; - final SortByVcsRoots 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 sortByVcsRoots) { - final MultiMap map = sortByVcsRoots.sort(myDeleted); + private void processDeletion() { + Map> 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 sortByVcsRoots) { - final MultiMap map = sortByVcsRoots.sort(myExisting); + private void processAddition() { + Map> map = groupByRoots(myProject, myExisting, identity()); + myPreparedAddition = new MultiMap<>(); for (VcsRoot vcsRoot : map.keySet()) { if (vcsRoot != null && vcsRoot.getVcs() != null) {