This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 136215 - Caret row highlighting broken for just emptied document (CompoundHighlightsContainer caching issue)
Summary: Caret row highlighting broken for just emptied document (CompoundHighlightsCo...
Status: RESOLVED FIXED
Alias: None
Product: editor
Classification: Unclassified
Component: Painting & Printing (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Vitezslav Stejskal
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-05-31 14:27 UTC by emi
Modified: 2009-11-05 10:15 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
The caret row highlight bug on 6.1 / OSX (11.81 KB, image/png)
2008-05-31 14:28 UTC, emi
Details
Patched OffsetsBag, note the new offset2raw(int) and raw2Offset(int) methods (30.79 KB, text/plain)
2008-05-31 14:32 UTC, emi
Details
Patched CompoundHighlightsContainer which stores the rawOffset for cacheLowestPos / cacheHighestPos (17.40 KB, text/plain)
2008-05-31 14:34 UTC, emi
Details

Note You need to log in before you can comment on or make changes to this bug.
Description emi 2008-05-31 14:27:43 UTC
I've noticed a problem with the editor highlighting for the caret row. In the following case, the highlighting doesn't
cover the whole line, but only the end of line, no matter how much text is added:

1. write some text, on one or multiple lines
2. select all text (ctrl+a)
3. delete the text (delete)
4. write some chars (without newline)
5. you'll notice that the chars don't have the caret-row background and that the caret row is visible, but starting from
the end of the text (till the end of line / end of the UI).

See attached screenshot made on OSX, I've seen it on Windows too.

After lots of debugging I've reduced the issue to the CompoundHighlightsContainer caching engine. If I disable caching
altogether the problem doesn't show up anymore, with a big penalty hit as the highlight layers are always queried.

The problem on the caching engine are actually the cacheLowestPos and cacheHighestPos variables because they are not
actually in sync with the cache (OffsetsBag). The OffsetsBug, ie. cache, adds a listener on the document for
insert/remove which notifies cache.marks.default{Insert|Remove}Update which in turn changes the mark's offset. *But*
this does not notify in any way the cache{Lowest|Highest}Pos vars which are bound to get out of sync.

So, on caret rows case, the marks become something like [0 - row color, 1 -null] with cacheLowestPos=0
cacheHighestPos=MAXINT. On each new insert marks.defaultInsertUpdate changes the marks to [document.length - row color,
document.length +1 - null] basically always moving the caret row highlights away from the new chars.

As cache{Lowest|Highest}Pos vars never change, the cache isn't refreshed and
CompoundHighlightsContainer.getHighlights(int,int) always return the broken interval (starting with document.length).

My fix involves using the rawOffset for the cache{Lowest|Highest}Pos vars and converting to normal document offset
during getHighlights(int,int). This way it seems that the OffsetsBag doesn't get out of sync. Of course, we also need
access to OffsetsBag.mark.{raw2Offset|offset2raw} methods. I'll also attach my change classes. These are by no means a
proper patch as I introduce new public methods and that's probably bad. Maybe you'll find another solution, this is what
I'm using right now.

Screnshot made on:
Product Version: NetBeans IDE 6.1 (Build 200804211638)
Java: 1.5.0_13; Java HotSpot(TM) Client VM 1.5.0_13-119
System: Mac OS X version 10.5.3 running on i386; MacRoman; en_US (nb)

Problem also visible on
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13-120)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_05-b13-52, mixed mode)
Comment 1 emi 2008-05-31 14:28:30 UTC
Created attachment 62206 [details]
The caret row highlight bug on 6.1 / OSX
Comment 2 emi 2008-05-31 14:32:08 UTC
Created attachment 62207 [details]
Patched OffsetsBag, note the new offset2raw(int) and raw2Offset(int) methods
Comment 3 emi 2008-05-31 14:34:54 UTC
Created attachment 62208 [details]
Patched CompoundHighlightsContainer which stores the rawOffset for cacheLowestPos / cacheHighestPos
Comment 4 Vitezslav Stejskal 2008-06-06 11:25:58 UTC
Wow, Emilian, thanks for tracking this down. I can imagine it took some long hours. I'll have a closer look on the
patches and find a way of doing this without an API change. Thanks again
Comment 5 emi 2008-06-28 15:26:30 UTC
Please note that performance-wise my changes aren't that good. Not sure why but it looks like I get more paint requests
which make especially scrolling a pain on large documents (and this is not the other antialias bug).

So, I've disabled my patch on my codebase, and I'm awaiting your optimized one :-) The broken CaretRowHighlighting is
less of a pain than the slow scrolling.
Comment 6 emi 2008-08-19 13:03:51 UTC
Just a quick note: the bug happens because of the different strategy related zero-locking with Position and Mark on
documents. CaretBasedBlockHighlighting uses Position internally which is zero-locked while OffsetsBag uses
OffsetGapList<Mark> which loses the zero-lock after the scenario I've listed (ctrl+A and then type some text). The Mark
gets a zero value but that's not absolute anymore (unlike the Position which remains zero).

Please note I'm writing this comment just to store it somewhere. I found this behavior long ago and I don't want the
explanation I found to get lost. My solution was to make CaretBasedBlockHighlighting.updateLineInfo a bit more
aggressive with HighlightsChange events when on the first line (this is the only situation the bug can show up).
Comment 7 Miloslav Metelka 2008-11-24 11:28:09 UTC
Reproduced - we should fix this in 7.0.
Comment 8 Vitezslav Stejskal 2009-11-03 18:31:36 UTC
I fixed CaretBasedBlockHighlighting to fire events when the block contains position 0. I guess that's the same fix as
you've done. http://hg.netbeans.org/jet-main/rev/cddac5cc5c51

As for the CompoundHighlightsContainer, I think I understand the problem. The zero-locked positions are evil. I'll try
to fix it somehow as well.
Comment 9 Vitezslav Stejskal 2009-11-04 16:26:40 UTC
An attempted fix in CompoundHighlightsContainer - http://hg.netbeans.org/jet-main/rev/374fbf53db62
Comment 10 Quality Engineering 2009-11-05 10:15:00 UTC
Integrated into 'main-golden', will be available in build *200911050201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/cddac5cc5c51
User: Vita Stejskal <vstejskal@netbeans.org>
Log: #136215: repaint caret row highlight when adding text at the beginning of a document