Page 1 of 1
Syntax Highlighting routine help needed
Posted: Fri Jan 30, 2009 11:40 am
by Garrett
I've been working on this off and on for about a year or so now, and I'm just not getting what I want from it. It does work, it highlights html tags, but it's slow. I've totally hit brain blockage on this and can't think of any new way to do this or speed it up.
Can someone please take a look at what I've got so far and maybe give me some ideas or help on making my code work faster or better...
Currently, it highlights the entire html page when you select and load one, but! It can lock up Rev on some bigger files... Still not sure what's happening there yet. It also can take a very long time loading the file since I'm formatting it at load up also.

I'd thought about trying to only highlight what's in view in the editor window, but my attempt at that before lead to a slow scrolling issue.
Highlighting as you type seems to work fine though, if anything, I believe I did something right with that!
If anyone has the time and maybe some ideas/suggestions/help, feel free to download what I have and check it over.
http://www.paraboliclogic.com/syntaxhighlight.rev
One of these days, I'd really like to finish this... But between A.D.D. and brain farts, I tell ya!
Thanks in advance,
~Garrett
Posted: Fri Jan 30, 2009 11:47 am
by Mark
Hi Garrett,
Can you post a link to a file that makes Rev lock up? I tried it with my own homepage and that seems to work fine --quite fast actually, on my 2.16Ghz MacBook.
Best,
Mark
Posted: Fri Jan 30, 2009 8:59 pm
by Garrett
Sure, I'll upload like a bookmarks file from firefox which did it.
http://www.paraboliclogic.com/bookmarks.html
as well, my index page on my site did it too
http://www.paraboliclogic.com/index.html
Thanks,
~Garrett
Posted: Sat Jan 31, 2009 9:37 pm
by Mark
Hi Garrett,
You loop at least 2 times through the entire file, character by character. You can make your code much faster by using offset instead of a repeat loop that checks each and every single character.
Best,
Mark
Posted: Sun Feb 01, 2009 11:54 am
by Garrett
Offset eh... Ok, I'll hit the docs and check that out. Thanks a bunch.

Posted: Thu Feb 05, 2009 9:31 pm
by Garrett
Well that sure cut a lot of code out that I had, but now I'm having another problem
Code: Select all
put 0 into varCharIndex
put 0 into varTempTimer
repeat until offset("<",field "fHtmlEditor",varCharIndex) is 0 or varTempTimer > 100
put varTempTimer + 1 into varTempTimer
put offset("<",field "fHtmlEditor",varCharIndex) into varStart
put offset(">",field "fHtmlEditor",varCharIndex) into varEnd
put char varStart to varEnd of field "fHtmlEditor" into varTemp
put varCharIndex into field "temptestfield"
put empty into varTagType
put varStart + 3 into varTemp
put char varStart to varTemp of field "fHtmlEditor" into varTemp
if varTemp is "<!--" then
put "comment" into varTagType
end if
put varStart + 6 into varTemp
put char varStart to varTemp of field "fHtmlEditor" into varTemp
if varTemp is "<script" then
put "script" into varTagType
end if
put varStart + 7 into varTemp
put char varStart to varTemp of field "fHtmlEditor" into varTemp
if varTemp is "</script" then
put "script" into varTagType
end if
set the textstyle of char varStart to varEnd of field "fHtmlEditor" to plain
if varTagType is "comment" then
set the textstyle of char varStart to varEnd of field "fHtmlEditor" to Italic
set the foregroundColor of char varStart to varEnd of field "fHtmlEditor" to "#404040"
else if varTagType is "script" then
set the foregroundColor of char varStart to varEnd of field "fHtmlEditor" to "#ff0000"
else
--set the textstyle of char varStart to varEnd of field "fHtmlEditor" to Bold
set the foregroundColor of char varStart to varEnd of field "fHtmlEditor" to "#0000ff"
end if
put varStart + 1 into varCharIndex
end repeat
For some reason it only finds the first instance of what I'm looking for, even though I'm setting the chars to skip using the position plus 1 of the last found instance. And it gets stuck in an endless loop, so I put a loop counter in there to stop it at 100 until I can find out what the heck i did wrong.
Is the offset() not good for working directly on the edit element as I have in my code? Should I pass the data in the edit element into a variable and use the variable in my above code, then set the chunks in my edit from the results from using the variable?
Or... did I just simply do something silly or miss something up there that I'm gonna slap my forehead for?
Thanks,
~Garrett
Posted: Thu Feb 05, 2009 10:21 pm
by Garrett
I'm still racking my brain on this
This is what I've tried since the above
Code: Select all
put 0 into varStartIndex
put 0 into varEndIndex
put 0 into varTempTimer
put field "fHtmlEditor" into varData
repeat until offset(numToChar(60),varData,varStartIndex) is 0 or varTempTimer > 100
put varTempTimer + 1 into varTempTimer
put offset(numToChar(60),varData,varStartIndex) into varStart
put offset(numToChar(62),varData,varEndIndex) into varEnd
put varStart && varEnd && varStartIndex into field "temptestfield"
set the textstyle of char varStart to varEnd of field "fHtmlEditor" to plain
set the foregroundColor of char varStart to varEnd of field "fHtmlEditor" to "#0000ff"
put varStart + 1 into varStartIndex
put varEnd + 1 into varEndIndex
end repeat
answer "Done checking for syntax highlighting"
And still the same result. It only hits the first instance. Is the itemoffset affected by spaces? Meaning it only checks the string until it hits a space?
Thanks for any help or info,
~Garrett
Posted: Thu Feb 05, 2009 10:35 pm
by Garrett
Well, I can't see anything wrong with the code. I can't think of anything else to try using offset(), so I'm a gonna have to go back to looping through the whole edit element char for char as i was before since offset() isn't doing what I thought it was suppose to do

Posted: Thu Feb 05, 2009 10:46 pm
by Mark
Hi Garrett,
An offset function like
offset(myString,myData,myIndex)
always returns the position of the found char relative to char myIndex. To find the actual position of the next character, you need to add myIndex (sometimes plus or minus 1, depending on your exact approach) to the value returned by the offset function.
Best,
Mark
Posted: Thu Feb 05, 2009 10:59 pm
by Garrett
OMG! LOL, you're kidding.. So if I did offset("a","abcabc",2) it would return 2 instead of 4?
Posted: Thu Feb 05, 2009 11:15 pm
by Garrett
See, I just knew I was going to feel like a moron when someone pointed out what was up with what I had... Doh!!
Mark, thanks very much, it works as it should and faster than what I had originally.
Now I can put some code back into that for the actual syntax highlighting, and this time, less bad code too. I saw some of the mistakes I had made in my original code while trying to figure out how to use the offset().
Again, many thanks!
~Garrett