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 164228 - Abbreviation misplaces cursor when expanding on beginning of document for text files
Summary: Abbreviation misplaces cursor when expanding on beginning of document for tex...
Status: RESOLVED FIXED
Alias: None
Product: editor
Classification: Unclassified
Component: Completion & Templates (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Miloslav Metelka
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-30 16:37 UTC by emi
Modified: 2009-05-07 07:51 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Avoid re-inserting text in the document on reformat if nothing actually changed. (1.36 KB, patch)
2009-04-30 16:38 UTC, emi
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description emi 2009-04-30 16:37:23 UTC
1. Open a text file in the IDE
2. Make sure you have some abbreviation defined for plain text (for example zzz -> expanded${cursor}text).
3. Go at the beginning at the file (the file doesn't need to be empty, just the position zero) and try to expand the
abbreviation (zzz+TAB or whatever your shortcut is).
4. You'll notice that while the text is expanded, the cursor remains on position zero. This isn't actually influenced if
"${cursor}" is present in text, the cursor will be misplaced every time.

The problem seems to be that we have a call similar to CodeTemplateManagerOperation.insert ->
CodeTemplateInsertHandler.processTemplate which adds in textSyncGroup some regions based on document Positions. The
problem with positions is that they get notified when the document changes.

So, later on, when you call formatter.reformat (in CodeTemplateInsertHandler.run) the positions will be notified and
will get reset to zero ! Then in TextRegionEditing.activateTextSync we call component.setCaretPosition with the zero
position.

The bug shows up in all the editor kits that use the old Formatter and the cause seems to be that
org.netbeans.editor.Formatter.reformat just seems to re-insert the same text for no reason. The side-effect of this is
that the Position is notified when the text was removed (and gets reset to zero) but not when it's re-added.

My patch just checks that the Formatter's writer actually changed something and only then re-adds the text.

Note: I'm not 100% sure it's a Position that gets added in TextRegionEditing as I didn't have the patience to go that
deep but I remembered the behavior from an older bug I tracked in the editor. The code in Formatter though does seem to
call remove/insertString for nothing when the text wasn't changed actually (which is bound to trigger some events from
the document for nothing).
Comment 1 emi 2009-04-30 16:38:12 UTC
Created attachment 81336 [details]
Avoid re-inserting text in the document on reformat if nothing actually changed.
Comment 2 Miloslav Metelka 2009-05-05 11:16:45 UTC
The patch looks good to me. I will apply it. A slight modification - I'll put the text into String variable and use Writer.write(String).
Comment 3 Miloslav Metelka 2009-05-05 16:11:51 UTC
Thanks for the patch. It's now applied.
http://hg.netbeans.org/jet-main/rev/46b4e97fc5ba
Comment 4 Quality Engineering 2009-05-07 07:51:10 UTC
Integrated into 'main-golden', will be available in build *200905070201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/46b4e97fc5ba
User: Miloslav Metelka <mmetelka@netbeans.org>
Log: #164228 - Abbreviation misplaces cursor when expanding on beginning of document for text files.