diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/IUnifiedDiffInputAdapter.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/IUnifiedDiffInputAdapter.java new file mode 100644 index 00000000000..125368429bf --- /dev/null +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/IUnifiedDiffInputAdapter.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2026 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare; + +import org.eclipse.ui.IEditorInput; + +public interface IUnifiedDiffInputAdapter { + + void setCompareInput(IEditorInput inp, CompareEditorInput input); + +} diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/UnifiedDiffInputAdapter.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/UnifiedDiffInputAdapter.java new file mode 100644 index 00000000000..b22cc2c3d92 --- /dev/null +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/UnifiedDiffInputAdapter.java @@ -0,0 +1,171 @@ +/******************************************************************************* + * Copyright (c) 2026 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare; + +import java.text.DateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.Map; +import java.lang.ref.WeakReference; +import java.util.WeakHashMap; + +import org.eclipse.compare.structuremergeviewer.ICompareInput; +import org.eclipse.compare.structuremergeviewer.SharedDocumentAdapterWrapper; +import org.eclipse.core.runtime.IAdapterFactory; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.texteditor.IComparedWithProvider; + +public class UnifiedDiffInputAdapter implements IAdapterFactory { + + private static final Class[] ADAPTERS = { IUnifiedDiffInputAdapter.class, IComparedWithProvider.class }; + private static final Map COMPARE_INPUTS = Collections + .synchronizedMap(new WeakHashMap<>()); + + private static final class CompareContext { + CompareEditorInput compareInput; + WeakReference ownerEditor= new WeakReference<>(null); + } + + public static final class UnifiedDiffInputDataAdapter implements IUnifiedDiffInputAdapter, IComparedWithProvider { + @Override + public void setCompareInput(IEditorInput inp, CompareEditorInput input) { + if (inp == null) { + return; + } + if (input == null) { + COMPARE_INPUTS.remove(inp); + return; + } + CompareContext context= COMPARE_INPUTS.computeIfAbsent(inp, __ -> new CompareContext()); + context.compareInput= input; + // A new compare operation re-opens claiming for the next editor instance. + context.ownerEditor= new WeakReference<>(null); + } + + @Override + @SuppressWarnings("nls") + public String getComparedWithLabel(IEditorInput inp, Object editorKey) { + CompareContext context= COMPARE_INPUTS.get(inp); + if (context == null || !claimOrMatch(context, editorKey)) { + return null; + } + CompareEditorInput compareInput = context.compareInput; + if (compareInput == null || !(compareInput.getCompareResult() instanceof ICompareInput input)) { + return null; + } + ITypedElement left = input.getLeft(); + ITypedElement right = input.getRight(); + IEditorInput leftInput = documentKeyOf(input.getLeft()); + IEditorInput rightInput = documentKeyOf(input.getRight()); + if (inp.equals(leftInput)) { + return comparedWithLabel(left, right); + } + if (inp.equals(rightInput)) { + return comparedWithLabel(right, left); + } + return compareInput.getTitle(); + } + + @Override + public void clearComparedWithLabel(IEditorInput inp, Object editorKey) { + if (inp == null || editorKey == null) { + return; + } + CompareContext context= COMPARE_INPUTS.get(inp); + if (context == null) { + return; + } + synchronized (context) { + Object owner= context.ownerEditor.get(); + if (owner == editorKey) { + COMPARE_INPUTS.remove(inp); + } + } + } + + private static boolean claimOrMatch(CompareContext context, Object editorKey) { + if (editorKey == null) { + return true; + } + synchronized (context) { + Object owner = context.ownerEditor.get(); + if (owner == null || owner == editorKey) { + context.ownerEditor= new WeakReference<>(editorKey); + return true; + } + return false; + } + } + + private static IEditorInput documentKeyOf(ITypedElement element) { + if (element == null) { + return null; + } + ISharedDocumentAdapter adapter = SharedDocumentAdapterWrapper.getAdapter(element); + return adapter == null ? null : adapter.getDocumentKey(element); + } + + private static String labelOf(ITypedElement element) { + if (element == null) { + return null; + } + String name = element.getName(); + return name == null || name.isBlank() ? null : name; + } + + private static String comparedWithLabel(ITypedElement current, ITypedElement opposite) { + String oppositeName = labelOf(opposite); + String oppositeDate = modificationDateOf(opposite); + if (oppositeDate == null) { + return oppositeName; + } + String currentName = labelOf(current); + if (currentName != null && currentName.equals(oppositeName)) { + // Same file name on both sides: the timestamp is the useful distinction. + return oppositeDate; + } + return oppositeName == null ? oppositeDate : oppositeName + " (" + oppositeDate + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + private static String modificationDateOf(ITypedElement element) { + + if (!(element instanceof IModificationDate dateProvider)) { + return null; + } + + long timestamp = dateProvider.getModificationDate(); + if (timestamp <= 0) { + return null; + } + return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(new Date(timestamp)); + } + } + + private static final IUnifiedDiffInputAdapter ADAPTER = new UnifiedDiffInputDataAdapter(); + + @Override + public T getAdapter(Object adaptableObject, Class adapterType) { + if ((adapterType == IUnifiedDiffInputAdapter.class || adapterType == IComparedWithProvider.class) + && adaptableObject instanceof IEditorInput) { + return adapterType.cast(ADAPTER); + } + return null; + } + + @Override + public Class[] getAdapterList() { + return ADAPTERS; + } + +} 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 17a10d9bb3f..9f5dbe6f7c3 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 @@ -54,6 +54,7 @@ import org.eclipse.compare.IStreamContentAccessor; import org.eclipse.compare.IStreamMerger; import org.eclipse.compare.ITypedElement; +import org.eclipse.compare.IUnifiedDiffInputAdapter; import org.eclipse.compare.internal.core.CompareSettings; import org.eclipse.compare.internal.merge.DocumentMerger.IDocumentMergerInput; import org.eclipse.compare.structuremergeviewer.ICompareInput; @@ -718,14 +719,22 @@ private boolean openUnifiedDiff(UnifiedDiffSource source, CompareEditorInput inp // no active workbench page; fall back to the classic compare editor path return false; } + + IEditorInput editorInput = source.editorInput(); + IUnifiedDiffInputAdapter diffAdapter = editorInput.getAdapter(IUnifiedDiffInputAdapter.class); + if (diffAdapter != null) { + diffAdapter.setCompareInput(editorInput, input); + } + boolean keepCompareContext = false; + // Only an editor opened here may be closed again when the diff cannot be // applied after all; one the user already had open stays untouched. - IEditorPart editorBefore = wpage.findEditor(source.editorInput()); + IEditorPart editorBefore = wpage.findEditor(editorInput); IEditorPart openedHere = null; try { IDocumentMergerInput mergerInput = findDocumentMergerInput(input, source.compareInput()); - IEditorPart editorPart = wpage.openEditor(source.editorInput(), - getEditorId(source.editorInput(), source.element())); + IEditorPart editorPart = wpage.openEditor(editorInput, + getEditorId(editorInput, source.element())); openedHere = editorPart == editorBefore ? null : editorPart; if (editorPart instanceof MultiPageEditorPart mpe && mpe.getSelectedPage() instanceof IEditorPart selected) { editorPart = selected; @@ -736,7 +745,7 @@ private boolean openUnifiedDiff(UnifiedDiffSource source, CompareEditorInput inp } // A side that is not a workspace file, the version shown for a deleted file // for example, is only worth overlaying when its content really loaded. - if (!(source.editorInput() instanceof IFileEditorInput) && !hasContent(textEditor)) { + if (!(editorInput instanceof IFileEditorInput) && !hasContent(textEditor)) { closeIfOpenedHere(wpage, openedHere); return false; } @@ -757,10 +766,15 @@ private boolean openUnifiedDiff(UnifiedDiffSource source, CompareEditorInput inp // The user canceled the diff, not the open: leave the text editor alone // instead of falling back to the classic compare editor. if (status.isOK() || status == UnifiedDiffManager.CANCELED_BY_USER) { + keepCompareContext = true; return true; } } catch (PartInitException e) { CompareUIPlugin.log(e); + } finally { + if (!keepCompareContext && diffAdapter != null) { + diffAdapter.setCompareInput(editorInput, null); + } } // The classic compare editor takes over, so the editor opened for the unified // diff would only be a second editor on the same file without a comparison. diff --git a/team/bundles/org.eclipse.compare/plugin.xml b/team/bundles/org.eclipse.compare/plugin.xml index 80117a0dd12..27f3b2a0476 100644 --- a/team/bundles/org.eclipse.compare/plugin.xml +++ b/team/bundles/org.eclipse.compare/plugin.xml @@ -695,4 +695,17 @@ + + + + + + + + diff --git a/team/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/history/FileRevisionTypedElement.java b/team/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/history/FileRevisionTypedElement.java index dc775c0ee5b..af0e9583709 100644 --- a/team/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/history/FileRevisionTypedElement.java +++ b/team/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/history/FileRevisionTypedElement.java @@ -17,6 +17,7 @@ import java.text.DateFormat; import java.util.Date; +import org.eclipse.compare.IModificationDate; import org.eclipse.compare.ITypedElement; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.Assert; @@ -30,7 +31,7 @@ * An {@link ITypedElement} wrapper for {@link IFileRevision} for use with the * Compare framework. */ -public class FileRevisionTypedElement extends StorageTypedElement { +public class FileRevisionTypedElement extends StorageTypedElement implements IModificationDate { private final IFileRevision fileRevision; private String author; @@ -83,6 +84,11 @@ public String getTimestamp() { return DateFormat.getDateTimeInstance().format(dateFromLong); } + @Override + public long getModificationDate() { + return fileRevision.getTimestamp(); + } + /** * Return the file revision of this element. * @return the file revision of this element