Skip to content

Conversation

@danthe1st
Copy link
Contributor

@danthe1st danthe1st commented Oct 21, 2025

In #3074, I prevented collapsing and expanding projection regions that overlap with parts of the file outside of the current visible region.

However, this does not prevent the projection annotation from being shown in the editor. This PR changes that behavior and ensures these annotations (that cannot be collapsed or expanded) cannot be shown.

To prevent showing empty "files" in the editor which cannot be expanded, I also changed the code for setting the visible region to expand any surrounding or "bordering" projection regions.

manual Testing

This can be tested by setting up an editor with content similar to the following:

visible_region_start

projection_start

visible_region_end

projection_end

If the visible region is configured from visible_region_start to visible_region_end and a projection annotation is created between projection_start and projection_end (in both cases, these texts are inclusive), there is an annotation at projection_start which cannot be collapsed.

If I figure out how to test whether an annotation is drawn, I can provide a proper test case.

Here is a test case for manual inspection:

	@Test
	public void testProjectionRegionsShownOnlyInVisibleRegion() throws BadLocationException, InterruptedException {
		Shell shell= new Shell(Display.getCurrent());
		shell.setLayout(new FillLayout());
		TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL);
                // This should hide the folding annotation
		String documentContent= """

				visible_region_start

				projection_start

				visible_region_end

				projection_end

				""";

                // This should show it
//		String documentContent= """
//
//				visible_region_start
//
//				projection_start
//
//				projection_end
//
//				visible_region_end
//
//
//				""";
		Document document= new Document(documentContent);
		viewer.setDocument(document, new AnnotationModel());
		int visibleRegionStart = documentContent.indexOf("visible_region_start");
		int visibleRegionEnd = documentContent.indexOf("\n", documentContent.indexOf("visible_region_end"))+1;

		int projectionStart= documentContent.indexOf("projection_start");
		int projectionEnd= documentContent.indexOf("\n", documentContent.indexOf("projection_end")) + 1;

		viewer.setVisibleRegion(visibleRegionStart, visibleRegionEnd - visibleRegionStart);
		viewer.enableProjection();
		ProjectionAnnotation annotation= new ProjectionAnnotation();
		viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(projectionStart, projectionEnd - projectionStart));
		shell.setVisible(true);
		Canvas canvas= new Canvas(shell, SWT.ALL);
		try {
			canvas.addPaintListener(new PaintListener() {

				@Override
				public void paintControl(PaintEvent e) {
					annotation.paint(e.gc, canvas, new Rectangle(e.x, e.y, e.width, e.height));
				}
			});

			Display display= shell.getDisplay();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} finally {
			canvas.dispose();
			shell.dispose();
		}
	}

This is what it should look like:
image

With the other version (or without this change), there is a projection annotation shown on the right:
image

I also added automated tests for it.

JDT example (for context)

More or less eclipse-jdt/eclipse.jdt.ui#2302 (comment), this can be seen with JDT using eclipse-jdt/eclipse.jdt.ui#2302 by enabling Window > Preferences > Java > Editor > Only show the selected Java Element as well as Window > Preferences > Java > Editor > Folding > Enable folding of custom regions

Open a Java file with the following code (adapted from eclipse-jdt/eclipse.jdt.ui#2302 (comment)):

public class SomeClass {
    // region

    void a() {

    }

    void b() {

    }

    // endregion
}

When selecting a() in the editor, it only shows

    // region

    void a() {

    }

Without this PR, the // region has a folding annotation on the right which cannot be used. With this PR, this region is used. However, it is still present when the whole class is shown.

@danthe1st danthe1st marked this pull request as ready for review October 21, 2025 17:33
@github-actions
Copy link
Contributor

github-actions bot commented Oct 21, 2025

Test Results

 3 018 files  ± 0   3 018 suites  ±0   2h 29m 48s ⏱️ + 6m 10s
 8 233 tests + 4   7 984 ✅ + 5  249 💤 ±0  0 ❌  - 1 
23 619 runs  +12  22 825 ✅ +13  794 💤 ±0  0 ❌  - 1 

Results for commit c7d5ff6. ± Comparison against base commit a988d08.

♻️ This comment has been updated with latest results.

@iloveeclipse
Copy link
Member

In general works as advertised, thanks. If you could figure out how to add test for that, it would be great.

@danthe1st danthe1st force-pushed the hide-uncollapsible-regions branch 3 times, most recently from b2d270d to f1879fb Compare October 23, 2025 15:45
@jjohnstn
Copy link
Contributor

@danthe1st There is a problem. If one collapses a region around an element (e.g. method) and then selects the method, it shows the collapsed region comment but no annotation. If one then selects the class, the region comment has a plus, though it is already expanded and the weird text at end of folded lines is all the place. Clicking on the plus resets everything back to where it is supposed to be.

public class TestFolding {

	// region

	void test() {

	}
	void test2() {

	}

	void test2a() {

	}
	void test3() {}

	// endregion

	// region2

	void test4() {

	}

	// endregion2
}

@danthe1st
Copy link
Contributor Author

danthe1st commented Oct 23, 2025

If one collapses a region around an element (e.g. method) and then selects the method, it shows the collapsed region comment but no annotation.

I see. It might be a possibility to automatically expand regions like this in such a situation (I guess selecting a visible region that is practically collapsed doesn't make much sense).

@danthe1st danthe1st force-pushed the hide-uncollapsible-regions branch 2 times, most recently from 2bf7aa8 to 67a1490 Compare October 23, 2025 18:37
@danthe1st
Copy link
Contributor Author

danthe1st commented Oct 23, 2025

@jjohnstn I attempted to provide a fix for it by automatically expanding regions that border the current folding region. However, I didn't get to test it properly yet (I plan to do that tomorrow).

I think this should be the best behavior for anything running into that behavior (not just JDT).

@danthe1st danthe1st force-pushed the hide-uncollapsible-regions branch from 67a1490 to 9afcd76 Compare October 24, 2025 18:06
Projection regions that overlap with parts of the file outside of the
current visible region cannot be collapsed hence their annotations
should not be painted.
@danthe1st danthe1st force-pushed the hide-uncollapsible-regions branch from 9afcd76 to c7d5ff6 Compare October 24, 2025 18:07
@danthe1st
Copy link
Contributor Author

danthe1st commented Oct 24, 2025

I improved and tested my fix to the problem mentioned by @jjohnstn and I adjusted this PR (both the code/tests and the PR description).

While testing, I think I found another issue but I think that's unrelated to this PR and should probably be fixed on its own.

@iloveeclipse iloveeclipse merged commit ce17c9e into eclipse-platform:master Oct 24, 2025
18 checks passed
@iloveeclipse
Copy link
Member

Thanks.

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