2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInspection.ex;
18 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.progress.ProcessCanceledException;
21 import com.intellij.openapi.progress.ProgressIndicator;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.Pair;
24 import com.intellij.openapi.util.TextRange;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.util.SequentialModalProgressTask;
27 import com.intellij.util.SequentialTask;
31 class SequentialCleanupTask implements SequentialTask {
32 private static final Logger LOG = Logger.getInstance(SequentialCleanupTask.class);
34 private final Project myProject;
35 private final LinkedHashMap<PsiFile, List<HighlightInfo>> myResults;
36 private Iterator<PsiFile> myFileIterator;
37 private final SequentialModalProgressTask myProgressTask;
38 private int myCount = 0;
40 public SequentialCleanupTask(Project project, LinkedHashMap<PsiFile, List<HighlightInfo>> results, SequentialModalProgressTask task) {
43 myProgressTask = task;
44 myFileIterator = myResults.keySet().iterator();
48 public void prepare() {}
51 public boolean isDone() {
52 return myFileIterator == null || !myFileIterator.hasNext();
56 public boolean iteration() {
57 final ProgressIndicator indicator = myProgressTask.getIndicator();
58 if (indicator != null) {
59 indicator.setFraction((double) myCount++/myResults.size());
61 final PsiFile file = myFileIterator.next();
62 final List<HighlightInfo> infos = myResults.get(file);
63 // sort from bottom to top
64 Collections.sort(infos, new Comparator<HighlightInfo>() {
66 public int compare(HighlightInfo info1, HighlightInfo info2) {
67 return info2.getStartOffset() - info1.getStartOffset();
70 for (HighlightInfo info : infos) {
71 for (final Pair<HighlightInfo.IntentionActionDescriptor, TextRange> actionRange : info.quickFixActionRanges) {
73 actionRange.getFirst().getAction().invoke(myProject, null, file);
75 catch (ProcessCanceledException e) {
88 myFileIterator = null;