diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java index 3554612676d1..4e9450b0305e 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java @@ -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)); } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationFieldGutter.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationFieldGutter.java index 0e0c2b0cdb18..cb7bd75d0cb1 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationFieldGutter.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationFieldGutter.java @@ -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 diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationPresentation.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationPresentation.java index 08d7b20d0121..28bb81e686a7 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationPresentation.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotationPresentation.java @@ -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 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 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); + } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/annotate/TextAnnotationPresentation.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/annotate/TextAnnotationPresentation.java index 1fca77641739..8d610295d65b 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/annotate/TextAnnotationPresentation.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/annotate/TextAnnotationPresentation.java @@ -25,4 +25,5 @@ public interface TextAnnotationPresentation { EditorFontType getFontType(int line); ColorKey getColor(int line); List getActions(int line); + void gutterClosed(); }