vcs annotations preloader (IDEA-CR-7773):

- do not schedule preloading for the same file more than once if not completed yet
- schedule preloading on file select not open
- do not enable in power save mode
- do not preload if file is already closed
- do not preload if annotation provider is not cacheable
This commit is contained in:
Egor.Ushakov
2016-01-13 15:00:48 +03:00
parent ec2e976f7b
commit 16f96599bc
@@ -15,8 +15,10 @@
*/
package com.intellij.openapi.vcs.annotate;
import com.intellij.ide.PowerSaveMode;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerAdapter;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
@@ -24,38 +26,55 @@ import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Alarm;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NotNull;
/**
* @author egor
*/
public class AnnotationsPreloader {
private final Alarm myAlarm;
private final MergingUpdateQueue myUpdateQueue;
private final Project myProject;
public AnnotationsPreloader(final Project project) {
myAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, project);
myProject = project;
myUpdateQueue = new MergingUpdateQueue("Annotations preloader queue", 1000, true, null, project, null, false);
project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull final VirtualFile file) {
if (!Registry.is("vcs.annotations.preload")) return;
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
if (!isEnabled()) return;
VirtualFile file = event.getNewFile();
if (file != null) {
schedulePreloading(file);
}
}
});
}
AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
if (vcs == null) return;
final AnnotationProvider annotationProvider = vcs.getCachingAnnotationProvider();
assert annotationProvider != null;
private static boolean isEnabled() {
// TODO: check cores number?
return Registry.is("vcs.annotations.preload") && !PowerSaveMode.isEnabled();
}
myAlarm.addRequest(new Runnable() {
@Override
public void run() {
try {
annotationProvider.annotate(file);
}
catch (VcsException ignore) {
}
private void schedulePreloading(@NotNull final VirtualFile file) {
AbstractVcs vcs = ProjectLevelVcsManager.getInstance(myProject).getVcsFor(file);
if (vcs == null || !(vcs.getAnnotationProvider() instanceof VcsCacheableAnnotationProvider)) return;
final AnnotationProvider annotationProvider = vcs.getCachingAnnotationProvider();
assert annotationProvider != null;
myUpdateQueue.queue(new Update(file) {
@Override
public void run() {
try {
if (FileEditorManager.getInstance(myProject).isFileOpen(file)) {
annotationProvider.annotate(file);
}
}, 0);
}
catch (VcsException ignore) {
}
}
});
}