Skip to content
Draft
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
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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<IEditorInput, CompareContext> COMPARE_INPUTS = Collections
.synchronizedMap(new WeakHashMap<>());

private static final class CompareContext {
CompareEditorInput compareInput;
WeakReference<Object> 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> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if ((adapterType == IUnifiedDiffInputAdapter.class || adapterType == IComparedWithProvider.class)
&& adaptableObject instanceof IEditorInput) {
return adapterType.cast(ADAPTER);
}
return null;
}

@Override
public Class<?>[] getAdapterList() {
return ADAPTERS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions team/bundles/org.eclipse.compare/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,17 @@
<themeid refid="org.eclipse.e4.ui.css.theme.e4_dark"></themeid>
</stylesheet>
</extension>
<extension
point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="org.eclipse.ui.IEditorInput"
class="org.eclipse.compare.UnifiedDiffInputAdapter">
<adapter
type="org.eclipse.compare.IUnifiedDiffInputAdapter">
</adapter>
<adapter
type="org.eclipse.ui.texteditor.IComparedWithProvider">
</adapter>
</factory>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading