Skip to content

Fold unchanged regions in the unified diff - #2791

Open
vogella wants to merge 1 commit into
eclipse-platform:masterfrom
vogella:unified-diff-fold-unchanged
Open

Fold unchanged regions in the unified diff#2791
vogella wants to merge 1 commit into
eclipse-platform:masterfrom
vogella:unified-diff-fold-unchanged

Conversation

@vogella

@vogella vogella commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Folds the unchanged regions of the experimental unified diff overlay, similar to the collapsed context in GitHub's diff view.
The gaps between the displayed changes are collapsed with three context lines kept around each change, which is what makes a large file with few changes reviewable at a glance: AbstractTextEditor.java with four changes fits on one screen instead of 8072 lines.
Each collapsed region carries a clickable band that expands it in place and then reads "Hide n unchanged lines", so a region opened by mistake can be put back without going to the folding ruler.
The editor's projection model is reused and the folds are tagged, so tearing the overlay down leaves the editor's own folds alone; a fold the editor contributes inside a collapsed region is taken out while that region is collapsed, because its indicator would otherwise sit on a foreign line and do nothing when clicked.
A "Fold unchanged regions" preference on the Compare page turns the folding off; it is on by default.

Two known limitations.
Reusing projection support means this has no effect in an editor that does not install it, such as the default text editor.
And in a Java editor, revealing a line whose enclosing element is the whole type, a line in the class javadoc for instance, expands every region inside that type, because JavaEditor.adjustHighlightRange exposes the element's full source range.

Needs eclipse-platform/eclipse.platform.ui#4361, which is merged.

Part of eclipse-platform/eclipse.platform.ui#3771

🤖 Generated with Claude Code

https://claude.ai/code/session_019P5XSUkajCGAiyxqAzgymD

@vogella

vogella commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

This is a draft so that @tobiasmelcher can join in if he wants.

Current state: Folding works but I have no way to unfold.

image

@vogella
vogella requested a review from tobiasmelcher July 2, 2026 07:56
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch from 9ab3ed5 to b50fcaf Compare July 2, 2026 08:03
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Test Results

    54 files  ±  0      54 suites  ±0   56m 12s ⏱️ - 1m 54s
 4 807 tests + 40   4 785 ✅ + 40   22 💤 ±0  0 ❌ ±0 
12 312 runs  +120  12 159 ✅ +120  153 💤 ±0  0 ❌ ±0 

Results for commit 33a8139. ± Comparison against base commit a68c37a.

♻️ This comment has been updated with latest results.

@vogella
vogella force-pushed the unified-diff-fold-unchanged branch 4 times, most recently from 0c90f6e to 909ab3f Compare August 4, 2026 18:43
@vogella
vogella requested a lite review from Copilot August 10, 2026 08:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to UnifiedDiffManager.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.

@tobiasmelcher

Copy link
Copy Markdown
Contributor

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.mp4

I think the issue is related to the folding region starting at a line that is already collapsed by an "Expand ...
unchanged lines" header annotation. In other words, the fold indicator is shown, but the region it would fold is
already partly hidden by the diff context mechanism, so the click has no visible effect.

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?
Alternatively, we could provide a preference so users can opt into the folding feature once it is more polished.

With best regards,
Tobias

@vogella

vogella commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

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.

@vogella
vogella force-pushed the unified-diff-fold-unchanged branch 5 times, most recently from 15e3eca to f5ec0f6 Compare August 14, 2026 13:51
@vogella

vogella commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Still some issues with this one....

@vogella
vogella force-pushed the unified-diff-fold-unchanged branch 2 times, most recently from bbbec75 to 14cd5eb Compare August 15, 2026 05:15
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch 3 times, most recently from 28b7998 to 469d5d4 Compare September 5, 2026 09:55
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch from 469d5d4 to f2e9cf8 Compare September 7, 2026 17:59
@vogella

vogella commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author
Screenshot From 2026-09-07 20-01-06

@tobiasmelcher please re-test.

@vogella
vogella marked this pull request as ready for review September 7, 2026 18:01
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch from f2e9cf8 to 78ef596 Compare September 8, 2026 07:14
vogella added a commit to vogella/eclipse.platform.ui that referenced this pull request Sep 8, 2026
InlinedAnnotationSupport treated every annotation in fInlinedAnnotations as
attached, so once a client removed a code mining annotation from the
annotation model directly, the annotation was reused for every later render
but never put back into the model and its minings stayed invisible.
Annotations the model no longer knows are now added again.

The fallback for annotation models without IAnnotationModelExtension now
removes only the annotations that are actually gone, instead of dropping all
of them and restoring only the newly added ones.

Discovered while working on the unified diff of org.eclipse.compare, see
eclipse-platform/eclipse.platform#2791

Assisted-by: multiple AI agents and layers of automated tooling 🤖
@vogella

vogella commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

vogella added a commit to vogella/eclipse.platform.ui that referenced this pull request Sep 8, 2026
InlinedAnnotationSupport treated every annotation in fInlinedAnnotations as
attached, so once a client removed a code mining annotation from the
annotation model directly, the annotation was reused for every later render
but never put back into the model and its minings stayed invisible.
Annotations the model no longer knows are now added again.

The fallback for annotation models without IAnnotationModelExtension now
removes only the annotations that are actually gone, instead of dropping all
of them and restoring only the newly added ones.

Discovered while working on the unified diff of org.eclipse.compare, see
eclipse-platform/eclipse.platform#2791

