Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>null</code> if neither side qualifies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Loading