Community
Participate
Working Groups
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().
(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.
> 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.
(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.
(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