From cb3da13a6cbaf7ff75f280352fb4b2f46cb0a382 Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Fri, 14 Aug 2026 13:40:34 +0200 Subject: [PATCH] Offer the switch to the unified diff only when it is enabled The compare editor showed the button that switches over to the unified diff for every comparison the unified diff can display, no matter whether the preference that enables the unified diff was set. The unified diff is still experimental, so the button now follows that preference and stays hidden while it is off. The check sits in canOfferUnifiedDiffSwitch next to canShowAsUnifiedDiff, which keeps answering from the structure of the comparison alone so that switching still works once the button was offered. A test covers both states of the preference for the same qualifying comparison. --- .../contentmergeviewer/TextMergeViewer.java | 5 ++-- .../compare/internal/CompareUIPlugin.java | 14 ++++++++++ .../compare/tests/UnifiedDiffOpenTest.java | 28 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java index 1339979b966..c35fe338dba 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java @@ -4204,11 +4204,12 @@ public void run() { /** * Adds the action that switches this side by side comparison over to the * unified diff, the counterpart of the unified diff's action to open the - * comparison. Only added when the input can be shown as a unified diff at all. + * comparison. Only added when the unified diff is enabled in the preferences + * and the input can be shown as one at all. */ private void createShowUnifiedDiffItem(ToolBarManager tbm) { if (!(getCompareConfiguration().getContainer() instanceof CompareEditorInput input) - || !CompareUIPlugin.canShowAsUnifiedDiff(input)) { + || !CompareUIPlugin.canOfferUnifiedDiffSwitch(input)) { return; } Action showUnifiedDiff = new Action() { diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java index f3df9ab744a..ae956668c4a 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java @@ -897,6 +897,20 @@ public static boolean canShowAsUnifiedDiff(CompareEditorInput input) { return unifiedDiffCandidateOf(input) != null; } + /** + * Returns whether the classic compare editor may offer a switch to the unified + * diff for the given input. The unified diff is still experimental, so the + * switch stays hidden until the preference that enables it is set. + */ + public static boolean canOfferUnifiedDiffSwitch(CompareEditorInput input) { + if (input == null) { + return false; + } + CompareConfiguration configuration = input.getCompareConfiguration(); + IPreferenceStore store = configuration == null ? null : configuration.getPreferenceStore(); + return store != null && store.getBoolean(ComparePreferencePage.UNIFIED_DIFF) && canShowAsUnifiedDiff(input); + } + /** * Picks the side the unified diff sits on, without reading any content. * Returns null if neither side qualifies. diff --git a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UnifiedDiffOpenTest.java b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UnifiedDiffOpenTest.java index 95329468bd1..7d46ab510f7 100644 --- a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UnifiedDiffOpenTest.java +++ b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/UnifiedDiffOpenTest.java @@ -57,6 +57,7 @@ import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.jface.action.Action; import org.eclipse.jface.preference.IPreferenceStore; @@ -596,6 +597,33 @@ public void testOnlyAQualifyingInputCanBeShownAsUnifiedDiff() throws Exception { "an input without a workspace file must not offer the unified diff"); //$NON-NLS-1$ } + /** + * The unified diff is experimental, so the switch is only offered while the + * preference that enables it is set, and then only for a comparison the unified + * diff can display. + */ + @Test + public void testTheSwitchIsOnlyOfferedWhileTheUnifiedDiffIsEnabled() throws Exception { + RecordingCompareEditorInput input = openClassicInput(); + assertTrue(CompareUIPlugin.canShowAsUnifiedDiff(input), + "a workspace file comparison must qualify for the unified diff"); //$NON-NLS-1$ + assertFalse(CompareUIPlugin.canOfferUnifiedDiffSwitch(input), + "a disabled unified diff must not be offered"); //$NON-NLS-1$ + + store().setValue(ComparePreferencePage.UNIFIED_DIFF, true); + assertTrue(CompareUIPlugin.canOfferUnifiedDiffSwitch(input), + "an enabled unified diff must be offered for a qualifying comparison"); //$NON-NLS-1$ + + // Prepared without an editor: opening one would leave the fallback to the + // classic editor pending past the end of this test. + RecordingCompareEditorInput inMemory = new RecordingCompareEditorInput( + new InMemoryElement("left.txt", "alpha\nbravo\n"), //$NON-NLS-1$ //$NON-NLS-2$ + new InMemoryElement("right.txt", "alpha\nBRAVO\n")); //$NON-NLS-1$ //$NON-NLS-2$ + inMemory.run(new NullProgressMonitor()); + assertFalse(CompareUIPlugin.canOfferUnifiedDiffSwitch(inMemory), + "an enabled unified diff must not be offered for a comparison it cannot display"); //$NON-NLS-1$ + } + /** * Switching a side by side comparison over to the unified diff must leave the * unified diff behind, not both editors.