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.
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).
Created attachment 81336 [details] Avoid re-inserting text in the document on reformat if nothing actually changed.
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).
Thanks for the patch. It's now applied. http://hg.netbeans.org/jet-main/rev/46b4e97fc5ba
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.