vcs: Refactored ChangeListManagerImpl.addUnversionedFiles() - code simplified, method...
authorKonstantin Kolosovsky <konstantin.kolosovsky@jetbrains.com>
Wed, 17 Dec 2014 10:16:26 +0000 (13:16 +0300)
committerKonstantin Kolosovsky <konstantin.kolosovsky@jetbrains.com>
Wed, 17 Dec 2014 10:16:26 +0000 (13:16 +0300)
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListManagerImpl.java

index 57d436ff435c77c6ff6d82edce6e0b1c5b5192f8..29f999d5d7323c31fb835aa4d1a8900089235629 100644 (file)
@@ -51,6 +51,7 @@ import com.intellij.openapi.vfs.*;
 import com.intellij.ui.EditorNotifications;
 import com.intellij.util.*;
 import com.intellij.util.concurrency.Semaphore;
+import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.containers.MultiMap;
 import com.intellij.util.continuation.ContinuationPause;
 import com.intellij.util.messages.Topic;
@@ -1216,44 +1217,21 @@ public class ChangeListManagerImpl extends ChangeListManagerEx implements Projec
       public void process(final AbstractVcs vcs, final List<VirtualFile> items) {
         final CheckinEnvironment environment = vcs.getCheckinEnvironment();
         if (environment != null) {
-          final Set<VirtualFile> descendant = new HashSet<VirtualFile>();
-          final Set<VirtualFile> parents = new HashSet<VirtualFile>();
-          for (VirtualFile item : items) {
-            final Processor<VirtualFile> addProcessor = new Processor<VirtualFile>() {
-              @Override
-              public boolean process(VirtualFile file) {
-                if (statusChecker.value(getStatus(file))) {
-                  descendant.add(file);
-                }
-                return true;
-              }
-            };
+          Set<VirtualFile> descendants = getUnversionedDescendantsRecursively(items, statusChecker);
+          Set<VirtualFile> parents =
+            vcs.areDirectoriesVersionedItems() ? getUnversionedParents(items, statusChecker) : Collections.<VirtualFile>emptySet();
 
-            VcsRootIterator.iterateVfUnderVcsRoot(myProject, item, addProcessor);
-            collectParentsToBeAdded(vcs, parents, item, statusChecker);
-          }
-          final List<VcsException> result = environment.scheduleUnversionedFilesForAddition(new ArrayList<VirtualFile>(descendant));
-          allProcessedFiles.addAll(descendant);
+          // it is assumed that not-added parents of files passed to scheduleUnversionedFilesForAddition() will also be added to vcs
+          // (inside the method) - so common add logic just needs to refresh statuses of parents
+          List<VcsException> result = environment.scheduleUnversionedFilesForAddition(ContainerUtil.newArrayList(descendants));
+
+          allProcessedFiles.addAll(descendants);
           allProcessedFiles.addAll(parents);
           if (result != null) {
             exceptions.addAll(result);
           }
         }
       }
-
-      public void collectParentsToBeAdded(@NotNull AbstractVcs vcs,
-                                          @NotNull Set<VirtualFile> parents,
-                                          @NotNull VirtualFile item,
-                                          @NotNull Condition<FileStatus> statusChecker) {
-        if (vcs.areDirectoriesVersionedItems()) {
-          VirtualFile parent = item.getParent();
-
-          while (parent != null && statusChecker.value(getStatus(parent))) {
-            parents.add(parent);
-            parent = parent.getParent();
-          }
-        }
-      }
     });
 
     if (exceptions.size() > 0) {
@@ -1306,6 +1284,43 @@ public class ChangeListManagerImpl extends ChangeListManagerEx implements Projec
     }
   }
 
+  @NotNull
+  private Set<VirtualFile> getUnversionedDescendantsRecursively(@NotNull List<VirtualFile> items,
+                                                                @NotNull final Condition<FileStatus> condition) {
+    final Set<VirtualFile> result = ContainerUtil.newHashSet();
+    Processor<VirtualFile> addToResultProcessor = new Processor<VirtualFile>() {
+      @Override
+      public boolean process(VirtualFile file) {
+        if (condition.value(getStatus(file))) {
+          result.add(file);
+        }
+        return true;
+      }
+    };
+
+    for (VirtualFile item : items) {
+      VcsRootIterator.iterateVfUnderVcsRoot(myProject, item, addToResultProcessor);
+    }
+
+    return result;
+  }
+
+  @NotNull
+  private Set<VirtualFile> getUnversionedParents(@NotNull Collection<VirtualFile> items, @NotNull Condition<FileStatus> condition) {
+    HashSet<VirtualFile> result = ContainerUtil.newHashSet();
+
+    for (VirtualFile item : items) {
+      VirtualFile parent = item.getParent();
+
+      while (parent != null && condition.value(getStatus(parent))) {
+        result.add(parent);
+        parent = parent.getParent();
+      }
+    }
+
+    return result;
+  }
+
   @Override
   public Project getProject() {
     return myProject;