Best way to force scrolling

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Best way to force scrolling

Postby Vicne » Sun Oct 20, 2013 8:15 am

Hi,

We're successfully using RSyntaxTextArea 2.0.1 in our project, and I have a small question about making the area scroll automatically.
When "stepping" through code in debug mode, the "current" line must always stay in the viewport. To achieve this, I first just used the caret position, like so:

Code: Select all
area.setCaretPosition(area.getLineStartOffset(lineNumber));


This results in having the window scrolled so that the line with the caret is always visible. Good.

Unfortunately, we just noticed that doing so causes the full code to be reparsed for syntax highlighting. This, for long contents, takes a considerable amount of time.
Is there a better way to force the area to scroll to a given line programmatically ? Or is that issue solved in v2.5.0 ?

Thanks and keep on the good work !

Best regards,

Vicne
Vicne
 
Posts: 9
Joined: Tue Feb 28, 2012 1:13 pm

Re: Best way to force scrolling

Postby robert » Tue Oct 22, 2013 1:30 pm

The editor's entire contents should not be re-parsed unless it's the first rendering of the component or you do something to line 1 that causes the entire document structure to change (such as add the start a multi-line comment that doesn't close).

RSTA will "parse" each line that comes into view as you scroll/page down, but even for page down events it should be very fast. It will only reparse the page once, and the parsing involves no heap allocations whatsoever. I can hold Page Down, and scroll through a Java or XML file many 10's of thousands of lines long, and not see a single lag.

Versions of RSTA newer than yours do have improved parsing logic, and don't re-parse lines as frequently. I'd suggest you update just for that. But it won't fix your problem - if the entire editor is being re-parsed, something else must be going on.

Can you send a use case that shows this behavior?
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: Best way to force scrolling

Postby Guest » Thu Oct 24, 2013 7:51 pm

Hi, Robert,

Thanks for your quick reply.

It's not easy to reproduce the problem in a simple case in fact. It happens when "stepping" through code as I said so there are interactions between execution, repaint() and .
One of the developers profiled the problem using VisualVM and explained me he "measured time from call setCaretPosition(getLineStartOffset(line)) until change of window content and paint a highlighted line. May be showing a highlight takes so much. I am not sure. But anyway in problem case profiling in VisualVM shows more than 100000 calls of addToken().".
He solved the issue by replacing the above call to setCaretPosition() by the following method to bring the given line in the visible area:
Code: Select all
Rectangle visibleRect = area.getVisibleRect();

int lineHeight = area.getFontMetrics(area.getFont()).getHeight();
       
int firstLine = (int) Math.ceil((double) visibleRect.y / lineHeight);
int lastLine = (int) Math.floor((double) (visibleRect.y + visibleRect.height) / lineHeight);

// do nothing if inside visible area
if (firstLine<=line && line<lastLine)
    return;
// scroll to position where line gets visible
visibleRect.y = lineHeight * (line>0 ? line-1 : 0);
area.scrollRectToVisible(visibleRect);


So in any case, our problem is solved and I think we can close this thread.

Thanks very much again for your help again.

Best regards,

Vicne
Guest
 

Re: Best way to force scrolling

Postby Vicne » Thu Oct 24, 2013 7:53 pm

(oops, I posted the previous message as guest, sorry. Feel free to delete it)

Hi, Robert,

Thanks for your quick reply.

It's not easy to reproduce the problem in a simple case in fact. It happens when "stepping" through code as I said so there are interactions between execution, repaint() and .
One of the developers profiled the problem using VisualVM and explained me he "measured time from call setCaretPosition(getLineStartOffset(line)) until change of window content and paint a highlighted line. May be showing a highlight takes so much. I am not sure. But anyway in problem case profiling in VisualVM shows more than 100000 calls of addToken().".
He solved the issue by replacing the above call to setCaretPosition() by the following method to bring the given line in the visible area:
Code: Select all
Rectangle visibleRect = area.getVisibleRect();

int lineHeight = area.getFontMetrics(area.getFont()).getHeight();
       
int firstLine = (int) Math.ceil((double) visibleRect.y / lineHeight);
int lastLine = (int) Math.floor((double) (visibleRect.y + visibleRect.height) / lineHeight);

// do nothing if inside visible area
if (firstLine<=line && line<lastLine)
    return;
// scroll to position where line gets visible
visibleRect.y = lineHeight * (line>0 ? line-1 : 0);
area.scrollRectToVisible(visibleRect);


So in any case, our problem is solved and I think we can close this thread.

Thanks very much again for your help again.

Best regards,

Vicne
Vicne
 
Posts: 9
Joined: Tue Feb 28, 2012 1:13 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron