From 0f0a21de9aecad22be5dfc223f9df0c284e4fb27 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Fri, 16 Aug 2024 12:45:28 +0200 Subject: [PATCH] IJPL-160366 IDEA-104009 patch: suggest switching into editable view for patches GitOrigin-RevId: aa883807ee8fe1ff50dc609420899d8c3071eb5f --- .../resources/messages/DiffBundle.properties | 1 + platform/diff-impl/resources/api-dump.txt | 1 + .../diff/util/DiffUserDataKeysEx.java | 2 ++ .../editor/actionSystem/TypedAction.java | 2 +- .../patch/tool/DiffPatchFileEditorProvider.kt | 34 +++++++++++++++---- .../vcs/changes/patch/tool/PatchDiffViewer.kt | 2 ++ .../patch/tool/SideBySidePatchDiffViewer.kt | 4 +++ 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/platform/diff-api/resources/messages/DiffBundle.properties b/platform/diff-api/resources/messages/DiffBundle.properties index 1b3f0a46451c..e4878d744f5f 100644 --- a/platform/diff-api/resources/messages/DiffBundle.properties +++ b/platform/diff-api/resources/messages/DiffBundle.properties @@ -272,6 +272,7 @@ option.three.side.color.policy.merge.resolved=Resolved merge conflict notification.action.text.blank.diff.select.file=Select file\u2026 notification.action.text.blank.diff.recent=Recent editing.viewer.hint.enable.editing.text=This view is read-only. Enable editing +patch.editing.viewer.hint.enable.editing.text=This view is read-only. Switch to editing diff.unchanged.lines.folding.marker.renderer=marker: collapsed unchanged lines column.dirdiff.name=Name column.dirdiff.size=Size diff --git a/platform/diff-impl/resources/api-dump.txt b/platform/diff-impl/resources/api-dump.txt index 7d435c5cb18a..91a6013485ba 100644 --- a/platform/diff-impl/resources/api-dump.txt +++ b/platform/diff-impl/resources/api-dump.txt @@ -2701,6 +2701,7 @@ com.intellij.diff.util.DiffUserDataKeysEx - sf:MERGE_CANCEL_HANDLER:com.intellij.openapi.util.Key - sf:MERGE_CANCEL_MESSAGE:com.intellij.openapi.util.Key - sf:NAVIGATION_CONTEXT:com.intellij.openapi.util.Key +- sf:PATCH_FILE_PREVIEW_MODIFICATION_SWITCH:com.intellij.openapi.util.Key - sf:SCROLL_TO_CHANGE:com.intellij.openapi.util.Key - sf:SHOW_READ_ONLY_LOCK:com.intellij.openapi.util.Key - sf:TWO_SIDE_SPLITTER_PROPORTION:com.intellij.openapi.util.Key diff --git a/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java b/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java index 95de55984f76..ad1d59556ceb 100644 --- a/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java +++ b/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java @@ -77,6 +77,8 @@ public interface DiffUserDataKeysEx extends DiffUserDataKeys { Key LEFT_TOOLBAR = Key.create("Diff.LeftToolbar"); + Key PATCH_FILE_PREVIEW_MODIFICATION_SWITCH = Key.create("Diff.PatchFilePreviewModificationSwitch"); + /** * Add panel to the bottom of diff window. * If passed panel implements Disposable, it will be disposed when window is closed. diff --git a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java index 4ce1e05065d9..4a8e19becadb 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java @@ -109,7 +109,7 @@ public abstract class TypedAction { private static final class Handler implements TypedActionHandler { @Override public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) { - if (editor.isViewer()) { + if (!EditorModificationUtil.checkModificationAllowed(editor)) { return; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/DiffPatchFileEditorProvider.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/DiffPatchFileEditorProvider.kt index 70028e804f26..2601d1f3d356 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/DiffPatchFileEditorProvider.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/DiffPatchFileEditorProvider.kt @@ -1,6 +1,7 @@ // 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.openapi.vcs.changes.patch.tool +import com.intellij.diff.DiffContext import com.intellij.diff.editor.DiffEditorViewerFileEditor import com.intellij.diff.requests.DiffRequest import com.intellij.diff.requests.ErrorDiffRequest @@ -9,18 +10,18 @@ import com.intellij.diff.tools.combined.CombinedBlockProducer import com.intellij.diff.tools.combined.CombinedDiffComponentProcessor import com.intellij.diff.tools.combined.CombinedDiffManager import com.intellij.diff.tools.combined.CombinedDiffRegistry +import com.intellij.diff.util.DiffUserDataKeysEx import com.intellij.diff.util.DiffUtil import com.intellij.ide.structureView.StructureViewBuilder import com.intellij.openapi.ListSelection import com.intellij.openapi.diff.DiffBundle import com.intellij.openapi.diff.impl.patch.* import com.intellij.openapi.editor.Document +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.EditorModificationUtil import com.intellij.openapi.editor.event.DocumentEvent import com.intellij.openapi.editor.event.DocumentListener -import com.intellij.openapi.fileEditor.FileDocumentManager -import com.intellij.openapi.fileEditor.FileEditor -import com.intellij.openapi.fileEditor.FileEditorPolicy -import com.intellij.openapi.fileEditor.FileEditorProvider +import com.intellij.openapi.fileEditor.* import com.intellij.openapi.fileEditor.ex.StructureViewFileEditorProvider import com.intellij.openapi.fileEditor.impl.JComponentFileEditor import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider @@ -43,7 +44,9 @@ import com.intellij.util.ui.update.MergingUpdateQueue import com.intellij.util.ui.update.Update import com.intellij.util.ui.update.queueTracked import com.intellij.vcsUtil.VcsUtil +import kotlinx.coroutines.Runnable import org.jetbrains.annotations.Nls +import javax.swing.event.HyperlinkEvent internal class DiffPatchFileEditorProvider : FileEditorProvider, StructureViewFileEditorProvider, DumbAware { /** @@ -65,6 +68,8 @@ internal class DiffPatchFileEditorProvider : FileEditorProvider, StructureViewFi if (CombinedDiffRegistry.isEnabled()) { val processor = CombinedDiffManager.getInstance(project).createProcessor() + processor.context.putUserData(DiffUserDataKeysEx.PATCH_FILE_PREVIEW_MODIFICATION_SWITCH, + Runnable { switchToEditableView(project, file) }) processor.setBlocks(buildCombinedDiffModel(document)) val editor = DiffEditorViewerFileEditor(file, processor) @@ -75,8 +80,10 @@ internal class DiffPatchFileEditorProvider : FileEditorProvider, StructureViewFi return editor } else { - val chain = PatchDiffRequestChain(document) - val processor = MutableDiffRequestChainProcessor(project, chain) + val processor = MutableDiffRequestChainProcessor(project, null) + processor.context.putUserData(DiffUserDataKeysEx.PATCH_FILE_PREVIEW_MODIFICATION_SWITCH, + Runnable { switchToEditableView(project, file) }) + processor.chain = PatchDiffRequestChain(document) val editor = DiffEditorViewerFileEditor(file, processor) @@ -100,6 +107,10 @@ internal class DiffPatchFileEditorProvider : FileEditorProvider, StructureViewFi } } +private fun switchToEditableView(project: Project, file: VirtualFile) { + FileEditorManager.getInstance(project).openTextEditor(OpenFileDescriptor(project, file), true) +} + private fun buildCombinedDiffModel(document: Document): List { val producers = createDiffRequestProducers(document) val diffModel = prepareCombinedBlocksFromProducers(producers) @@ -181,3 +192,14 @@ private class ErrorDiffRequestProducer(private val file: VirtualFile, return ErrorDiffRequest(message) } } + +internal fun listenTypingAttempts(diffContext: DiffContext, editor: Editor) { + val onTypingSwitch = diffContext.getUserData(DiffUserDataKeysEx.PATCH_FILE_PREVIEW_MODIFICATION_SWITCH) + if (onTypingSwitch != null) { + EditorModificationUtil.setReadOnlyHint(editor, DiffBundle.message("patch.editing.viewer.hint.enable.editing.text"), { e -> + if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) { + onTypingSwitch.run() + } + }) + } +} diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/PatchDiffViewer.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/PatchDiffViewer.kt index ca402dfee1be..76e06ffede32 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/PatchDiffViewer.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/PatchDiffViewer.kt @@ -75,6 +75,8 @@ internal class PatchDiffViewer( } editorSettingsAction = SetEditorSettingsAction(TextDiffViewerUtil.getTextSettings(diffContext), editors) editorSettingsAction.applyDefaults() + + listenTypingAttempts(diffContext, editor) } override fun getComponent(): JComponent = panel diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/SideBySidePatchDiffViewer.kt b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/SideBySidePatchDiffViewer.kt index 0a579d50cca2..a7593214d7e9 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/SideBySidePatchDiffViewer.kt +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/tool/SideBySidePatchDiffViewer.kt @@ -16,6 +16,7 @@ import com.intellij.diff.tools.simple.AlignableChange import com.intellij.diff.tools.simple.AlignedDiffModel import com.intellij.diff.tools.simple.AlignedDiffModelBase import com.intellij.diff.tools.util.* +import com.intellij.diff.tools.util.BaseSyncScrollable.ScrollHelper import com.intellij.diff.tools.util.FocusTrackerSupport.Twoside import com.intellij.diff.tools.util.SyncScrollSupport.TwosideSyncScrollSupport import com.intellij.diff.tools.util.base.TextDiffSettingsHolder.TextDiffSettings @@ -119,6 +120,9 @@ internal class SideBySidePatchDiffViewer( editorSettingsAction = SetEditorSettingsAction(textSettings, editors) editorSettingsAction.setSyncScrollSupport(syncScrollSupport) editorSettingsAction.applyDefaults() + + listenTypingAttempts(diffContext, editor1) + listenTypingAttempts(diffContext, editor2) } override fun getComponent(): JComponent = panel