replace duplicates: search for duplicates in background; progress

This commit is contained in:
Anna Kozlova
2012-05-30 13:29:28 +04:00
parent ca8083ca87
commit be6a12bde1
@@ -31,8 +31,11 @@ import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
@@ -75,13 +78,13 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
AnalysisUIOptions.getInstance(project), element);
dlg.show();
if (dlg.isOK()) {
ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Locate duplicates", true) {
@Override
public void run() {
ProgressManager.getInstance().getProgressIndicator().setIndeterminate(true);
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
invokeOnScope(project, member, dlg.getScope(AnalysisUIOptions.getInstance(project), scope, project, module));
}
}, "Locate duplicates", true, project) ;
});
}
}
@@ -124,10 +127,22 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
public static void invokeOnScope(final Project project, final Set<PsiMember> members, final AnalysisScope scope, boolean silent) {
final Map<PsiMember, List<Match>> duplicates = new HashMap<PsiMember, List<Match>>();
final int fileCount = scope.getFileCount();
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null) {
progressIndicator.setIndeterminate(false);
}
scope.accept(new PsiRecursiveElementVisitor() {
private int myFileCount = 0;
@Override public void visitFile(final PsiFile file) {
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null && progressIndicator.isCanceled()) return;
if (progressIndicator != null){
if (progressIndicator.isCanceled()) return;
progressIndicator.setFraction(((double)myFileCount++)/fileCount);
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile != null) {
progressIndicator.setText2(ProjectUtil.calcRelativeToProjectPath(virtualFile, project));
}
}
for (PsiMember method : members) {
final List<Match> matchList = hasDuplicates(file, method);
for (Iterator<Match> iterator = matchList.iterator(); iterator.hasNext(); ) {
@@ -174,14 +189,14 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
}
private static void replaceDuplicate(final Project project, final Map<PsiMember, List<Match>> duplicates, final Set<PsiMember> methods) {
LocalHistoryAction a = LocalHistory.getInstance().startAction(REFACTORING_NAME);
try {
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null && progressIndicator.isCanceled()) return;
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null && progressIndicator.isCanceled()) return;
final Runnable replaceRunnable = new Runnable() {
@Override
public void run() {
final Runnable replaceRunnable = new Runnable() {
@Override
public void run() {
LocalHistoryAction a = LocalHistory.getInstance().startAction(REFACTORING_NAME);
try {
for (final PsiMember member : methods) {
final List<Match> matches = duplicates.get(member);
if (matches == null) continue;
@@ -201,16 +216,16 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
});
}
}, REFACTORING_NAME, REFACTORING_NAME);
WindowManager.getInstance().getStatusBar(project).setInfo("");
}
}
};
ApplicationManager.getApplication().invokeLater(replaceRunnable, ModalityState.NON_MODAL);
}
finally {
a.finish();
}
finally {
a.finish();
}
}
};
ApplicationManager.getApplication().invokeLater(replaceRunnable, ModalityState.NON_MODAL);
}
public static List<Match> hasDuplicates(final PsiFile file, final PsiMember member) {