diff --git a/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF b/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF index 3356acd93a4..571f1346aab 100644 --- a/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF +++ b/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF @@ -70,7 +70,7 @@ Export-Package: org.eclipse.compare; Require-Bundle: org.eclipse.ui;bundle-version="[3.206.0,4.0.0)", org.eclipse.swt;bundle-version="[3.136.0,4.0.0)", org.eclipse.core.resources;bundle-version="[3.4.0,4.0.0)", - org.eclipse.jface.text;bundle-version="[3.8.0,4.0.0)", + org.eclipse.jface.text;bundle-version="[3.32.0,4.0.0)", org.eclipse.ui.ide;bundle-version="[3.3.0,4.0.0)", org.eclipse.ui.views;bundle-version="[3.2.0,4.0.0)", org.eclipse.ui.workbench.texteditor;bundle-version="[3.5.0,4.0.0)", diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java index 7bd961dcaf8..d42b3628d5d 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java @@ -25,6 +25,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.BiConsumer; +import java.util.function.Predicate; import java.util.stream.Stream; import org.eclipse.compare.ICompareFilter; @@ -91,6 +92,7 @@ import org.eclipse.jface.text.source.IAnnotationModelListenerExtension; import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.ISourceViewerExtension5; +import org.eclipse.jface.text.source.ISourceViewerExtension6; import org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation; import org.eclipse.jface.text.source.projection.ProjectionAnnotation; import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; @@ -138,6 +140,9 @@ public class UnifiedDiffManager { + private static final Predicate UNIFIED_DIFF_MINING_FILTER = mining -> mining + .getProvider() instanceof UnifiedDiffCodeMiningProvider; + private static final String CURRENT_SELECTED_UNIFIED_DIFF_ANNO_KEY = "CURRENT_SELECTED_UNIFIED_DIFF_ANNO_KEY"; //$NON-NLS-1$ private static final String UNDO_LISTENER_KEY = "UNIFIED_DIFF_UNDO_LISTENER_KEY"; //$NON-NLS-1$ private static final String DOCUMENT_LISTENER_KEY = "UNIFIED_DIFF_DOCUMENT_LISTENER_KEY"; //$NON-NLS-1$ @@ -527,7 +532,10 @@ private static IStatus open(ITextViewer viewer, IDocument leftDocument, IAnnotat setToolbarActionPresentations(viewer, toolbarActionPresentations); addPaintListener(viewer, model, mode); addMouseMoveListener(viewer, model); - if (viewer instanceof ISourceViewerExtension5 ext) { + if (!unifiedDiffs.isEmpty() && viewer instanceof ISourceViewerExtension6 ext) { + // Adding the filter indirectly updates code minings; no separate updateCodeMinings() call is needed. + ext.addCodeMiningFilter(UNIFIED_DIFF_MINING_FILTER); + } else if (viewer instanceof ISourceViewerExtension5 ext) { ext.updateCodeMinings(); } drawToolBarForAllDiffs(viewer, model, additionalActions, mode, toolbarActionPresentations); @@ -1548,9 +1556,10 @@ private static void clearAll(ITextViewer tv, IAnnotationModel model) { } } diffs1.clear(); - // open unified diff a second time for the same viewer; the old code minings - // need to be removed before adding new ones - if (tv instanceof ISourceViewerExtension5 ext) { + if (tv instanceof ISourceViewerExtension6 ext) { + // Removing the filter indirectly updates code minings; no separate updateCodeMinings() call is needed. + ext.removeCodeMiningFilter(UNIFIED_DIFF_MINING_FILTER); + } else if (tv instanceof ISourceViewerExtension5 ext) { ext.updateCodeMinings(); } } @@ -2398,6 +2407,11 @@ static void disposeUnifiedDiff(ITextViewer tv, IAnnotationModel model, StyledTex diffsByViewer.remove(tv); fileByViewer.remove(tv); + if (tw != null && !tw.isDisposed() && tv.getTextWidget() == tw + && tv instanceof ISourceViewerExtension6 ext) { + // Removing the filter indirectly updates code minings; no separate updateCodeMinings() call is needed. + ext.removeCodeMiningFilter(UNIFIED_DIFF_MINING_FILTER); + } if (tw == null || tw.isDisposed()) { // SWT removes listeners automatically when the widget is disposed diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/UnifiedDiffManagerTest.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/UnifiedDiffManagerTest.java index 948b30d57b2..59c818996af 100644 --- a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/UnifiedDiffManagerTest.java +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/UnifiedDiffManagerTest.java @@ -31,11 +31,13 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; +import java.util.function.Predicate; import org.eclipse.compare.ICompareFilter; import org.eclipse.compare.contentmergeviewer.ITokenComparator; @@ -51,6 +53,7 @@ import org.eclipse.compare.unifieddiff.internal.HideAllDiffsRunnable; import org.eclipse.compare.unifieddiff.internal.KeepAllRunnable; import org.eclipse.compare.unifieddiff.internal.UndoAllRunnable; +import org.eclipse.compare.unifieddiff.internal.UnifiedDiffCodeMiningProvider; import org.eclipse.compare.unifieddiff.internal.UnifiedDiffCodeMiningProvider.UnifiedDiffLineHeaderCodeMining; import org.eclipse.compare.unifieddiff.internal.UnifiedDiffManager; import org.eclipse.core.resources.IFile; @@ -58,6 +61,7 @@ import org.eclipse.core.resources.ResourceAttributes; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.ILogListener; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.preferences.InstanceScope; @@ -70,11 +74,16 @@ import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider; import org.eclipse.jface.text.codemining.ICodeMining; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; +import org.eclipse.jface.text.codemining.LineHeaderCodeMining; import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.jface.text.source.IAnnotationModelExtension; import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.ISourceViewerExtension5; +import org.eclipse.jface.text.source.ISourceViewerExtension6; import org.eclipse.jface.text.source.inlined.LineHeaderAnnotation; import org.eclipse.jface.text.source.projection.ProjectionViewer; import org.eclipse.swt.SWT; @@ -110,6 +119,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; @SuppressWarnings("restriction") @ExtendWith(WorkspaceResetExtension.class) @@ -1098,6 +1109,65 @@ public void testHideAllDiffsCommandDisposesTheUnifiedDiff() throws Exception { assertEquals(0, countAnnotations(annotationModel(), DETAILED_DELETION_ANNO_TYPE)); } + @ParameterizedTest + @ValueSource(strings = { "org.eclipse.compare.hideAllDiffs", "org.eclipse.compare.acceptAllDiffs", + "org.eclipse.compare.hideCurrentDiff", "org.eclipse.compare.acceptCurrentDiff" }) + public void testOtherMiningsAreRestoredAfterResolvingDiffs(String command) throws Exception { + ProblemMiningProvider provider = installProblemMining(); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + waitForProblemMining(provider, false); + assertNotNull(waitForLineHeaderAnnotation(), "unified diff minings must remain visible"); + executeEditorCommand(command); + if (command.contains("accept")) { + waitForDocumentContent(RIGHT); + } + waitForUnifiedDiffCount(0); + waitForProblemMining(provider, true); + assertEquals(0, provider.disposals, "filtering must not dispose installed providers"); + } + + @Test + public void testOtherMiningsAreRestoredAfterEmptyReopenAndModeChanges() throws Exception { + ProblemMiningProvider provider = installProblemMining(); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + waitForProblemMining(provider, false); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_READ_ONLY_MODE).open().isOK()); + waitForProblemMining(provider, false); + assertTrue(UnifiedDiff.create(editor, LEFT, UnifiedDiffMode.OVERLAY_READ_ONLY_MODE).open().isOK()); + waitForProblemMining(provider, true); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + waitForProblemMining(provider, false); + executeEditorCommand("org.eclipse.compare.hideAllDiffs"); + waitForProblemMining(provider, true); + } + + @Test + public void testFilterLifecycleDoesNotRequestDuplicateProviderRefreshes() throws Exception { + ProblemMiningProvider provider = installProblemMining(); + int initialRequests = provider.requests; + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + assertEquals(initialRequests + 1, provider.requests, "open must refresh providers only once"); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + assertEquals(initialRequests + 3, provider.requests, + "reopen must refresh once for clearing the old session and once for the new session"); + executeEditorCommand("org.eclipse.compare.hideAllDiffs"); + waitForProblemMining(provider, true); + } + + @Test + public void testClosingDiffDoesNotRemoveAnotherClientsMiningFilter() throws Exception { + ProblemMiningProvider provider = installProblemMining(); + var extension = (ISourceViewerExtension6) viewer(); + Predicate otherFilter = mining -> mining.getProvider() != provider; + extension.addCodeMiningFilter(otherFilter); + assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.OVERLAY_MODE).open().isOK()); + waitForProblemMining(provider, false); + executeEditorCommand("org.eclipse.compare.hideAllDiffs"); + waitForProblemMining(provider, false); + extension.removeCodeMiningFilter(otherFilter); + waitForProblemMining(provider, true); + } + @Test public void testAcceptAllDiffsCommandAppliesAllChanges() throws Exception { setEditorContent(LEFT); @@ -1460,11 +1530,13 @@ public void testUnifiedDiffEventListenerReportsDocumentUndoAfterTheDocumentChang List events = new ArrayList<>(); List documentContentsAtNotification = new ArrayList<>(); setEditorContent(LEFT); + ProblemMiningProvider provider = installProblemMining(); assertTrue(UnifiedDiff.create(editor, RIGHT, UnifiedDiffMode.REPLACE_MODE).eventListener(event -> { events.add(event); documentContentsAtNotification.add(document().get()); }).open().isOK()); waitForDocumentContent(RIGHT); + waitForProblemMining(provider, false); IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(document()); assertTrue(undoManager.undoable(), "opening a replace-mode diff must create an undoable document change"); @@ -1476,6 +1548,7 @@ public void testUnifiedDiffEventListenerReportsDocumentUndoAfterTheDocumentChang assertEquals(List.of(LEFT), documentContentsAtNotification, "the undo event must be sent after the document has been restored"); assertNull(UnifiedDiffManager.get(viewer()), "undoing the only replace-mode hunk must dispose the unified diff"); + waitForProblemMining(provider, true); } @Test @@ -1785,6 +1858,55 @@ private ITextViewer viewer() { return viewer; } + private ProblemMiningProvider installProblemMining() throws Exception { + ProblemMiningProvider provider = new ProblemMiningProvider(); + ((ISourceViewerExtension5) viewer()).setCodeMiningProviders( + new ICodeMiningProvider[] { new UnifiedDiffCodeMiningProvider(), provider }); + waitForProblemMining(provider, true); + return provider; + } + + private void waitForProblemMining(ProblemMiningProvider provider, boolean visible) throws Exception { + long deadline = System.currentTimeMillis() + 5000; + while (System.currentTimeMillis() < deadline) { + processEvents(); + boolean found = false; + for (Iterator annotations = annotationModel().getAnnotationIterator(); annotations.hasNext();) { + if (annotations.next() instanceof LineHeaderAnnotation header) { + found |= header.getMinings().stream().anyMatch(mining -> mining.getProvider() == provider); + } + } + if (found == visible) { + return; + } + Thread.sleep(20); + } + throw new AssertionError("Problem mining visibility did not become " + visible); + } + + private static final class ProblemMiningProvider extends AbstractCodeMiningProvider { + private int disposals; + private int requests; + + @Override + public CompletableFuture> provideCodeMinings(ITextViewer viewer, + IProgressMonitor monitor) { + requests++; + try { + LineHeaderCodeMining mining = new LineHeaderCodeMining(1, viewer.getDocument(), this) {}; + mining.setLabel("Synthetic problem annotation"); + return CompletableFuture.completedFuture(List.of(mining)); + } catch (BadLocationException e) { + throw new AssertionError(e); + } + } + + @Override + public void dispose() { + disposals++; + } + } + /** * Dispatches a synthetic {@link SWT#MouseMove} event with the given * widget-relative y coordinate to every {@link MouseMoveListener} attached to