Bug 487280 - DefaultDocumentAdapter.getLineCount() is incompatible with StyledTextContent's API contracts
Summary: DefaultDocumentAdapter.getLineCount() is incompatible with StyledTextContent'...
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.6 M7   Edit
Assignee: Dani Megert CLA
QA Contact:
URL:
Whiteboard:
Keywords: Documentation
Depends on:
Blocks:
 
Reported: 2016-02-04 14:13 EST by Stefan Xenos CLA
Modified: 2016-04-27 10:17 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Xenos CLA 2016-02-04 14:13:55 EST
The JavaDoc on StyledTextContent requires that getLineCount() never returns a value  smaller than 1 and many callers rely upon this fact, including the StyledText widget itself.

However, the implementation of DefaultDocumentAdapter delegates directly to IDocument.getNumberOfLines() whose JavaDoc has no such restriction on 0.

This seems to be the most likely explanation for why StyledText has been triggering exceptions by returning negative values from methods such as getTopLine().
Comment 1 Dani Megert CLA 2016-02-05 04:51:04 EST
(In reply to Stefan Xenos from comment #0)
> The JavaDoc on StyledTextContent requires that getLineCount() never returns
> a value  smaller than 1 and many callers rely upon this fact, including the
> StyledText widget itself.
> 
> However, the implementation of DefaultDocumentAdapter delegates directly to
> IDocument.getNumberOfLines() whose JavaDoc has no such restriction on 0.

It is clearly the expectation and intention that 1 is returned for the first line even though not explicitly mentioned in the Javadoc. We can fix this.

ILineTracker.getNumberOfLines(): Note that a document always has at least one line.


> This seems to be the most likely explanation for why StyledText has been
> triggering exceptions by returning negative values from methods such as
> getTopLine().

Can you provide a case where it indeed does return a value below 0?  All AbstractDocument(s) use the line tracker which returns values >= 1, or at least should, otherwise it would be an implementation bug.
Comment 2 Stefan Xenos CLA 2016-02-05 22:20:53 EST
> Can you provide a case where it indeed does return a value below 0? 

That comment was referring specifically to StyledText.getTopLine(), not IDocument.getNumberOfLines().

We know that StyledText.getTopLine() is returning negative values since we've seen the consequences in a bunch of bug reports. If you search for all references, you'll see that most of them clamp the value to 0 as a workaround -- so there's probably a lot of dupes I don't know about. One representative one would be bug 471192.

I don't know for sure that IDocument.getNumberOfLines() is returning 0... but if it is, that would be a plausible explanation for why StyledText.getTopLine() is returning negative values.
Comment 3 Dani Megert CLA 2016-02-08 03:18:26 EST
(In reply to Stefan Xenos from comment #2)
> I don't know for sure that IDocument.getNumberOfLines() is returning 0...
> but if it is, that would be a plausible explanation for why
> StyledText.getTopLine() is returning negative values.

You can see in the tracker that this isn't the case.
Comment 4 Dani Megert CLA 2016-03-18 06:55:07 EDT
(In reply to Dani Megert from comment #1)
> (In reply to Stefan Xenos from comment #0)
> It is clearly the expectation and intention that 1 is returned for the first
> line even though not explicitly mentioned in the Javadoc. We can fix this.

Fixed with http://git.eclipse.org/c/platform/eclipse.platform.text.git/commit/?id=25a4b22643b1a8e30a72350fd9cda69d7095ce1f