Skip to content

Commit

Permalink
Show number of differences in the Compare editor #504
Browse files Browse the repository at this point in the history
Fixed Issues identified during review :

1. The label is not recomputed if the diff config changes
	(Ignore White Space).
2. The label is incorrect for zero changes - it says 0 Difference

3. Test case failure

Fixes #504
  • Loading branch information
lathapatil committed Nov 10, 2023
1 parent c611ac8 commit cbdc94f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected Control createControl(Composite parent) {

GridLayout compositeLayout = new GridLayout(1, false);
compositeLayout.marginTop = -3;
compositeLayout.marginBottom = -3;
compositeLayout.marginBottom = -6;
composite.setLayout(compositeLayout);
GridData labelLayout = new GridData(SWT.LEFT, SWT.BOTTOM, true, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.compare.ICompareContainer;
import org.eclipse.compare.ICompareInputLabelProvider;
import org.eclipse.compare.IPropertyChangeNotifier;
import org.eclipse.compare.LabelContributionItem;
import org.eclipse.compare.internal.ChangePropertyAction;
import org.eclipse.compare.internal.CompareEditor;
import org.eclipse.compare.internal.CompareHandlerService;
Expand Down Expand Up @@ -366,7 +365,6 @@ private void resize(MouseEvent e) {
private Cursor fHSashCursor;
private Cursor fVSashCursor;
private Cursor fHVSashCursor;
private int documentDiffCount;

private final ILabelProviderListener labelChangeListener = event -> {
Object[] elements = event.getElements();
Expand Down Expand Up @@ -781,7 +779,6 @@ public void refresh() {
}

private void internalRefresh(Object input) {
final String DIFF_COUNT_ID = "DiffCount"; //$NON-NLS-1$
IMergeViewerContentProvider content= getMergeContentProvider();
if (content != null) {
Object ancestor= content.getAncestorContent(input);
Expand All @@ -808,15 +805,6 @@ private void internalRefresh(Object input) {
if (Utilities.okToUse(fComposite) && Utilities.okToUse(fComposite.getParent())) {
ToolBarManager tbm = (ToolBarManager) getToolBarManager(fComposite.getParent());
if (tbm != null) {
String label = documentDiffCount > 1 ? " Differences" : " Difference"; //$NON-NLS-1$ //$NON-NLS-2$
LabelContributionItem labelContributionItem = new LabelContributionItem(DIFF_COUNT_ID,
documentDiffCount + label);

if (tbm.find(DIFF_COUNT_ID) != null) {
tbm.replaceItem(DIFF_COUNT_ID, labelContributionItem);
} else {
tbm.appendToGroup("diffLabel", labelContributionItem); //$NON-NLS-1$
}
updateToolItems();
Display.getDefault().asyncExec(() -> {
// relayout in next tick
Expand Down Expand Up @@ -1465,12 +1453,4 @@ protected boolean isLeftEditable() {
protected boolean isRightEditable() {
return fCompareConfiguration.isMirrored() ? fCompareConfiguration.isLeftEditable() : fCompareConfiguration.isRightEditable();
}

/**
* @param docDiffCount - current number of differences in the compare editor
* @since 3.10
*/
protected void setDocumentDiffCount(int docDiffCount) {
documentDiffCount = docDiffCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.LabelContributionItem;
import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
import org.eclipse.compare.internal.ChangePropertyAction;
Expand Down Expand Up @@ -3060,9 +3061,7 @@ protected void updateContent(Object ancestor, Object left, Object right) {
setSyncScrolling(fPreferenceStore.getBoolean(ComparePreferencePage.SYNCHRONIZE_SCROLLING));

update(false);
if (fMerger!=null) {
setDocumentDiffCount(fMerger.changesCount());
}

if (!fHasErrors && !emptyInput && !fComposite.isDisposed()) {
if (isRefreshing()) {
fLeftContributor.updateSelection(fLeft, !fSynchronizedScrolling);
Expand Down Expand Up @@ -5414,6 +5413,32 @@ private boolean isIgnoreAncestor() {

updateVScrollBar();
updatePresentation();
updateToolbarLabel();
}

private void updateToolbarLabel() {
final String DIFF_COUNT_ID = "DiffCount"; //$NON-NLS-1$
ToolBarManager tbm =
(ToolBarManager) getToolBarManager(fComposite.getParent());
int differenceCount = fMerger.changesCount();
if (tbm != null) {

String label = differenceCount > 1 ? differenceCount + " Differences" //$NON-NLS-1$
: differenceCount == 1 ? differenceCount + " Difference" : "No Difference"; //$NON-NLS-1$ //$NON-NLS-2$
LabelContributionItem labelContributionItem = new LabelContributionItem(DIFF_COUNT_ID,
label);

if (tbm.find(DIFF_COUNT_ID) != null) {
tbm.replaceItem(DIFF_COUNT_ID, labelContributionItem);
} else {
tbm.appendToGroup("diffLabel", labelContributionItem); //$NON-NLS-1$
}
Display.getDefault().asyncExec(() -> {
// relayout in next tick
tbm.update(true);
tbm.getControl().getParent().setRedraw(true);
});
}
}

private void resetDiffs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,48 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.compare.*;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareViewerPane;
import org.eclipse.compare.ICompareFilter;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.LabelContributionItem;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.compare.internal.*;
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
import org.eclipse.compare.internal.IMergeViewerTestAdapter;
import org.eclipse.compare.internal.MergeViewerContentProvider;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import java.net.URL;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.junit.Test;
import org.eclipse.swt.SWT;

public class TextMergeViewerTest {

Expand Down Expand Up @@ -498,13 +518,11 @@ public void testToolbarLabelContribution() throws Exception {
DiffNode parentNode = new DiffNode(new ParentTestElement(), new ParentTestElement());
DiffNode testNode = new DiffNode(parentNode, Differencer.CHANGE, null, new EditableTestElement(url.openStream().readAllBytes()), new EditableTestElement(url1.openStream().readAllBytes()));

runInDialogWithToolbarDiffLabel(testNode, () -> {
//Not required
});
runInDialogWithToolbarDiffLabel(testNode);
}

CompareViewerPane fCompareViewerPane;
private void runInDialogWithToolbarDiffLabel(DiffNode testNode, Runnable runnable) throws Exception {
private void runInDialogWithToolbarDiffLabel(DiffNode testNode) throws Exception {

CompareConfiguration compareConfig = new CompareConfiguration();
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Expand All @@ -524,19 +542,24 @@ protected Control createDialogArea(Composite parent) {
fCompareViewerPane.setContent(viewer.getControl());
ToolBarManager toolbarManager = CompareViewerPane.getToolBarManager(fCompareViewerPane);

processQueuedEvents();

IContributionItem contributionItem = toolbarManager.find("DiffCount");
assertNotNull(contributionItem);
LabelContributionItem labelContributionItem=(LabelContributionItem) contributionItem;
assertTrue(labelContributionItem.getToolbarLabel().getText().equals("7 Differences"));
try {
runnable.run();
} catch (WrappedException e) {
e.throwException();
}

dialog.close();
viewer = null;
}

private void processQueuedEvents() {
while (Display.getCurrent().readAndDispatch()) {
// Process all the events in the queue
}

}

private void runInDialogWithPartioner(Object input, Runnable runnable, final CompareConfiguration cc) throws Exception {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Dialog dialog = new Dialog(shell) {
Expand Down

0 comments on commit cbdc94f

Please sign in to comment.