show progress for cleanup taks; make cancelable (IDEA-92533)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 4 Sep 2015 12:27:41 +0000 (15:27 +0300)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 4 Sep 2015 13:16:43 +0000 (16:16 +0300)
platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java
platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java
platform/lang-impl/src/com/intellij/codeInspection/ex/QuickFixAction.java
platform/lang-impl/src/com/intellij/codeInspection/ex/SequentialCleanupTask.java

index 3d16648f29c5842f067621867765b3eb96ed6de5..14a31ecabd1bd8180c9ac19d4287ed27d47fed0c 100644 (file)
@@ -91,7 +91,7 @@ public class CleanupInspectionIntention implements IntentionAction, HighPriority
     
     final String templatePresentationText = "Apply Fixes";
     final SequentialModalProgressTask progressTask =
-      new SequentialModalProgressTask(project, templatePresentationText, false);
+      new SequentialModalProgressTask(project, templatePresentationText, true);
     final PerformFixesTask fixesTask = new PerformFixesTask(project, descriptions.toArray(new CommonProblemDescriptor[descriptions.size()]), progressTask);
     CommandProcessor.getInstance().executeCommand(project, new Runnable() {
       @Override
index c5a32724a2fb92ab8a339b7694f13e29fe9e4bce..b4530caaddcdf527241e417f0f3d181823cc7848 100644 (file)
@@ -912,12 +912,13 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
       });
       return;
     }
+    final String title = "Code Cleanup";
     Runnable runnable = new Runnable() {
       @Override
       public void run() {
         if (!FileModificationService.getInstance().preparePsiElementsForWrite(results.keySet())) return;
 
-        final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, "Code Cleanup", true);
+        final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, title, true);
         progressTask.setMinIterationTime(200);
         progressTask.setTask(new SequentialCleanupTask(project, results, progressTask));
         CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@@ -926,17 +927,12 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
             if (commandName != null) {
               CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
             }
-            ApplicationManager.getApplication().runWriteAction(new Runnable() {
-              @Override
-              public void run() {
-                ProgressManager.getInstance().run(progressTask);
-              }
-            });
+            ProgressManager.getInstance().run(progressTask);
             if (postRunnable != null) {
               ApplicationManager.getApplication().invokeLater(postRunnable);
             }
           }
-        }, commandName, null);
+        }, title, null);
       }
     };
     if (ApplicationManager.getApplication().isDispatchThread()) {
index 52d0133ee3640c576d4181ac665fdd49e4764757..d21c1cf1e6782dbf2e1bb3733cf5a18dc4a88711 100644 (file)
@@ -152,7 +152,7 @@ public class QuickFixAction extends AnAction {
         public void run() {
           CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
           final SequentialModalProgressTask progressTask =
-            new SequentialModalProgressTask(project, templatePresentationText, false);
+            new SequentialModalProgressTask(project, templatePresentationText, true);
           progressTask.setMinIterationTime(200);
           progressTask.setTask(new PerformFixesTask(project, descriptors, ignoredElements, progressTask, context));
           ProgressManager.getInstance().run(progressTask);
index 92f5361a60a7297da24fa22f75bd35f237529660..3911b0da5a4bbee5543a2866f47ccb9291c546fb 100644 (file)
 package com.intellij.codeInspection.ex;
 
 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
+import com.intellij.openapi.actionSystem.ActionManager;
+import com.intellij.openapi.application.AccessToken;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.WriteAction;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.progress.ProcessCanceledException;
 import com.intellij.openapi.progress.ProgressIndicator;
@@ -32,16 +36,27 @@ class SequentialCleanupTask implements SequentialTask {
   private static final Logger LOG = Logger.getInstance(SequentialCleanupTask.class);
 
   private final Project myProject;
-  private final LinkedHashMap<PsiFile, List<HighlightInfo>> myResults;
-  private Iterator<PsiFile> myFileIterator;
+  private final List<Pair<PsiFile, HighlightInfo>> myResults = new ArrayList<Pair<PsiFile, HighlightInfo>>();
   private final SequentialModalProgressTask myProgressTask;
   private int myCount = 0;
   
   public SequentialCleanupTask(Project project, LinkedHashMap<PsiFile, List<HighlightInfo>> results, SequentialModalProgressTask task) {
     myProject = project;
-    myResults = results;
+    for (Map.Entry<PsiFile, List<HighlightInfo>> entry : results.entrySet()) {
+      PsiFile file = entry.getKey();
+      List<HighlightInfo> infos = entry.getValue();
+      // sort from bottom to top
+      Collections.sort(infos, new Comparator<HighlightInfo>() {
+        @Override
+        public int compare(HighlightInfo info1, HighlightInfo info2) {
+          return info2.getStartOffset() - info1.getStartOffset();
+        }
+      });
+      for (HighlightInfo info : infos) {
+        myResults.add(Pair.create(file, info));
+      }
+    }
     myProgressTask = task;
-    myFileIterator = myResults.keySet().iterator();
   }
 
   @Override
@@ -49,42 +64,35 @@ class SequentialCleanupTask implements SequentialTask {
 
   @Override
   public boolean isDone() {
-    return myFileIterator == null || !myFileIterator.hasNext();
+    return myCount > myResults.size() - 1;
   }
 
   @Override
   public boolean iteration() {
+    final Pair<PsiFile, HighlightInfo> pair = myResults.get(myCount++);
     final ProgressIndicator indicator = myProgressTask.getIndicator();
     if (indicator != null) {
-      indicator.setFraction((double) myCount++/myResults.size());
+      indicator.setFraction((double) myCount/myResults.size());
+      indicator.setText("Processing " + pair.first.getName());
     }
-    final PsiFile file = myFileIterator.next();
-    final List<HighlightInfo> infos = myResults.get(file);
-    // sort from bottom to top
-    Collections.sort(infos, new Comparator<HighlightInfo>() {
-      @Override
-      public int compare(HighlightInfo info1, HighlightInfo info2) {
-        return info2.getStartOffset() - info1.getStartOffset();
+    for (final Pair<HighlightInfo.IntentionActionDescriptor, TextRange> actionRange : pair.second.quickFixActionRanges) {
+      final AccessToken token = WriteAction.start();
+      try {
+        actionRange.getFirst().getAction().invoke(myProject, null, pair.first);
       }
-    });
-    for (HighlightInfo info : infos) {
-      for (final Pair<HighlightInfo.IntentionActionDescriptor, TextRange> actionRange : info.quickFixActionRanges) {
-        try {
-          actionRange.getFirst().getAction().invoke(myProject, null, file);
-        }
-        catch (ProcessCanceledException e) {
-          throw e;
-        }
-        catch (Exception e) {
-          LOG.error(e);
-        }
+      catch (ProcessCanceledException e) {
+        throw e;
+      }
+      catch (Exception e) {
+        LOG.error(e);
+      }
+      finally {
+        token.finish();
       }
     }
     return true;
   }
 
   @Override
-  public void stop() {
-    myFileIterator = null;
-  }
+  public void stop() {}
 }