vcs: move dispose logic out of AnnotationFieldGutter

This commit is contained in:
Aleksey Pivovarov
2016-10-17 16:15:25 +03:00
parent 1bfe8a5380
commit f7bcc376c7
4 changed files with 40 additions and 7 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.vcs.actions;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Separator;
import com.intellij.openapi.actionSystem.ToggleAction;
@@ -27,12 +28,14 @@ import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.annotate.AnnotationGutterActionProvider;
import com.intellij.openapi.vcs.annotate.AnnotationSourceSwitcher;
import com.intellij.openapi.vcs.annotate.FileAnnotation;
import com.intellij.openapi.vcs.annotate.LineAnnotationAspect;
import com.intellij.openapi.vcs.changes.VcsAnnotationLocalChangesListener;
import com.intellij.openapi.vcs.history.VcsFileRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vcs.impl.UpToDateLineNumberProviderImpl;
@@ -89,8 +92,23 @@ public class AnnotateToggleAction extends ToggleAction implements DumbAware {
@NotNull final FileAnnotation fileAnnotation,
@NotNull final AbstractVcs vcs,
@Nullable final UpToDateLineNumberProvider upToDateLineNumberProvider) {
Disposable disposable = new Disposable() {
@Override
public void dispose() {
fileAnnotation.dispose();
}
};
if (fileAnnotation.getFile() != null && fileAnnotation.getFile().isInLocalFileSystem()) {
ProjectLevelVcsManager.getInstance(project).getAnnotationLocalChangesListener().registerAnnotation(fileAnnotation.getFile(), fileAnnotation);
VcsAnnotationLocalChangesListener changesListener = ProjectLevelVcsManager.getInstance(project).getAnnotationLocalChangesListener();
changesListener.registerAnnotation(fileAnnotation.getFile(), fileAnnotation);
Disposer.register(disposable, new Disposable() {
@Override
public void dispose() {
changesListener.unregisterAnnotation(fileAnnotation.getFile(), fileAnnotation);
}
});
}
editor.getGutter().closeAllAnnotations();
@@ -116,7 +134,7 @@ public class AnnotateToggleAction extends ToggleAction implements DumbAware {
upToDateLineNumberProvider :
new UpToDateLineNumberProviderImpl(editor.getDocument(), project);
final AnnotationPresentation presentation = new AnnotationPresentation(fileAnnotation, getUpToDateLineNumber, switcher);
final AnnotationPresentation presentation = new AnnotationPresentation(fileAnnotation, getUpToDateLineNumber, switcher, disposable);
if (currentFile != null && vcs.getCommittedChangesProvider() != null) {
presentation.addAction(new ShowDiffFromAnnotation(fileAnnotation, vcs, currentFile));
}
@@ -86,9 +86,7 @@ public abstract class AnnotationFieldGutter implements ActiveAnnotationGutter {
@Override
public void gutterClosed() {
ProjectLevelVcsManager.getInstance(myAnnotation.getProject()).getAnnotationLocalChangesListener()
.unregisterAnnotation(myAnnotation.getFile(), myAnnotation);
myAnnotation.dispose();
myPresentation.gutterClosed();
}
@Nullable
@@ -15,13 +15,14 @@
*/
package com.intellij.openapi.vcs.actions;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.localVcs.UpToDateLineNumberProvider;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vcs.annotate.*;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.util.Consumer;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,25 +36,33 @@ class AnnotationPresentation implements TextAnnotationPresentation {
@Nullable private final AnnotationSourceSwitcher mySwitcher;
private final ArrayList<AnAction> myActions = new ArrayList<>();
@NotNull private final Disposable myDisposable;
private boolean myDisposed = false;
AnnotationPresentation(@NotNull FileAnnotation fileAnnotation,
@NotNull UpToDateLineNumberProvider upToDateLineNumberProvider,
@Nullable final AnnotationSourceSwitcher switcher) {
@Nullable AnnotationSourceSwitcher switcher,
@NotNull Disposable disposable) {
myUpToDateLineNumberProvider = upToDateLineNumberProvider;
myFileAnnotation = fileAnnotation;
mySwitcher = switcher;
myDisposable = disposable;
}
@Override
public EditorFontType getFontType(final int line) {
VcsRevisionNumber revision = myFileAnnotation.originalRevision(line);
VcsRevisionNumber currentRevision = myFileAnnotation.getCurrentRevision();
return currentRevision != null && currentRevision.equals(revision) ? EditorFontType.BOLD : EditorFontType.PLAIN;
}
@Override
public ColorKey getColor(final int line) {
if (mySwitcher == null) return AnnotationSource.LOCAL.getColor();
return mySwitcher.getAnnotationSource(line).getColor();
}
@Override
public List<AnAction> getActions(int line) {
int correctedNumber = myUpToDateLineNumberProvider.getLineNumber(line);
for (AnAction action : myActions) {
@@ -79,4 +88,11 @@ class AnnotationPresentation implements TextAnnotationPresentation {
public void addAction(AnAction action, int index) {
myActions.add(index, action);
}
@Override
public void gutterClosed() {
if (myDisposed) return;
myDisposed = true;
Disposer.dispose(myDisposable);
}
}
@@ -25,4 +25,5 @@ public interface TextAnnotationPresentation {
EditorFontType getFontType(int line);
ColorKey getColor(int line);
List<AnAction> getActions(int line);
void gutterClosed();
}