Skip to content

Allow external visible regions in ProjectionViewer #3073

Open
@danthe1st

Description

@danthe1st

Suggestion

The ProjectionViewer allows adding folding regions (projections) but it also allows setting the visible regions using the setVisibleRegion() method. However, when setVisibleRegion is called, projections are disabled automatically, presumably because ProjectionViewer needs to hide parts of the document when folding regions are collapsed.

To see what happens when visible regions are used together with folding, consider the following test (added as an additional method to ProjectionViewerTest):

	@Test
	public void testProjectionWithVisibleRegion() {
		Shell shell= new Shell();
		shell.setLayout(new FillLayout());
		ProjectionViewer viewer= new ProjectionViewer(shell, null, null, false, SWT.NONE);
		String documentContent= """
				Hello
				World
				123
				456
				""";
		Document document= new Document(documentContent);
		viewer.setDocument(document, new AnnotationModel());
		int regionLength= documentContent.indexOf('\n');
		viewer.setVisibleRegion(0, regionLength);
		viewer.enableProjection();
		viewer.getProjectionAnnotationModel().addAnnotation(new ProjectionAnnotation(false), new ProjectionPosition(document));
		shell.setVisible(true);
		// viewer.getTextOperationTarget().doOperation(ProjectionViewer.COLLAPSE_ALL); // uncommenting this leads to a different test failure
		viewer.getTextOperationTarget().doOperation(ITextOperationTarget.SELECT_ALL);
		try {
			assertEquals(0, viewer.getVisibleRegion().getOffset());//If collapsed: expected 0 but was 6
			assertEquals(regionLength, viewer.getVisibleRegion().getLength());//expected 5 but was 20
		} finally {
			shell.dispose();
		}
	}

This test enables folding (enableProjection()) after calling setVisibleRegion() (this being allowed may be a bug) and shows what happens when using both projections and visible regions together. (The Javadoc of ProjectionViewer does not seem to suggest that folding with a custom visible region is not supported so I guess this could also be seen as a bug.)

I suggest that the public setVisibleRegion is changed in a way such that:

  • It hides everything before and after the passed region
  • Folding regions can be collapsed as it is possible without setVisibleRegion.

It might be possible to internally implement this by adding an additional collapsed region from the document to the start of the given visible region and one from the end of the visible region to the end of the visible document.

This is motivated by eclipse-jdt/eclipse.jdt.ui#2264 as JDT uses both folding and also has a "Only show selected Java element" that calls setVisibleRegion to ensure only the selected element is shown. If this feature is enabled, folding is generally disabled if "Only show the selected element" is enabled.

To test it, the (store == null || !store.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS)) check could be removed from https://github.com/eclipse-jdt/eclipse.jdt.ui/blob/fc9808007c93d80d8e56dc421175f528296d418a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java#L1878. After that, folding should (in theory) work with "Only show the selected Java element" (and selecting different parts of while the document is open).

Community

  • I understand suggesting an enhancement doesn't mandate anyone to implement it. Other contributors may consider this suggestion, or not, at their own convenience. The most efficient way to get it fixed is that I implement it myself and contribute it back as a good quality patch to the project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions