From d72a1402acd6dcdabca657d6ed501c56068393c8 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 14 Aug 2018 10:16:22 +0200 Subject: [PATCH] do not use EditorEventListener.TOPIC for now --- .../command/impl/DocumentUndoProvider.java | 39 ++++++++++--------- .../openapi/command/impl/UndoManagerImpl.java | 19 ++++----- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/command/impl/DocumentUndoProvider.java b/platform/platform-impl/src/com/intellij/openapi/command/impl/DocumentUndoProvider.java index 3f7688fd3419..2920bf2ff2bf 100644 --- a/platform/platform-impl/src/com/intellij/openapi/command/impl/DocumentUndoProvider.java +++ b/platform/platform-impl/src/com/intellij/openapi/command/impl/DocumentUndoProvider.java @@ -1,28 +1,42 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.command.impl; +import com.intellij.openapi.Disposable; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.undo.DocumentReference; import com.intellij.openapi.command.undo.DocumentReferenceManager; import com.intellij.openapi.command.undo.UndoConstants; import com.intellij.openapi.command.undo.UndoManager; import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.editor.event.DocumentEvent; -import com.intellij.openapi.editor.event.EditorEventListener; +import com.intellij.openapi.editor.event.DocumentListener; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.AbstractFileViewProvider; -import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class DocumentUndoProvider { +public class DocumentUndoProvider implements Disposable { private static final Key UNDOING_EDITOR_CHANGE = Key.create("DocumentUndoProvider.UNDOING_EDITOR_CHANGE"); - DocumentUndoProvider(@Nullable Project project, @NotNull MessageBusConnection busConnection) { - busConnection.subscribe(EditorEventListener.TOPIC, new MyEditorDocumentListener(project)); + private final Project myProject; + + DocumentUndoProvider(Project project) { + MyEditorDocumentListener documentListener = new MyEditorDocumentListener(); + myProject = project; + + EditorFactory.getInstance().getEventMulticaster().addDocumentListener(documentListener, this); + } + + @Override + public void dispose() { + } + + private UndoManagerImpl getUndoManager() { + return (UndoManagerImpl)(myProject == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(myProject)); } public static void startDocumentUndo(@Nullable Document doc) { @@ -33,18 +47,7 @@ public class DocumentUndoProvider { if (doc != null) doc.putUserData(UNDOING_EDITOR_CHANGE, null); } - private static class MyEditorDocumentListener implements EditorEventListener { - @Nullable - private final Project myProject; - - public MyEditorDocumentListener(@Nullable Project project) { - myProject = project; - } - - private UndoManagerImpl getUndoManager() { - return (UndoManagerImpl)(myProject == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(myProject)); - } - + private class MyEditorDocumentListener implements DocumentListener { @Override public void beforeDocumentChange(@NotNull DocumentEvent e) { Document document = e.getDocument(); @@ -80,7 +83,7 @@ public class DocumentUndoProvider { && shouldRecordActions(document); } - private static boolean shouldRecordActions(final Document document) { + private boolean shouldRecordActions(final Document document) { if (document.getUserData(UndoConstants.DONT_RECORD_UNDO) == Boolean.TRUE) return false; VirtualFile vFile = FileDocumentManager.getInstance().getFile(document); diff --git a/platform/platform-impl/src/com/intellij/openapi/command/impl/UndoManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/command/impl/UndoManagerImpl.java index f8447780f832..aec93780c604 100644 --- a/platform/platform-impl/src/com/intellij/openapi/command/impl/UndoManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/command/impl/UndoManagerImpl.java @@ -33,7 +33,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ex.WindowManagerEx; import com.intellij.psi.ExternalChangeAction; import com.intellij.util.ObjectUtils; -import com.intellij.util.messages.MessageBusConnection; +import com.intellij.util.messages.MessageBus; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,15 +86,15 @@ public class UndoManagerImpl extends UndoManager implements Disposable { return Registry.intValue("undo.documentUndoLimit"); } - public UndoManagerImpl() { - this(null); + public UndoManagerImpl(CommandProcessor commandProcessor) { + this(null, commandProcessor); } - public UndoManagerImpl(@Nullable ProjectEx project) { + public UndoManagerImpl(@Nullable ProjectEx project, @NotNull CommandProcessor commandProcessor) { myProject = project; if (myProject == null || !myProject.isDefault()) { - runStartupActivity(myProject); + runStartupActivity(myProject, commandProcessor); } myMerger = new CommandMerger(this); @@ -109,7 +109,7 @@ public class UndoManagerImpl extends UndoManager implements Disposable { public void dispose() { } - private void runStartupActivity(@Nullable Project project) { + private void runStartupActivity(@Nullable Project project, @NotNull CommandProcessor commandProcessor) { myUndoProviders = project == null ? Extensions.getExtensions(UndoProvider.EP_NAME) : Extensions.getExtensions(UndoProvider.PROJECT_EP_NAME, myProject); @@ -121,8 +121,9 @@ public class UndoManagerImpl extends UndoManager implements Disposable { myEditorProvider = new FocusBasedCurrentEditorProvider(); - MessageBusConnection busConnection = (project == null ? ApplicationManager.getApplication().getMessageBus() : project.getMessageBus()).connect(this); - busConnection.subscribe(CommandListener.TOPIC, new CommandListener() { + MessageBus messageBus = project == null ? ApplicationManager.getApplication().getMessageBus() : project.getMessageBus(); + + messageBus.connect(this).subscribe(CommandListener.TOPIC, new CommandListener() { private boolean myStarted; @Override @@ -156,7 +157,7 @@ public class UndoManagerImpl extends UndoManager implements Disposable { } }); - new DocumentUndoProvider(myProject, busConnection); + Disposer.register(this, new DocumentUndoProvider(myProject)); } public boolean isActive() {