RSyntaxTextArea continuously re-lexing and repainting

Questions on using RSyntaxTextArea should go here.

Moderator: robert

RSyntaxTextArea continuously re-lexing and repainting

Postby plaidflannel » Wed Feb 22, 2012 4:09 am

I'm a newbie to RSyntaxTextArea. I've been conducting some experiments with custom grammars.

I've noticed that whenever my RSTA has focus, it is continuously re-lexing and repainting, even when I am not doing any editing or typing. Is that normal behavior? It seems wasteful to do all that work repeatedly when the text has not changed.

Can I turn that off? I'd like to save the cycles for my worker threads.

Posts: 8
Joined: Wed Feb 22, 2012 4:02 am

Re: RSyntaxTextArea continuously re-lexing and repainting

Postby robert » Wed Feb 22, 2012 2:24 pm

All Swing text components continuously repaint while they have focus due to the Caret blinking. This is something that cannot be avoided.

As for re-lexing, this is actually related to the repainting. RSTA doesn't store any token information about the parsed document (well, it stores the token type of the last token on each line, but no lexical/color information about the document as a whole), and so every time it repaints, it has to do some re-parsing. This isn't as bad as it sounds for a few reasons:

  • As the caret blinks, it only re-parses the line the caret is currently on, not the entire document. Again, this is due to needing to repaint around the caret.
  • As you scroll, it parses each line as it becomes visible, but not again once it's visible on-screen. This is thanks to Swing's double-buffering.
  • RSTA's parsing is super-fast. It involves no memory allocation. It's literally a scan through the chars on a line. More cycles are probably spent in most common usage scenarios, such as the viewToModel() call to place the Caret when clicking on a random location in the editor.

It all comes down to RSTA's trade-off of using less memory to support larger documents. The tradoff of re-parsing seemed justified since the parsing is so cheap. If you're seeing RSTA use any measurable CPU while sitting idle (i.e. > 0% in Task Manager), let me know and perhaps we can look into it.
User avatar
Posts: 852
Joined: Sat May 10, 2008 5:16 pm

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest