diff-preview: move escape handler setup to file editor constructor
authorDmitry Zhuravlev <dmitry.zhuravlev@jetbrains.com>
Thu, 29 Apr 2021 11:46:26 +0000 (14:46 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Fri, 30 Apr 2021 11:29:44 +0000 (11:29 +0000)
Since moving editor tab to a new window frame trigger recreation of the file editor, therefore all escape handlers will be lost.

GitOrigin-RevId: a6191525715cae6d3fd2819fdfc9e659f2ef08d7

platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt
platform/diff-impl/src/com/intellij/diff/editor/DiffVirtualFile.kt
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/EditorTabPreview.kt
platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogDiffPreview.kt

index 449f54a46a5598a6f2835f1c06a6970fb351da6d..6bd9c1bd78f0eb181c967f4cb43e16f68242a27b 100644 (file)
@@ -6,6 +6,7 @@ import com.intellij.diff.util.DiffUserDataKeysEx
 import com.intellij.diff.util.DiffUtil
 import com.intellij.diff.util.FileEditorBase
 import com.intellij.openapi.Disposable
+import com.intellij.openapi.actionSystem.CommonShortcuts
 import com.intellij.openapi.diagnostic.logger
 import com.intellij.openapi.diff.DiffBundle
 import com.intellij.openapi.fileEditor.FileEditor
@@ -46,6 +47,8 @@ open class DiffRequestProcessorEditor(
 
     processor.component.registerKeyboardAction({ Disposer.dispose(this) },
                                                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW)
+
+    file.getUserData(DiffVirtualFile.ESCAPE_HANDLER)?.registerCustomShortcutSet(CommonShortcuts.ESCAPE, component, this)
   }
 
   override fun getComponent(): JComponent = panel
index 92cc73adc5eed8808353d068478442dd23f75ea6..662fab7a5993461905a440ab453c353791896dba 100644 (file)
@@ -2,7 +2,9 @@
 package com.intellij.diff.editor
 
 import com.intellij.diff.impl.DiffRequestProcessor
+import com.intellij.openapi.actionSystem.AnAction
 import com.intellij.openapi.project.Project
+import com.intellij.openapi.util.Key
 import com.intellij.openapi.vfs.VirtualFileWithoutContent
 import com.intellij.testFramework.LightVirtualFile
 
@@ -14,4 +16,9 @@ abstract class DiffVirtualFile(name: String) :
   override fun isWritable(): Boolean = false
 
   override fun toString(): String = "${javaClass.name}@${Integer.toHexString(hashCode())}"
+
+  companion object {
+    @JvmField
+    val ESCAPE_HANDLER = Key<AnAction?>("ESCAPE_HANDLER")
+  }
 }
index fbb6e13ef22f19d0070ed8a738a2ba32c984434c..ed1b5ac7fd7a5c3578320156fcb86bf688914334 100644 (file)
@@ -3,6 +3,7 @@ package com.intellij.openapi.vcs.changes
 
 import com.intellij.diff.chains.DiffRequestChain
 import com.intellij.diff.chains.SimpleDiffRequestChain
+import com.intellij.diff.editor.DiffVirtualFile
 import com.intellij.diff.impl.DiffRequestProcessor
 import com.intellij.diff.util.DiffUserDataKeysEx
 import com.intellij.ide.actions.SplitAction
@@ -10,7 +11,6 @@ import com.intellij.openapi.Disposable
 import com.intellij.openapi.ListSelection
 import com.intellij.openapi.actionSystem.ActionManager
 import com.intellij.openapi.actionSystem.AnActionEvent
-import com.intellij.openapi.actionSystem.CommonShortcuts.ESCAPE
 import com.intellij.openapi.actionSystem.IdeActions
 import com.intellij.openapi.fileEditor.FileEditor
 import com.intellij.openapi.fileEditor.FileEditorManager
@@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project
 import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.util.Disposer.isDisposed
 import com.intellij.openapi.vcs.changes.ui.ChangesTree
+import com.intellij.openapi.vfs.VirtualFile
 import com.intellij.openapi.wm.ToolWindowManager
 import com.intellij.util.EditSourceOnDoubleClickHandler.isToggleEvent
 import com.intellij.util.IJSwingUtilities
@@ -134,13 +135,9 @@ abstract class EditorTabPreview(protected val diffProcessor: DiffRequestProcesso
     updatePreviewProcessor?.refresh(false)
     if (!hasContent()) return false
 
-    val editors = openPreview(project, previewFile, focusEditor)
+    escapeHandler?.let { handler -> registerEscapeHandler(previewFile, handler) }
 
-    escapeHandler?.let { handler ->
-      for (editor in editors) {
-        registerEscapeHandler(editor, handler)
-      }
-    }
+    openPreview(project, previewFile, focusEditor)
 
     return true
   }
@@ -159,8 +156,8 @@ abstract class EditorTabPreview(protected val diffProcessor: DiffRequestProcesso
       return VcsEditorTabFilesManager.getInstance().openFile(project, file, focusEditor)
     }
 
-    fun registerEscapeHandler(editor: FileEditor, handler: Runnable) {
-      EditorTabPreviewEscapeAction(handler).registerCustomShortcutSet(ESCAPE, editor.component, editor)
+    fun registerEscapeHandler(file: VirtualFile, handler: Runnable) {
+      file.putUserData(DiffVirtualFile.ESCAPE_HANDLER, EditorTabPreviewEscapeAction(handler))
     }
   }
 }
index b57bb45c9d5bc6a7f7a1e5af4912afeb1157e675..c941eb1edf300aa3ad6316b6057b8d123e99d5c8 100644 (file)
@@ -12,6 +12,7 @@ import com.intellij.openapi.ui.Splitter
 import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.util.registry.Registry
 import com.intellij.openapi.vcs.changes.*
+import com.intellij.openapi.vcs.changes.EditorTabPreview.Companion.registerEscapeHandler
 import com.intellij.openapi.vcs.changes.ui.ChangesViewContentManager
 import com.intellij.openapi.wm.IdeFocusManager
 import com.intellij.openapi.wm.ToolWindowManager
@@ -127,10 +128,8 @@ abstract class EditorDiffPreview(private val project: Project,
       toolWindow?.activate({ IdeFocusManager.getInstance(project).requestFocus(getOwnerComponent(), true) }, false)
     }
 
-    val editors = EditorTabPreview.openPreview(project, previewFile, focusEditor)
-    for (editor in editors) {
-      EditorTabPreview.registerEscapeHandler(editor, escapeHandler)
-    }
+    registerEscapeHandler(previewFile, escapeHandler)
+    EditorTabPreview.openPreview(project, previewFile, focusEditor)
   }
 
   fun closePreview() {