diff: extract "diff_opened_in_new_window" key to diff related listener
authorDmitry Zhuravlev <dmitry.zhuravlev@jetbrains.com>
Fri, 18 Jun 2021 15:49:41 +0000 (18:49 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Tue, 22 Jun 2021 11:50:16 +0000 (11:50 +0000)
GitOrigin-RevId: 1acdf12fab5a04b6b3a85e55a154ce0e10c6f044

platform/diff-impl/src/com/intellij/diff/editor/DiffEditorTabFilesManager.kt
platform/vcs-impl/resources/META-INF/VCS.xml
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/DiffEditorTabFilesListener.kt [new file with mode: 0644]
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ShowDiffInEditorTooltipInstaller.kt
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsEditorTabFilesManager.kt
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/MoveDiffPreviewAction.kt

index 90bd58a9c0e8e2eecdbeb3a88a3e99e1b7e92f30..9462609b3ff37aeb177d880fab18e26eff83cef4 100644 (file)
@@ -6,6 +6,8 @@ import com.intellij.openapi.fileEditor.FileEditor
 import com.intellij.openapi.fileEditor.FileEditorManager
 import com.intellij.openapi.options.advanced.AdvancedSettings
 import com.intellij.openapi.project.Project
+import com.intellij.openapi.util.Key
+import com.intellij.openapi.vfs.VirtualFile
 import org.jetbrains.annotations.ApiStatus
 
 @ApiStatus.Internal
@@ -18,11 +20,17 @@ interface DiffEditorTabFilesManager {
   companion object {
     const val SHOW_DIFF_IN_EDITOR_SETTING = "show.diff.as.editor.tab"
 
+    @JvmStatic
+    fun isDiffOpenedInNewWindow(file: VirtualFile): Boolean = DIFF_OPENED_IN_NEW_WINDOW.get(file, false)
+
     @JvmStatic
     fun getInstance(project: Project): DiffEditorTabFilesManager = project.service()
   }
 }
 
+@JvmField
+val DIFF_OPENED_IN_NEW_WINDOW = Key<Boolean>("DIFF_OPENED_IN_NEW_WINDOW")
+
 class DefaultDiffTabFilesManager(private val project: Project) : DiffEditorTabFilesManager {
 
   override fun showDiffFile(diffFile: ChainDiffVirtualFile, focusEditor: Boolean): Array<out FileEditor> {
index ebe728fcc92c4b62e01dd1003ca16e0990c9d541..25160abc31350bbba1862a68f29920187436634f 100644 (file)
@@ -55,4 +55,8 @@
     <listener class="com.intellij.openapi.vcs.annotate.AnnotationsPreloader$AnnotationsPreloaderFileEditorManagerListener"
               topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>
   </projectListeners>
+  <applicationListeners>
+    <listener class="com.intellij.openapi.vcs.changes.DiffEditorTabStateListener"
+              topic="com.intellij.openapi.vcs.changes.VcsEditorTabFilesListener"/>
+  </applicationListeners>
 </idea-plugin>
diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/DiffEditorTabFilesListener.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/DiffEditorTabFilesListener.kt
new file mode 100644 (file)
index 0000000..3183af8
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2000-2021 JetBrains s.r.o. and contributors. 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.vcs.changes
+
+import com.intellij.diff.editor.DIFF_OPENED_IN_NEW_WINDOW
+import com.intellij.diff.editor.DiffContentVirtualFile
+import com.intellij.openapi.vfs.VirtualFile
+
+abstract class DefaultDiffEditorTabFilesListener : VcsEditorTabFilesListener {
+  final override fun shouldOpenInNewWindowChanged(file: VirtualFile, shouldOpenInNewWindow: Boolean) {
+    if (file is DiffContentVirtualFile) {
+      shouldOpenInNewWindowChanged(file as DiffContentVirtualFile, shouldOpenInNewWindow)
+    }
+  }
+
+  abstract fun shouldOpenInNewWindowChanged(diffFile: DiffContentVirtualFile, shouldOpenInNewWindow: Boolean)
+}
+
+
+class DiffEditorTabStateListener : DefaultDiffEditorTabFilesListener() {
+
+  override fun shouldOpenInNewWindowChanged(diffFile: DiffContentVirtualFile, shouldOpenInNewWindow: Boolean) {
+    (diffFile as? VirtualFile)?.putUserData(DIFF_OPENED_IN_NEW_WINDOW, if (shouldOpenInNewWindow) true else null)
+  }
+}
index 60c6463478561ac8194c2e9bb3fc998ecc80f3f0..a697453d1c283409b5f41246571b5298844ed6fc 100644 (file)
@@ -1,6 +1,7 @@
-// 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. and contributors. 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.vcs.changes
 
+import com.intellij.diff.editor.DiffContentVirtualFile
 import com.intellij.diff.editor.DiffRequestProcessorEditorCustomizer
 import com.intellij.diff.impl.DiffRequestProcessor
 import com.intellij.openapi.Disposable
@@ -23,7 +24,7 @@ class ShowDiffInEditorTooltipInstaller : DiffRequestProcessorEditorCustomizer {
 
 private class ShowDiffInEditorTabTooltipHolder(disposable: Disposable,
                                                private val diffProcessor: DiffRequestProcessor) :
-  VcsEditorTabFilesListener, Disposable {
+  DefaultDiffEditorTabFilesListener(), Disposable {
 
   companion object {
     const val TOOLTIP_ID = "show.diff.in.editor"
@@ -39,7 +40,7 @@ private class ShowDiffInEditorTabTooltipHolder(disposable: Disposable,
     ApplicationManager.getApplication().messageBus.connect(this).subscribe(VcsEditorTabFilesListener.TOPIC, this)
   }
 
-  override fun shouldOpenInNewWindowChanged(shouldOpenInNewWindow: Boolean) {
+  override fun shouldOpenInNewWindowChanged(diffFile: DiffContentVirtualFile, shouldOpenInNewWindow: Boolean) {
     if (shouldOpenInNewWindow) {
       showGotItTooltip()
     }
index f83fff6c13955ac62e7b020580de7413e7e8779f..a15f9877d4d5932d3a2a9447bd29d6e8a3705b12 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
+// Copyright 2000-2021 JetBrains s.r.o. and contributors. 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.vcs.changes
 
 import com.intellij.diff.editor.DiffContentVirtualFile
@@ -12,7 +12,6 @@ import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
 import com.intellij.openapi.fileEditor.impl.EditorWindow
 import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl
 import com.intellij.openapi.project.Project
-import com.intellij.openapi.util.Key
 import com.intellij.openapi.vfs.VirtualFile
 import com.intellij.util.concurrency.annotations.RequiresEdt
 import com.intellij.util.messages.Topic
@@ -34,9 +33,8 @@ class VcsEditorTabFilesManager :
         //currently shouldOpenInNewWindow is bound only to diff files
         if (file is DiffContentVirtualFile && source is FileEditorManagerEx) {
           val isOpenInNewWindow = source.findFloatingWindowForFile(file) != null
-          file.putUserData(OPENED_IN_NEW_WINDOW, if (isOpenInNewWindow) true else null)
           shouldOpenInNewWindow = isOpenInNewWindow
-          messageBus.syncPublisher(VcsEditorTabFilesListener.TOPIC).shouldOpenInNewWindowChanged(isOpenInNewWindow)
+          messageBus.syncPublisher(VcsEditorTabFilesListener.TOPIC).shouldOpenInNewWindowChanged(file, isOpenInNewWindow)
         }
       }
     })
@@ -89,9 +87,6 @@ class VcsEditorTabFilesManager :
   override fun dispose() {}
 
   companion object {
-    @JvmField
-    val OPENED_IN_NEW_WINDOW = Key<Boolean>("OPENED_IN_NEW_WINDOW")
-
     @JvmStatic
     fun getInstance(): VcsEditorTabFilesManager = service()
 
@@ -104,7 +99,7 @@ class VcsEditorTabFilesManager :
 
 interface VcsEditorTabFilesListener {
   @RequiresEdt
-  fun shouldOpenInNewWindowChanged(shouldOpenInNewWindow: Boolean)
+  fun shouldOpenInNewWindowChanged(file: VirtualFile, shouldOpenInNewWindow: Boolean)
 
   companion object {
     @JvmField
index f5e68d0c2bb2204bca8cceb70b0a781319a7f946..b5e11f3db70f0179e8e660abfe071784ae475233 100644 (file)
@@ -2,13 +2,13 @@
 package com.intellij.openapi.vcs.changes.actions.diff
 
 import com.intellij.diff.editor.DiffContentVirtualFile
+import com.intellij.diff.editor.DiffEditorTabFilesManager.Companion.isDiffOpenedInNewWindow
 import com.intellij.openapi.actionSystem.AnActionEvent
 import com.intellij.openapi.fileEditor.FileEditorManager
 import com.intellij.openapi.project.DumbAwareAction
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.vcs.VcsDataKeys.VIRTUAL_FILES
 import com.intellij.openapi.vcs.changes.VcsEditorTabFilesManager
-import com.intellij.openapi.vcs.changes.VcsEditorTabFilesManager.Companion.OPENED_IN_NEW_WINDOW
 import com.intellij.openapi.vfs.VirtualFile
 import com.intellij.util.containers.JBIterable
 
@@ -44,12 +44,10 @@ private fun AnActionEvent.findDiffPreviewFile(): VirtualFile? {
   return if (selectedFile is DiffContentVirtualFile) selectedFile else null
 }
 
-private fun VirtualFile.openedInNewWindow() = OPENED_IN_NEW_WINDOW.get(this, false)
-
 internal class MoveDiffPreviewToEditorAction : MoveDiffPreviewAction(false) {
-  override fun isEnabledAndVisible(project: Project, file: VirtualFile): Boolean = file.openedInNewWindow()
+  override fun isEnabledAndVisible(project: Project, file: VirtualFile): Boolean = isDiffOpenedInNewWindow(file)
 }
 
 internal class MoveDiffPreviewToNewWindowAction : MoveDiffPreviewAction(true) {
-  override fun isEnabledAndVisible(project: Project, file: VirtualFile): Boolean = !file.openedInNewWindow()
+  override fun isEnabledAndVisible(project: Project, file: VirtualFile): Boolean = !isDiffOpenedInNewWindow(file)
 }