From 70a13ff1cad56d2f796e6ee48172d4aedd2d1a71 Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Thu, 29 Apr 2021 14:46:26 +0300 Subject: [PATCH] diff-preview: move escape handler setup to file editor constructor 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 --- .../diff/editor/DiffRequestProcessorEditor.kt | 3 +++ .../com/intellij/diff/editor/DiffVirtualFile.kt | 7 +++++++ .../openapi/vcs/changes/EditorTabPreview.kt | 15 ++++++--------- .../vcs/log/ui/frame/VcsLogDiffPreview.kt | 7 +++---- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt index 449f54a46a55..6bd9c1bd78f0 100644 --- a/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt +++ b/platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt @@ -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 diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffVirtualFile.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffVirtualFile.kt index 92cc73adc5ee..662fab7a5993 100644 --- a/platform/diff-impl/src/com/intellij/diff/editor/DiffVirtualFile.kt +++ b/platform/diff-impl/src/com/intellij/diff/editor/DiffVirtualFile.kt @@ -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("ESCAPE_HANDLER") + } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/EditorTabPreview.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/EditorTabPreview.kt index fbb6e13ef22f..ed1b5ac7fd7a 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/EditorTabPreview.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/EditorTabPreview.kt @@ -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)) } } } diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogDiffPreview.kt b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogDiffPreview.kt index b57bb45c9d5b..c941eb1edf30 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogDiffPreview.kt +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogDiffPreview.kt @@ -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() { -- 2.32.0