Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352847

Summary: [jface] TextViewer.setVisibleRegion begins at offset line's start
Product: [Eclipse Project] Platform Reporter: Patrick Konemann <patrick.koenemann>
Component: TextAssignee: Platform-Text-Inbox <platform-text-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: Andreas.Muelder, daniel_megert, nyssen, remy.suen
Version: 3.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Patrick Konemann CLA 2011-07-22 07:25:01 EDT
When setting the visible region for a text viewer via TextViewer.setVisibleRegion(start, length), parameter 'start' is ignored.

The cause is apparently ProjectionViewer.updateSlaveDocument in which the offset is calculated with respect to multi-line documents.
Instead of *adding* the line offset to the specified offset, the code *replaces* the specified offset with the line offset:

if (!isProjectionMode()) {
	// mimic original TextViewer behavior
	IDocument master= projection.getMasterDocument();
	int line= master.getLineOfOffset(modelRangeOffset);
	offset= master.getLineOffset(line);
	length= (modelRangeOffset - offset) + modelRangeLength;
}

Changing the offset assignment to the following works for us:

	offset+= master.getLineOffset(line);
Comment 1 Remy Suen CLA 2011-07-22 07:56:40 EDT
Text owns this code.
Comment 2 Dani Megert CLA 2011-07-25 03:43:33 EDT
This looks like a bug on your end.

    int line= master.getLineOfOffset(modelRangeOffset);
This gets the line which contains the start offset

    offset= master.getLineOffset(line);
This computes the start of the line, which means this offset <= start offset and hence includes the start offset. The length is adjusted accordingly.


The start offset is honored but the it is adjusted to start at the beginning of the line and same for the end offset. This is as specified by ProjectViewer: it only supports full line projections.
Comment 3 Patrick Konemann CLA 2011-07-25 06:12:59 EDT
We are confused, according to the JavaDoc in ITextViewer.setVisibleRegion():

 * Defines and sets the region of this viewer's document which will be
 * visible in the presentation. Every character inside the specified region
 * is supposed to be visible in the viewer's widget after that call.

This does not say anything about lines.
And since updateSlaveDocument is directly called in TextViewer.setVisibleRegion, we would expect the offset to be honored per character and not only per line. Otherwise this function would not make any sense in single-line texts.

Another point is that setVisibleRegion works for cutting the end of a line (it does not keep the entire line).
Comment 4 Dani Megert CLA 2011-07-25 06:15:29 EDT
But you are using a ProjectViewer which refines the contract. If you use a TextViewer then it should work.
Comment 5 Dani Megert CLA 2011-07-25 06:15:47 EDT
>But you are using a ProjectViewer
ProjectionViewer
Comment 6 Dani Megert CLA 2011-07-25 06:32:37 EDT
(In reply to comment #4)
> But you are using a ProjectViewer which refines the contract. If you use a
> TextViewer then it should work.
Mmh - looking at the old implementation in TextViewer.updateVisibleDocument(...) I think also TextViewer doesn't work as you'd expect. We always take the full first line and this is not against the contract which only says that the characters in the given range are supposed to be visible. It doesn't say anything about cutting text at the beginning of the first line. Cutting at the beginning can be a problem for existing code which computes things relative to the line start.

We won't change that behavior. If you really need this you can either implement your own viewer or we turn this bug into a feature request for adding new API that behaves the way you want it.
Comment 7 Patrick Konemann CLA 2011-07-25 10:49:06 EDT
Ok, thank you for the clarifications. We'll then change this default behaviour in a subclass.