diff --git a/platform/collaboration-tools/src/com/intellij/collaboration/file/codereview/CodeReviewDiffVirtualFile.kt b/platform/collaboration-tools/src/com/intellij/collaboration/file/codereview/CodeReviewDiffVirtualFile.kt index aba9081b82db..bfcc976b35ee 100644 --- a/platform/collaboration-tools/src/com/intellij/collaboration/file/codereview/CodeReviewDiffVirtualFile.kt +++ b/platform/collaboration-tools/src/com/intellij/collaboration/file/codereview/CodeReviewDiffVirtualFile.kt @@ -3,7 +3,6 @@ package com.intellij.collaboration.file.codereview import com.intellij.diff.editor.DiffFileType import com.intellij.diff.editor.DiffVirtualFile -import com.intellij.diff.tools.combined.CombinedDiffModelBuilder import com.intellij.diff.tools.combined.CombinedDiffVirtualFile import com.intellij.ide.actions.SplitAction import com.intellij.openapi.fileTypes.FileType @@ -43,7 +42,6 @@ abstract class CodeReviewDiffVirtualFile(name: String) @ApiStatus.Experimental abstract class CodeReviewCombinedDiffVirtualFile(sourceId: String, name: String) : CombinedDiffVirtualFile(sourceId, name), - CombinedDiffModelBuilder, VirtualFilePathWrapper { init { diff --git a/platform/diff-impl/src/com/intellij/diff/editor/DiffEditorProvider.kt b/platform/diff-impl/src/com/intellij/diff/editor/DiffEditorProvider.kt index abf92b595960..f76e8e4783a0 100644 --- a/platform/diff-impl/src/com/intellij/diff/editor/DiffEditorProvider.kt +++ b/platform/diff-impl/src/com/intellij/diff/editor/DiffEditorProvider.kt @@ -1,7 +1,9 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.diff.editor -import com.intellij.diff.tools.combined.* +import com.intellij.diff.tools.combined.CombinedDiffComponentFactoryProvider +import com.intellij.diff.tools.combined.CombinedDiffEditor +import com.intellij.diff.tools.combined.CombinedDiffVirtualFile import com.intellij.ide.structureView.StructureViewBuilder import com.intellij.openapi.components.service import com.intellij.openapi.fileEditor.FileEditor @@ -28,14 +30,7 @@ internal class DiffEditorProvider : DefaultPlatformFileEditorProvider, Structure override fun createEditor(project: Project, file: VirtualFile): FileEditor { if (file is CombinedDiffVirtualFile) { - val sourceId = file.sourceId - val modelRepository = project.service() - var combinedDiffModel = modelRepository.findModel(sourceId) - if (combinedDiffModel == null && file is CombinedDiffModelBuilder) { - combinedDiffModel = file.createModel(sourceId) - modelRepository.registerModel(sourceId, combinedDiffModel) - } - requireNotNull(combinedDiffModel) { "Combined diff model doesn't registered for $sourceId" } + val combinedDiffModel = file.createModel() val factory = project.service().create(combinedDiffModel) val editor = CombinedDiffEditor(file, factory) DiffRequestProcessorEditorCustomizer.customize(file, editor, factory.model.context) diff --git a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffEditor.kt b/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffEditor.kt index c112c385c0eb..e4ada5eca294 100644 --- a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffEditor.kt +++ b/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffEditor.kt @@ -7,7 +7,7 @@ import com.intellij.openapi.fileEditor.FileEditorWithTextEditors import com.intellij.openapi.util.Disposer import javax.swing.JComponent -internal class CombinedDiffEditor(file: CombinedDiffVirtualFile, private val factory: CombinedDiffComponentFactory) : +class CombinedDiffEditor(file: CombinedDiffVirtualFile, val factory: CombinedDiffComponentFactory) : DiffEditorBase(file, factory.getMainComponent(), factory.ourDisposable), FileEditorWithTextEditors { override fun dispose() { diff --git a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffModelRepository.kt b/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffModelRepository.kt deleted file mode 100644 index d3fbe8f3444f..000000000000 --- a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffModelRepository.kt +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.diff.tools.combined - -import com.intellij.openapi.Disposable -import com.intellij.openapi.components.Service -import com.intellij.openapi.util.Disposer - -@Service(Service.Level.PROJECT) -class CombinedDiffModelRepository : Disposable { - - private val models: HashMap = hashMapOf() - - fun registerModel(sourceId: String, model: CombinedDiffModel) { - disposeIfRegistered(sourceId) - - Disposer.register(model.ourDisposable) { - models.remove(sourceId) - } - models[sourceId] = model - } - - fun findModel(sourceId: String): CombinedDiffModel? = models[sourceId] - - private fun disposeIfRegistered(sourceId: String) { - val diffModel = models.remove(sourceId) ?: return - Disposer.dispose(diffModel.ourDisposable) - } - - override fun dispose() { - models.clear() // TODO: why not dispose? - } -} diff --git a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffVirtualFile.kt b/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffVirtualFile.kt index 81663160f754..c30446919861 100644 --- a/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffVirtualFile.kt +++ b/platform/diff-impl/src/com/intellij/diff/tools/combined/CombinedDiffVirtualFile.kt @@ -2,13 +2,13 @@ package com.intellij.diff.tools.combined import com.intellij.diff.editor.DiffVirtualFileBase -import org.jetbrains.annotations.ApiStatus -open class CombinedDiffVirtualFile(val sourceId: String, name: String, private val path: String? = null) : DiffVirtualFileBase(name) { - override fun getPath(): String = path ?: name +abstract class CombinedDiffVirtualFile(name: String, private val path: String = name) : DiffVirtualFileBase(name) { + override fun getPath(): String = path + abstract fun createModel(): CombinedDiffModel } -@ApiStatus.Internal -interface CombinedDiffModelBuilder { - fun createModel(id: String): CombinedDiffModelImpl +class CombinedDiffVirtualFileImpl(val model: CombinedDiffModel, name: String, path: String = name) + : CombinedDiffVirtualFile(name, path) { + override fun createModel(): CombinedDiffModel = model } diff --git a/platform/lvcs-impl/src/com/intellij/platform/lvcs/impl/ui/CombinedActivityDiffPreview.kt b/platform/lvcs-impl/src/com/intellij/platform/lvcs/impl/ui/CombinedActivityDiffPreview.kt index 57a88b5c44fa..578a4b49acd2 100644 --- a/platform/lvcs-impl/src/com/intellij/platform/lvcs/impl/ui/CombinedActivityDiffPreview.kt +++ b/platform/lvcs-impl/src/com/intellij/platform/lvcs/impl/ui/CombinedActivityDiffPreview.kt @@ -24,14 +24,12 @@ import javax.swing.JComponent internal open class CombinedActivityDiffPreview(project: Project, targetComponent: JComponent, val scope: ActivityScope, parentDisposable: Disposable) : CombinedDiffPreview(project, targetComponent, true, parentDisposable) { - override val sourceId: String = targetComponent.id - override fun createModel(): CombinedDiffPreviewModel { return CombinedActivityDiffPreviewModel(project, scope, parentDisposable) } override fun getCombinedDiffTabTitle(): String { - val filePath = model.selected?.filePath + val filePath = model?.selected?.filePath if (filePath != null) return LocalHistoryBundle.message("activity.diff.tab.title.file", filePath.name) if (scope == ActivityScope.Recent) return LocalHistoryBundle.message("activity.diff.tab.title.recent") return LocalHistoryBundle.message("activity.diff.tab.title") diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsEditorTabTitleProvider.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsEditorTabTitleProvider.kt index 2def8352a033..7e2606757b77 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsEditorTabTitleProvider.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsEditorTabTitleProvider.kt @@ -2,11 +2,10 @@ package com.intellij.openapi.vcs.changes import com.intellij.diff.editor.DiffRequestProcessorEditor -import com.intellij.diff.tools.combined.CombinedDiffModelRepository +import com.intellij.diff.tools.combined.CombinedDiffEditor import com.intellij.openapi.application.EDT import com.intellij.openapi.application.ModalityState import com.intellij.openapi.application.asContextElement -import com.intellij.openapi.components.service import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.fileEditor.impl.EditorTabTitleProvider import com.intellij.openapi.progress.blockingContext @@ -38,15 +37,18 @@ private class VcsEditorTabTitleProvider : EditorTabTitleProvider, DumbAware { } val supplier = { val editors = FileEditorManager.getInstance(project).getEditors(file) - val editor = ContainerUtil.findInstance(editors, DiffRequestProcessorEditor::class.java) - val processor = editor?.processor - if (file is PreviewDiffVirtualFile) { - file.provider.getEditorTabName(processor) - } - else { - val sourceId = (file as CombinedDiffPreviewVirtualFile).sourceId - val diffModel = project.service().findModel(sourceId) - diffModel?.context?.getUserData(COMBINED_DIFF_PREVIEW_TAB_NAME)?.invoke() + when (file) { + is PreviewDiffVirtualFile -> { + val editor = ContainerUtil.findInstance(editors, DiffRequestProcessorEditor::class.java) + val processor = editor?.processor + file.provider.getEditorTabName(processor) + } + is CombinedDiffPreviewVirtualFile -> { + val editor = ContainerUtil.findInstance(editors, CombinedDiffEditor::class.java) + val model = editor?.factory?.model + model?.context?.getUserData(COMBINED_DIFF_PREVIEW_TAB_NAME)?.invoke() + } + else -> null } } if (EDT.isCurrentThreadEdt()) { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedDiffPreview.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedDiffPreview.kt index 293fc73dcd19..c2da5815bc91 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedDiffPreview.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedDiffPreview.kt @@ -6,7 +6,6 @@ import com.intellij.diff.tools.combined.* import com.intellij.openapi.Disposable import com.intellij.openapi.ListSelection import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.components.service import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key @@ -19,13 +18,12 @@ import com.intellij.openapi.vcs.changes.actions.diff.CombinedDiffPreviewModel.Co import com.intellij.openapi.vcs.changes.ui.ChangeDiffRequestChain import com.intellij.openapi.vfs.VirtualFile import com.intellij.vcsUtil.Delegates -import org.jetbrains.annotations.NonNls import javax.swing.JComponent @JvmField internal val COMBINED_DIFF_PREVIEW_TAB_NAME = Key.create<() -> @NlsContexts.TabTitle String>("combined_diff_preview_tab_name") -class CombinedDiffPreviewVirtualFile(sourceId: String) : CombinedDiffVirtualFile(sourceId, "") +abstract class CombinedDiffPreviewVirtualFile() : CombinedDiffVirtualFile("") abstract class CombinedDiffPreview(project: Project, targetComponent: JComponent, @@ -33,17 +31,23 @@ abstract class CombinedDiffPreview(project: Project, parentDisposable: Disposable) : EditorTabPreviewBase(project, parentDisposable) { - protected abstract val sourceId: String + override val previewFile: VirtualFile by lazy { + object : CombinedDiffPreviewVirtualFile() { + override fun createModel(): CombinedDiffModel = this@CombinedDiffPreview.createModel() + } + } - override val previewFile: VirtualFile by lazy { CombinedDiffPreviewVirtualFile(sourceId) } + override val updatePreviewProcessor get() = getOrCreateModel() - override val updatePreviewProcessor get() = model + var model: CombinedDiffPreviewModel? = null + private set - protected open val model by lazy { createModel().also { model -> customizeModel(sourceId, model) } } - - protected fun customizeModel(sourceId: String, model: CombinedDiffPreviewModel) { - model.context.putUserData(COMBINED_DIFF_PREVIEW_TAB_NAME, ::getCombinedDiffTabTitle) - project.service().registerModel(sourceId, model) + private fun getOrCreateModel(): CombinedDiffPreviewModel { + model?.let { return it } + model = createModel().also { + it.context.putUserData(COMBINED_DIFF_PREVIEW_TAB_NAME, ::getCombinedDiffTabTitle) + } + return model!! } override fun updatePreview(fromModelRefresh: Boolean) { @@ -65,6 +69,7 @@ abstract class CombinedDiffPreview(project: Project, } protected open fun updatePreview() { + val model = model ?: return if (model.ourDisposable.isDisposed) return model.context.putUserData(COMBINED_DIFF_VIEWER_KEY, null) val changes = model.iterateAllChanges().toList() @@ -87,12 +92,10 @@ abstract class CombinedDiffPreview(project: Project, event.presentation.isVisible = event.isFromActionToolbar || event.presentation.isEnabled } - override fun getCurrentName(): String? = model.selected?.presentableName - override fun hasContent(): Boolean = model.requests.isNotEmpty() + override fun getCurrentName(): String? = model?.selected?.presentableName + override fun hasContent(): Boolean = !model?.requests.isNullOrEmpty() - internal fun getFileSize(): Int = model.requests.size - - protected val JComponent.id: @NonNls String get() = javaClass.name + "@" + Integer.toHexString(hashCode()) + internal fun getFileSize(): Int = model?.requests?.size ?: 0 } abstract class CombinedDiffPreviewModel(project: Project, parentDisposable: Disposable) : diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedTreeDiffPreview.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedTreeDiffPreview.kt index f72466fdf9b9..4930d623091e 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedTreeDiffPreview.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/CombinedTreeDiffPreview.kt @@ -17,8 +17,6 @@ abstract class CombinedTreeDiffPreview(protected val tree: ChangesTree, parentDisposable: Disposable) : CombinedDiffPreview(tree.project, targetComponent, needSetupOpenPreviewListeners, parentDisposable) { - override val sourceId: String get() = tree.id - constructor(tree: ChangesTree, parentDisposable: Disposable) : this(tree, tree, false, true, parentDisposable) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ShowCombinedDiffAction.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ShowCombinedDiffAction.kt index 55f1a7b1b8b6..8f9fce0cec2e 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ShowCombinedDiffAction.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ShowCombinedDiffAction.kt @@ -5,7 +5,6 @@ import com.intellij.diff.editor.DiffEditorTabFilesManager import com.intellij.diff.tools.combined.* import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.components.service import com.intellij.openapi.project.DumbAwareAction import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key @@ -13,7 +12,6 @@ import com.intellij.openapi.vcs.VcsBundle import com.intellij.openapi.vcs.VcsDataKeys import com.intellij.openapi.vcs.changes.Change import com.intellij.openapi.vcs.history.VcsDiffUtil -import java.util.* class ShowCombinedDiffAction : DumbAwareAction() { override fun update(e: AnActionEvent) { @@ -45,11 +43,9 @@ class ShowCombinedDiffAction : DumbAwareAction() { CombinedBlockProducer(id, producer) } - val sourceId = UUID.randomUUID().toString() val model = CombinedDiffModelImpl(project) - project.service().registerModel(sourceId, model) model.setBlocks(producers) - val allInOneDiffFile = CombinedDiffVirtualFile(sourceId, VcsBundle.message("changes.combined.diff")) + val allInOneDiffFile = CombinedDiffVirtualFileImpl(model, VcsBundle.message("changes.combined.diff")) DiffEditorTabFilesManager.getInstance(project).showDiffFile(allInOneDiffFile, true) } diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogCombinedDiffPreview.kt b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogCombinedDiffPreview.kt index 394da6fa3e73..129734cbe3ab 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogCombinedDiffPreview.kt +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/VcsLogCombinedDiffPreview.kt @@ -19,7 +19,7 @@ class VcsLogCombinedDiffPreview(private val browser: VcsLogChangesBrowser) : Com } override fun getCombinedDiffTabTitle(): String { - val filePath = model.selected?.filePath + val filePath = model?.selected?.filePath return if (filePath == null) VcsLogBundle.message("vcs.log.diff.preview.editor.empty.tab.name") else VcsLogBundle.message("vcs.log.diff.preview.editor.tab.name", filePath.name) } diff --git a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHNewPRDiffVirtualFile.kt b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHNewPRDiffVirtualFile.kt index 8473243a5804..daf90437ff44 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHNewPRDiffVirtualFile.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHNewPRDiffVirtualFile.kt @@ -6,6 +6,7 @@ import com.intellij.collaboration.file.codereview.CodeReviewDiffVirtualFile import com.intellij.diff.chains.DiffRequestChain import com.intellij.diff.impl.DiffRequestProcessor import com.intellij.diff.tools.combined.CombinedBlockProducer +import com.intellij.diff.tools.combined.CombinedDiffModel import com.intellij.diff.tools.combined.CombinedDiffModelImpl import com.intellij.diff.tools.combined.CombinedPathBlockId import com.intellij.openapi.project.Project @@ -51,7 +52,7 @@ internal data class GHNewPRCombinedDiffPreviewVirtualFile(private val fileManage override fun isValid(): Boolean = isFileValid(fileManagerId, project, repository) - override fun createModel(id: String): CombinedDiffModelImpl { + override fun createModel(): CombinedDiffModel { val model = CombinedDiffModelImpl(project) val dataContext = GHPRDataContextRepository.getInstance(project).findContext(repository)!! val diffModel: GHPRDiffRequestModel = dataContext.newPRDiffModel diff --git a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffVirtualFile.kt b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffVirtualFile.kt index dd57a1d8487d..1a42f21859db 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffVirtualFile.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/pullrequest/GHPRDiffVirtualFile.kt @@ -4,7 +4,7 @@ package org.jetbrains.plugins.github.pullrequest import com.intellij.collaboration.file.codereview.CodeReviewCombinedDiffVirtualFile import com.intellij.collaboration.file.codereview.CodeReviewDiffVirtualFile import com.intellij.diff.impl.DiffRequestProcessor -import com.intellij.diff.tools.combined.CombinedDiffModelImpl +import com.intellij.diff.tools.combined.CombinedDiffModel import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.vcs.editor.ComplexPathVirtualFileSystem @@ -50,8 +50,9 @@ internal data class GHPRCombinedDiffPreviewVirtualFile(private val fileManagerId override fun isValid(): Boolean = isFileValid(fileManagerId, project, repository) - override fun createModel(id: String): CombinedDiffModelImpl = - project.service().createCombinedDiffModel(repository, pullRequest) + override fun createModel(): CombinedDiffModel { + return project.service().createCombinedDiffModel(repository, pullRequest) + } } private fun createSourceId(fileManagerId: String, repository: GHRepositoryCoordinates, pullRequest: GHPRIdentifier) = diff --git a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/mergerequest/file/GitLabMergeRequestDiffFile.kt b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/mergerequest/file/GitLabMergeRequestDiffFile.kt index 3f75e4ac9e5e..04c51db29a8b 100644 --- a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/mergerequest/file/GitLabMergeRequestDiffFile.kt +++ b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/mergerequest/file/GitLabMergeRequestDiffFile.kt @@ -6,6 +6,7 @@ import com.intellij.collaboration.file.codereview.CodeReviewDiffVirtualFile import com.intellij.collaboration.ui.codereview.diff.CodeReviewDiffHandlerHelper import com.intellij.collaboration.util.KeyValuePair import com.intellij.diff.impl.DiffRequestProcessor +import com.intellij.diff.tools.combined.CombinedDiffModel import com.intellij.diff.tools.combined.CombinedDiffModelImpl import com.intellij.diff.util.DiffUserDataKeys import com.intellij.openapi.actionSystem.ActionManager @@ -66,8 +67,9 @@ internal data class GitLabMergeRequestCombinedDiffFile( override fun isValid(): Boolean = isFileValid(project, connectionId) - override fun createModel(id: String): CombinedDiffModelImpl = - project.service().createGitLabCombinedDiffModel(connectionId, mergeRequestIid) + override fun createModel(): CombinedDiffModel { + return project.service().createGitLabCombinedDiffModel(connectionId, mergeRequestIid) + } } private fun createSourceId(connectionId: String, glProject: GitLabProjectCoordinates, mergeRequestIid: String) =