You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@netbeans.apache.org by Tim Boudreau <ni...@gmail.com> on 2019/03/13 15:17:42 UTC

Editor text rewriting

So, I've got some stuff working for Antlr grammars, where you can implement
one method and one annotation, and register a formatter for an Antlr
grammar. That all works.

Getting the editor to behave right when replacing the document text is
another matter.

The formatting API is NetBeans-independent, and uses Antlr's very helpful
TokenStreamRewriter - also meaning you can rewrite any token, not just
whitespace (which the editor.indent API's Context class supports decently).
So I'm really rewriting the entire document.

Getting this to behave sanely (not scroll to the bottom of the document,
maintain caret position) seems to be a near impossibility - and yes, I'm
locking the document, disabling MutableTextInput and acquiring the Reformat
lock.

Full text replacement ALWAYS scrolls to the bottom - this seems to get
handled later on the AWT thread because even grabbing the JTextComponent
and disabling autoscrolling and repaints until the replacement is completed
doesn't work - guessing the layout getting fully invalidated does that.

I can get somewhat sane behavior if I split the replacement into lines and
replace line by line - but why the heck should I have to do that? And doing
this on the AWT thread, which solves some caret and selection madness,
results in one undo event per line - not at all desirable.

The bottom line is, I know the exact character position and scroll position
and token the caret should be at, post-replacement, and am really just
looking for a way to turn off all clever behavior the editor and it's caret
have until the document is fully updated.

Or a mutable version of TokenHierarchy would do it - I could easily
construct and replace tokens as needed, which would be more likely to
preserve relative position information.

For a simple thing, it is absurdly difficult.

Any ideas?

-Tim

-- 
http://timboudreau.com