diff: cleanup
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Mon, 29 Jun 2015 17:28:29 +0000 (20:28 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Thu, 3 Sep 2015 16:28:09 +0000 (19:28 +0300)
platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateDiffViewerAction.java

index e1587fca84a555e99e8bd13d413e313c74c40a3c..018878f3a64dc934e79f3d049d6a772ee542f3ea 100644 (file)
@@ -186,75 +186,43 @@ public class AnnotateDiffViewerAction extends DumbAwareAction {
   private static FileAnnotationLoader createAnnotationsLoader(@NotNull Project project, @NotNull DiffRequest request, @NotNull Side side) {
     Change change = request.getUserData(ChangeDiffRequestProducer.CHANGE_KEY);
     if (change != null) {
-      final ContentRevision revision = side.select(change.getBeforeRevision(), change.getAfterRevision());
-      if (revision == null) return null;
-      AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
-      if (vcs == null) return null;
-
-      final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
-      if (annotationProvider == null) return null;
-
-      if (revision instanceof CurrentContentRevision) {
-        return new FileAnnotationLoader(vcs, false) {
-          @Override
-          public FileAnnotation compute() throws VcsException {
-            final VirtualFile file = ((CurrentContentRevision)revision).getVirtualFile();
-            if (file == null) throw new VcsException("Failed to annotate: file not found");
-            return annotationProvider.annotate(file);
-          }
-        };
-      }
-      else {
-        if (!(annotationProvider instanceof AnnotationProviderEx)) return null;
-        return new FileAnnotationLoader(vcs, true) {
-          @Override
-          public FileAnnotation compute() throws VcsException {
-            return ((AnnotationProviderEx)annotationProvider).annotate(revision.getFile(), revision.getRevisionNumber());
-          }
-        };
+      ContentRevision revision = side.select(change.getBeforeRevision(), change.getAfterRevision());
+      if (revision != null) {
+        AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
+
+        if (revision instanceof CurrentContentRevision) {
+          VirtualFile file = ((CurrentContentRevision)revision).getVirtualFile();
+          FileAnnotationLoader loader = doCreateAnnotationsLoader(vcs, file);
+          if (loader != null) return loader;
+        }
+        else {
+          FileAnnotationLoader loader = doCreateAnnotationsLoader(vcs, revision.getFile(), revision.getRevisionNumber());
+          if (loader != null) return loader;
+        }
       }
     }
 
     if (request instanceof ContentDiffRequest) {
       ContentDiffRequest requestEx = (ContentDiffRequest)request;
-      if (requestEx.getContents().size() != 2) return null;
-
-      DiffContent content = side.select(requestEx.getContents());
-      if (content instanceof FileContent) {
-        final VirtualFile file = ((FileContent)content).getFile();
-        AbstractVcs vcs = VcsUtil.getVcsFor(project, file);
-        if (vcs == null) return null;
-
-        final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
-        if (annotationProvider == null) return null;
+      if (requestEx.getContents().size() == 2) {
+
+        DiffContent content = side.select(requestEx.getContents());
+        if (content instanceof FileContent) {
+          VirtualFile file = ((FileContent)content).getFile();
+          AbstractVcs vcs = VcsUtil.getVcsFor(project, file);
+          FileAnnotationLoader loader = doCreateAnnotationsLoader(vcs, file);
+          if (loader != null) return loader;
+        }
 
-        return new FileAnnotationLoader(vcs, false) {
-          @Override
-          public FileAnnotation compute() throws VcsException {
-            return annotationProvider.annotate(file);
+        VcsFileRevision[] fileRevisions = request.getUserData(VcsHistoryUtil.REVISIONS_KEY);
+        if (fileRevisions != null && fileRevisions.length == 2) {
+          VcsFileRevision fileRevision = side.select(fileRevisions);
+          if (fileRevision instanceof VcsFileRevisionEx) {
+            FilePath filePath = ((VcsFileRevisionEx)fileRevision).getPath();
+            AbstractVcs vcs = VcsUtil.getVcsFor(project, filePath);
+            FileAnnotationLoader loader = doCreateAnnotationsLoader(vcs, filePath, fileRevision.getRevisionNumber());
+            if (loader != null) return loader;
           }
-        };
-      }
-
-      VcsFileRevision[] fileRevisions = request.getUserData(VcsHistoryUtil.REVISIONS_KEY);
-      if (fileRevisions != null && fileRevisions.length == 2) {
-        VcsFileRevision fileRevision = side.select(fileRevisions);
-        if (fileRevision instanceof VcsFileRevisionEx) {
-          final FilePath path = ((VcsFileRevisionEx)fileRevision).getPath();
-          final VcsRevisionNumber revisionNumber = fileRevision.getRevisionNumber();
-
-          AbstractVcs vcs = VcsUtil.getVcsFor(project, path);
-          if (vcs == null) return null;
-
-          final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
-          if (!(annotationProvider instanceof AnnotationProviderEx)) return null;
-
-          return new FileAnnotationLoader(vcs, true) {
-            @Override
-            public FileAnnotation compute() throws VcsException {
-              return ((AnnotationProviderEx)annotationProvider).annotate(path, revisionNumber);
-            }
-          };
         }
       }
     }
@@ -262,6 +230,37 @@ public class AnnotateDiffViewerAction extends DumbAwareAction {
     return null;
   }
 
+  @Nullable
+  private static FileAnnotationLoader doCreateAnnotationsLoader(@Nullable AbstractVcs vcs, @Nullable final VirtualFile file) {
+    if (vcs == null || file == null) return null;
+    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
+    if (annotationProvider == null) return null;
+
+    // TODO: cache them too, listening for ProjectLevelVcsManager.getInstance(project).getAnnotationLocalChangesListener() ?
+    return new FileAnnotationLoader(vcs, false) {
+      @Override
+      public FileAnnotation compute() throws VcsException {
+        return annotationProvider.annotate(file);
+      }
+    };
+  }
+
+  @Nullable
+  private static FileAnnotationLoader doCreateAnnotationsLoader(@Nullable AbstractVcs vcs,
+                                                                @Nullable final FilePath path,
+                                                                @Nullable final VcsRevisionNumber revisionNumber) {
+    if (vcs == null || path == null || revisionNumber == null) return null;
+    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
+    if (!(annotationProvider instanceof AnnotationProviderEx)) return null;
+
+    return new FileAnnotationLoader(vcs, true) {
+      @Override
+      public FileAnnotation compute() throws VcsException {
+        return ((AnnotationProviderEx)annotationProvider).annotate(path, revisionNumber);
+      }
+    };
+  }
+
   private static void putDataToCache(@NotNull DiffViewerBase viewer, @NotNull Side side, @NotNull AnnotationData data) {
     AnnotationData[] cache = viewer.getRequest().getUserData(CACHE_KEY);
     if (cache == null || cache.length != 2) {