Fold unchanged regions in the unified diff - #2791
Conversation
|
This is a draft so that @tobiasmelcher can join in if he wants. Current state: Folding works but I have no way to unfold.
|
9ab3ed5 to
b50fcaf
Compare
0c90f6e to
909ab3f
Compare
There was a problem hiding this comment.
Pull request overview
This pull request adds folding of unchanged regions to the experimental unified diff overlay in the Eclipse Compare UI, making large files with sparse changes more compact by collapsing unchanged gaps while preserving configurable context around each change.
Changes:
- Introduces a new
UnifiedDiff.Builder#foldUnchanged(int contextLines)option and wires it through toUnifiedDiffManager.open(...). - Implements unchanged-region fold computation and lifecycle management using the editor projection (folding) model, including teardown of added fold annotations.
- Adds “Expand n unchanged lines” code minings for collapsed fold regions and introduces a dedicated test suite for fold-region selection logic.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/UnifiedDiffFoldRegionsTest.java | New unit tests for computing which unchanged lines should be folded given diffs + context. |
| team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/AllTeamUITests.java | Registers the new fold-region test in the Team UI test suite. |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/UnifiedDiff.java | Adds the foldUnchanged(int) builder option and passes it to the manager. |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java | Computes unchanged fold regions, adds/removes tagged projection annotations, and updates code minings when folds change. |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffCodeMiningProvider.java | Adds fold-expander code minings based on collapsed fold annotations. |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareUIPlugin.java | Enables unchanged-region folding with a default of 3 context lines when opening unified diff. |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareMessages.properties | Adds NLS message keys for the fold-expander label(s). |
| team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareMessages.java | Adds corresponding NLS fields for the new expander messages. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /** | ||
| * Collapses unchanged regions between diffs, keeping the given number of | ||
| * context lines (at least one) around each change. A negative value disables | ||
| * folding. | ||
| */ |
|
Hi Lars, thanks for adding code folding to the unified diff view! I tried it out and ran into a confusing interaction. When I click on line 6 and line 46, the fold indicator appears to be clickable, but no folding actually happens. The lines remain fully expanded after the click. I recorded a short screencast showing the behavior: code_folding_unified_diff.mp4I think the issue is related to the folding region starting at a line that is already collapsed by an "Expand ... In general, I like the feature. Clicking the "Expand ... unchanged lines" header annotation works perfectly fine. What are your plans for this? Should we merge the feature as-is, or would it make sense to first fix this interaction? With best regards, |
|
Hey Tobias thanks for testing and the feedback. I also think it will be quite useful especially in case of big files with only small changes. I left it in draft because I also found issues, for example if I increase the text size only parts of the unified display changes. I would like to leave it for now in draft, I testing at the moment the functionality and will return to this pr in a few days. During development I realized that compare was too slow and focused on this before working on new functionality. For example a 10 000 line file is or at least was too slow to be seen in unified diff. |
15e3eca to
f5ec0f6
Compare
|
Still some issues with this one.... |
f5ec0f6 to
bbbec75
Compare
Add a foldUnchanged(contextLines) option to UnifiedDiff that collapses the unchanged gaps between changes, keeping a few context lines around each change, similar to the unified view on GitHub. It reuses the editor's projection (folding) model, so it is a no-op when folding is disabled, and its folds are tagged so they can be removed without touching the editor's own folds. Each collapsed region shows a clickable "Expand n unchanged lines" code mining that expands it in place, drawn as a band across the editor so it reads as a break between two hunks. A "Fold unchanged regions" preference turns the folding on and off. It is on by default and shares a group with the existing unified diff preference on the Compare page. Only a complete set of code minings is reused. Collapsing a region can keep a mining from being created, and reusing what is left dropped that diff for good, because nothing recomputed it from the diffs afterwards. A line taken out in one place and put back further down lost the overlay of its old place that way. Folds of the editor whose first line a collapsed region hides are taken out of the projection model while that region is collapsed and put back when it is expanded again. The folding ruler paints such a fold on the first line of it that is still visible but toggles it by its start line, so its indicator would sit on a foreign line and clicking it would do nothing.
bbbec75 to
14cd5eb
Compare

This adds folding of unchanged regions to the experimental unified diff overlay, similar to the collapsed unchanged sections in GitHub's diff view.
A new
UnifiedDiff.Builder#foldUnchanged(contextLines)option collapses the unchanged gaps between the displayed changes, keeping a few context lines visible around each change; it is enabled with three context lines where the unified diff is opened.Each collapsed region shows a clickable "Expand n unchanged lines" code mining that expands it in place; re-collapsing via the folding ruler brings the expander back.
The implementation reuses the editor's projection (folding) model and the folds are tagged with a marker annotation so they can be removed on overlay teardown (Hide All Diffs, switching to the 2-way editor, dismissing the last diff) without touching the editor's own structural folds.
This makes reviewing large files with few changes considerably more compact.
Still a draft for one reason: reusing the projection model means this only takes effect in editors that install projection support, such as the Java editor or the Generic Editor.
AbstractDecoratedTextEditorandTextEditordo not, so for a plain text file opened with the default text editor the option is silently a no-op. That needs a decision before this lands: accept it as a limitation, or fall back to something that does not depend on projection.Part of eclipse-platform/eclipse.platform.ui#3771