diff-preview: extract Esc handler setup to common editor customizer
authorDmitry Zhuravlev <dmitry.zhuravlev@jetbrains.com>
Thu, 29 Apr 2021 17:59:38 +0000 (20:59 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Fri, 30 Apr 2021 11:29:46 +0000 (11:29 +0000)
This will unify Esc handler setup for all components which customized by such editor customizer.

GitOrigin-RevId: 2dd86a2389902fe9c1c84e64b4bb1562c9039abc

platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt [new file with mode: 0644]
platform/diff-impl/src/com/intellij/diff/editor/DiffRequestProcessorEditor.kt
platform/diff-impl/src/com/intellij/diff/util/FileEditorBase.kt
platform/vcs-impl/resources/META-INF/VcsExtensions.xml
plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffFileEditor.kt

diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffFileEditorEscapeHandlerCustomizer.kt
new file mode 100644 (file)
index 0000000..d203222
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright 2000-2021 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.diff.editor
+
+import com.intellij.diff.impl.DiffRequestProcessor
+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.fileEditor.FileEditor
+import com.intellij.openapi.util.Disposer
+import com.intellij.openapi.vfs.VirtualFile
+import java.awt.event.KeyEvent
+import javax.swing.JComponent
+import javax.swing.KeyStroke
+
+class DiffFileEditorEscapeHandlerCustomizer : DiffRequestProcessorEditorCustomizer {
+
+  override fun customize(file: VirtualFile, editor: FileEditor, processor: DiffRequestProcessor) {
+    if (editor !is FileEditorBase) return
+
+    if (!DiffUtil.isUserDataFlagSet(DiffUserDataKeysEx.DIFF_IN_EDITOR_WITH_EXPLICIT_DISPOSABLE, processor.context)) {
+      Disposer.register(editor, Disposable {
+        Disposer.dispose(processor)
+      })
+    }
+    Disposer.register(processor, Disposable {
+      editor.firePropertyChange(FileEditor.PROP_VALID, true, false)
+    })
+
+    processor.component.registerKeyboardAction({ Disposer.dispose(editor) },
+                                               KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW)
+
+    file.getUserData(DiffVirtualFile.ESCAPE_HANDLER)?.registerCustomShortcutSet(CommonShortcuts.ESCAPE, editor.component, editor)
+  }
+}
index 6bd9c1bd78f0eb181c967f4cb43e16f68242a27b..4bacea62fbbf63eabf2f8e6965149def6db9abc2 100644 (file)
@@ -2,25 +2,16 @@
 package com.intellij.diff.editor
 
 import com.intellij.diff.impl.DiffRequestProcessor
 package com.intellij.diff.editor
 
 import com.intellij.diff.impl.DiffRequestProcessor
-import com.intellij.diff.util.DiffUserDataKeysEx
-import com.intellij.diff.util.DiffUtil
 import com.intellij.diff.util.FileEditorBase
 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.diagnostic.logger
 import com.intellij.openapi.diff.DiffBundle
-import com.intellij.openapi.fileEditor.FileEditor
 import com.intellij.openapi.fileEditor.impl.EditorWindow
 import com.intellij.openapi.fileEditor.impl.EditorWindow
-import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.vfs.VirtualFile
 import java.awt.BorderLayout
 import java.awt.event.ContainerAdapter
 import java.awt.event.ContainerEvent
 import com.intellij.openapi.vfs.VirtualFile
 import java.awt.BorderLayout
 import java.awt.event.ContainerAdapter
 import java.awt.event.ContainerEvent
-import java.awt.event.KeyEvent
 import javax.swing.JComponent
 import javax.swing.JComponent
-import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW
 import javax.swing.JPanel
 import javax.swing.JPanel
-import javax.swing.KeyStroke
 
 open class DiffRequestProcessorEditor(
   private val file: DiffVirtualFile,
 
 open class DiffRequestProcessorEditor(
   private val file: DiffVirtualFile,
@@ -36,19 +27,6 @@ open class DiffRequestProcessorEditor(
 
   init {
     putUserData(EditorWindow.HIDE_TABS, true)
 
   init {
     putUserData(EditorWindow.HIDE_TABS, true)
-    if (!DiffUtil.isUserDataFlagSet(DiffUserDataKeysEx.DIFF_IN_EDITOR_WITH_EXPLICIT_DISPOSABLE, processor.context)) {
-      Disposer.register(this, Disposable {
-        Disposer.dispose(processor)
-      })
-    }
-    Disposer.register(processor, Disposable {
-      propertyChangeSupport.firePropertyChange(FileEditor.PROP_VALID, true, false)
-    })
-
-    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
   }
 
   override fun getComponent(): JComponent = panel
index 42366920531b3a97a0e05444ad21693f2e10adec..e7fc434eab3c7b89804949a1169fccd4b9205e15 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2000-2019 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.
+// Copyright 2000-2021 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.diff.util
 
 import com.intellij.codeHighlighting.BackgroundEditorHighlighter
 package com.intellij.diff.util
 
 import com.intellij.codeHighlighting.BackgroundEditorHighlighter
@@ -27,6 +27,10 @@ abstract class FileEditorBase : UserDataHolderBase(), FileEditor {
   override fun selectNotify() {}
   override fun deselectNotify() {}
 
   override fun selectNotify() {}
   override fun deselectNotify() {}
 
+  fun firePropertyChange(propName: String, oldValue: Boolean, newValue: Boolean) {
+    propertyChangeSupport.firePropertyChange(propName, oldValue, newValue)
+  }
+
   override fun addPropertyChangeListener(listener: PropertyChangeListener) {
     propertyChangeSupport.addPropertyChangeListener(listener)
   }
   override fun addPropertyChangeListener(listener: PropertyChangeListener) {
     propertyChangeSupport.addPropertyChangeListener(listener)
   }
index 6f27b03460d7231416e1c8515b4a4b088b455ca5..79962352a407e700c3587f737f2094467f5a32b6 100644 (file)
     <statistics.notificationIdsHolder implementation="com.intellij.openapi.vcs.VcsNotificationIdsHolder"/>
 
     <diff.editor.diffRequestProcessorEditorCustomizer implementation="com.intellij.openapi.vcs.changes.ShowDiffInEditorTooltipInstaller"/>
     <statistics.notificationIdsHolder implementation="com.intellij.openapi.vcs.VcsNotificationIdsHolder"/>
 
     <diff.editor.diffRequestProcessorEditorCustomizer implementation="com.intellij.openapi.vcs.changes.ShowDiffInEditorTooltipInstaller"/>
+    <diff.editor.diffRequestProcessorEditorCustomizer implementation="com.intellij.diff.editor.DiffFileEditorEscapeHandlerCustomizer"/>
     <registryKey key="patch.context.line.count" defaultValue="3" description="Number of context lines for creating patches"/>
   </extensions>
 
     <registryKey key="patch.context.line.count" defaultValue="3" description="Number of context lines for creating patches"/>
   </extensions>
 
index dc3c4b4701ce6d4d3036626d0748f62791842e93..afccc1109de61be0cd1dd5ced68f23d2c7bd3b6c 100644 (file)
@@ -1,18 +1,13 @@
-// Copyright 2000-2020 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.
+// Copyright 2000-2021 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 org.jetbrains.plugins.github.pullrequest
 
 package org.jetbrains.plugins.github.pullrequest
 
+import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor
 import com.intellij.diff.util.FileEditorBase
 import com.intellij.diff.util.FileEditorBase
-import com.intellij.openapi.fileEditor.FileEditor.PROP_VALID
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.util.Disposer
-import com.intellij.openapi.vfs.VirtualFile
-import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor
 import com.intellij.util.ui.update.MergingUpdateQueue
 import com.intellij.util.ui.update.Update
 import org.jetbrains.plugins.github.i18n.GithubBundle
 import com.intellij.util.ui.update.MergingUpdateQueue
 import com.intellij.util.ui.update.Update
 import org.jetbrains.plugins.github.i18n.GithubBundle
-import java.awt.event.KeyEvent
-import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW
-import javax.swing.KeyStroke
 
 internal class GHPRDiffFileEditor(project: Project,
                                   private val diffRequestModel: GHPRDiffRequestModel,
 
 internal class GHPRDiffFileEditor(project: Project,
                                   private val diffRequestModel: GHPRDiffRequestModel,
@@ -29,13 +24,6 @@ internal class GHPRDiffFileEditor(project: Project,
   override fun isValid() = !Disposer.isDisposed(diffProcessor)
 
   init {
   override fun isValid() = !Disposer.isDisposed(diffProcessor)
 
   init {
-    Disposer.register(this, diffProcessor)
-
-    diffProcessor.component.registerKeyboardAction({
-                                                     propertyChangeSupport.firePropertyChange(PROP_VALID, true, false)
-                                                   },
-                                                   KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW)
-
     diffRequestModel.addAndInvokeRequestChainListener(diffChainUpdateQueue) {
       val chain = diffRequestModel.requestChain
       diffChainUpdateQueue.run(Update.create(diffRequestModel) {
     diffRequestModel.addAndInvokeRequestChainListener(diffChainUpdateQueue) {
       val chain = diffRequestModel.requestChain
       diffChainUpdateQueue.run(Update.create(diffRequestModel) {