From cbdc94fae4d6425a0adc09095dccf2e19d1e503f Mon Sep 17 00:00:00 2001 From: lathapatil Date: Fri, 10 Nov 2023 13:58:17 +0530 Subject: [PATCH] Show number of differences in the Compare editor #504 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 https://github.com/eclipse-platform/eclipse.platform/issues/504 --- .../compare/LabelContributionItem.java | 2 +- .../ContentMergeViewer.java | 20 ------- .../contentmergeviewer/TextMergeViewer.java | 31 ++++++++++- .../compare/tests/TextMergeViewerTest.java | 55 +++++++++++++------ 4 files changed, 68 insertions(+), 40 deletions(-) diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java index a3c5f99d3f2..8a2cfc3506b 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java @@ -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); diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java index b7565473dc9..11482457ccc 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java @@ -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; @@ -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(); @@ -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); @@ -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 @@ -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; - } } 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 696b06d0eaf..426a4c7d637 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 @@ -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; @@ -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); @@ -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() { diff --git a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java index a5135938a80..42c1b031eb9 100644 --- a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java +++ b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java @@ -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 { @@ -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(); @@ -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) {