SQL syntax highlighting optimization

G

Guest

Sorry I posted this post earlier but got the name and subject the wrong way round (new to this!) so it perhaps didn't look right
Sorry for that. But please please answer if you know anything about it..
Thanks
I've got a procedure that is responsible for performing the following operations on a richtextbox
1) determining what parts of the text to forma
2) formatting specific ranges of text a specific color

I need to do this whenever the contents of the richtextbox change. The algorithm is quite fast as I have built in some speed saving optimizations, but i need to do some more, I was wondering if anybody could give me any suggestions? So far, I've coded it to
* - retrieve information about all ranges 'as of now' but only setting the format of ones that are new / only unsetting the format of ones that were there but aren't no
* - only set the format of ranges that are visible. For this, I use the message EM_GETFIRSTVISIBLELINE to get the first visible line, and the line count by dividing the height by the height of a line (got by GetTextMetrics). But then I need to do a rescan of ranges to format on vscroll aswell as on textchange, which is a bit bad as it's slow when you scroll

I was thinking of having some sort of 'lazy updater' type thing that either runs for, say, 100 milliseconds each second, or in a different thread, to update the RTB for the ranges that are off the visible range once all the others have been done, but I can't get my head round how to handle the synchronicity with the fact that when the richtextbox's contents change, the ranges will change too. Should I consider this a thing that takes 'a long time' to happen? Or can anyone else lend a hand with the obviously huge amount of sideways thinking I'm having to do!!

It's for a syntax highlighting control, right now I'm just trying to get it to be able to parse c-style block comments, line comments and quotes (red.

Any ideas on performance in this area
Have posted to C++ groups as although I am writing the app in c# I am non too averse to turn my hand to c++

Thank
 
A

Arne Janning

Johnny said:
Sorry I posted this post earlier but got the name and subject the wrong way round (new to this!) so it perhaps didn't look right.
Sorry for that. But please please answer if you know anything about it...
Thanks!
I've got a procedure that is responsible for performing the following operations on a richtextbox:
1) determining what parts of the text to format
2) formatting specific ranges of text a specific color.

I need to do this whenever the contents of the richtextbox change. The algorithm is quite fast as I have built in some speed saving optimizations, but i need to do some more, I was wondering if anybody could give me any suggestions? So far, I've coded it to:
* - retrieve information about all ranges 'as of now' but only setting the format of ones that are new / only unsetting the format of ones that were there but aren't now
* - only set the format of ranges that are visible. For this, I use the message EM_GETFIRSTVISIBLELINE to get the first visible line, and the line count by dividing the height by the height of a line (got by GetTextMetrics). But then I need to do a rescan of ranges to format on vscroll aswell as on textchange, which is a bit bad as it's slow when you scroll.

I was thinking of having some sort of 'lazy updater' type thing that either runs for, say, 100 milliseconds each second, or in a different thread, to update the RTB for the ranges that are off the visible range once all the others have been done, but I can't get my head round how to handle the synchronicity with the fact that when the richtextbox's contents change, the ranges will change too. Should I consider this a thing that takes 'a long time' to happen? Or can anyone else lend a hand with the obviously huge amount of sideways thinking I'm having to do!!!

It's for a syntax highlighting control, right now I'm just trying to get it to be able to parse c-style block comments, line comments and quotes (red.)

Any ideas on performance in this area?
Have posted to C++ groups as although I am writing the app in c# I am non too averse to turn my hand to c++.

Hi Johnny,

wouldn't it be easier and cheaper to take a syntax editor control?

http://www.actiprosoftware.com/Products/DotNet/SyntaxEditor/

Cheers

Arne Janning
 
D

Dan Dan

I tried 15 ways to get this to work and the results I produced were
never favoriable. In the end I came up with something that was just too
damn slow to be useful.

I guess you're highlighting line by line judging that you want the
lines. My version worked by using GetCharIndexFromPosition, and putting
the top left corner of my control to get the highest visible text, and
the bottom left + line length to get the lowest visible text. I found
this to be useful. When you scrolled, key downed, or changed text, it
would highlight the visible portion. It kept track of the highest and
lowest point not highlighted, so that when you scrolled over the same
area again it wouldn't highlight it. Otherwise when the TextChanged
event fired it would change all of the text that was changed plus
everything that was on the same line as the start and end of the text.
I used the wndproc events from scrolling, and painting in my code. I
also had a second richtextbox as a buffer which made things a tiny bit
quicker. It also used regular expressions to parse everything - I don't
know if this was smart or not, but the regular expression were compiled,
and I didn't need to use any different logic for comments and quotes.

I found that using threading never quite worked the way I hoped for -
and I have never seen anybody used threading in a RichTextBox syntax
highlighter and have it work well. If you are going to continue to try
a threaded method, then I suggest you remember to use Delegates for all
the functions that access the control in your threaded method.
Otherwise you'll guy will blow up. When I tried using threads I was
able to get around a lot of problems by keeping track of what line I was
on [actually the indices of the start and end char]. My thread would
never work on that line, and just split any work before and after that
line. On the line I was working on the TextChanged Event would deal
with that.

Here's an idea. Scrap what you're doing. I've wrapped my noggin around
this guy for a while. I think my mistake was inheriting from
RichTextBox. Think about it. It has so much functiality we don't want!
What's to stop somebody from pasting an image. You can rig and rig but
you're just going to produce something that is sloppy. TextBox does
everything we want except colorize text. Well in theory it seems the
answer there is just to override the Onpaint event. No worrying about
scrolling, threading or any other crap like in our RTB methods, plus no
worrying about somebody changing the Font by pasting from Word, or
pasting an image.

Also don't be cracking nobody's work. Itain't right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top