Assisted-by: multiple AI agents and layers of automated tooling 🤖
vogella added a commit to vogella/eclipse.platform.ui that referenced this pull request Sep 8, 2026
InlinedAnnotationSupport treated every annotation in fInlinedAnnotations as
attached, so once a client removed a code mining annotation from the
annotation model directly, the annotation was reused for every later render
but never put back into the model and its minings stayed invisible.
Annotations the model no longer knows are now added again.

The fallback for annotation models without IAnnotationModelExtension now
removes only the annotations that are actually gone, instead of dropping all
of them and restoring only the newly added ones.

Discovered while working on the unified diff of org.eclipse.compare, see
eclipse-platform/eclipse.platform#2791

Assisted-by: multiple AI agents and layers of automated tooling 🤖
@tobiasmelcher

Copy link
Copy Markdown
Contributor

Thanks a lot Lars, looks really nice now.

Unfortunately, I found a small issue after clicking the first "Show ... unchanged files" header annotation. The header mining jumps then back to the top of the document which doesn't feel right, it should have been disappeared from my point of view. Here a screencast:

collapse_in_unified_diff.mp4

I tested the changes of this pull request together with eclipse-platform/eclipse.platform.ui#4361 . Was that correct?

vogella added a commit to eclipse-platform/eclipse.platform.ui that referenced this pull request Sep 8, 2026
InlinedAnnotationSupport treated every annotation in fInlinedAnnotations as
attached, so once a client removed a code mining annotation from the
annotation model directly, the annotation was reused for every later render
but never put back into the model and its minings stayed invisible.
Annotations the model no longer knows are now added again.

The fallback for annotation models without IAnnotationModelExtension now
removes only the annotations that are actually gone, instead of dropping all
of them and restoring only the newly added ones.

Discovered while working on the unified diff of org.eclipse.compare, see
eclipse-platform/eclipse.platform#2791

Assisted-by: multiple AI agents and layers of automated tooling 🤖
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch from a44e8c9 to 9b0f8ec Compare September 8, 2026 16:45
@tobiasmelcher

Copy link
Copy Markdown
Contributor

Hi Lars,

I retested the latest changes locally together with eclipse.platform.ui#4361. The folding itself works well, but after expanding a folded region the "Hide 4 unchanged lines" code mining remains visible. I think the header should be an expander only and disappear once its region is expanded; the folding ruler can still be used to collapse it again.

I tested the attached patch locally and it fixes this scenario. It creates unchanged-region code minings only for collapsed folds, removes the obsolete “Hide …” label, and adds a regression test which checks that the attached mining disappears after expansion.

If this matches your intended interaction, could you please apply it with:
git apply fix-unified-diff-expander-mining.patch

fix-unified-diff-expander-mining.patch

Thanks,
Tobias

@vogella

vogella commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @tobiasmelcher I think the "Hide 4 unchanged lines" is a useful feature. Sometimes I want to expand a part to see the x lines above the change and afterwards I want to hide them again.

Here are some screenshots:

shot-01-initial shot-02-top shot-03-expanded shot-04-after-expand-band shot-05-hide-band shot-06-first-region-expanded shot-07-hide241 shot-08-folded-overview

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 "Show n unchanged lines"
code mining that expands it in place, drawn as a band across the editor with
an expander button, so it reads as a break between two hunks rather than as
another line of the file.

The band is a toggle: it shows a collapsed region and hides an expanded one
again, so a region opened by mistake or read to the end can be put back
without going to the folding ruler. Its label and the direction of its
chevrons follow that state, and an expanded region's band sits above the
block it would hide, because there is no gap to sit in.

While it is collapsed the band belongs to the first line after its region. A collapsed fold keeps
its own first line visible as the caption, so a band drawn above that line
sat before a line that is still there while the lines it stands for were
below it. The last line of the document is therefore never folded away, so
that every region has a line after it to be drawn above.

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.

Attached code minings are reused only when they map one to one, by
identity, onto the diffs shown; minings of diffs no longer shown are
dropped. Collapsing a region can keep a mining from being created, and
reusing what was left dropped that diff for good, because nothing
recomputed it from the diffs afterwards. Reopening a diff in the same
editor leaves the minings of the old diffs attached while the new ones are
put, and a count cannot tell the two apart.

Reopening also took the old code mining annotations out of the annotation
model behind the back of the code mining framework, which never puts an
annotation back that it believes to be attached. Next to a provider that
answers asynchronously, as the Java editor's do, the request that would
have dropped them is cancelled by the next one, and the rebuilt overlay
minings landed on annotations that were no longer in the model. The
manager now leaves those annotations to the framework. open() is split into
an editor and a viewer level so that a reopen can be tested on a bare
ProjectionViewer.

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.

A region reaching the end of the document stops above the last line that
carries content. A file ending in a line delimiter has an empty last line,
and an annotation there is never redrawn, because the painter has neither a
character nor a delimiter to repaint; the band of the last region stayed
invisible.

The band keeps its line when the region is toggled. Only two lines of a
region are visible while it is collapsed, its caption and the line after it,
so a band that sits at one end when collapsed and at the other when expanded
moves by the whole height of the region under the pointer that just clicked
it; for the region at the start of the file it landed on the first line of
the document.

Assisted-by: multiple AI agents and layers of automated tooling 🤖
@vogella
vogella force-pushed the unified-diff-fold-unchanged branch from 9b0f8ec to 33a8139 Compare September 9, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants