Syntax Highlighting classes with example

BlitzMax Forums/BlitzMax Programming/Syntax Highlighting classes with example

ozak(Posted 2006) [#1]
Yup. After some quick hacking I got an example running very well. Nice clean written classes and example. No optimizations done yet mind you (except where obvious).

I'm thinking about adding it to the sample code section later after you guys have checked it out. (And myself)

A lot of people talked about the speed of the syntax highlighter, and I haven't really tested it with large files.
However, I only update the current line on each keystroke which seems plenty fast on my systems :)

First the example code. Show's how easy it is to use.
Save it as whatever you want. It's your main file.


script.txt


SyntaxHighlighter.bmx


LineTokenizer.bmx



Mark Tiffany(Posted 2006) [#2]
I haven't checked this out, but I'm interested in this from the POV of the Community IDE. It looks like it would be failry easy to have different syntax highlighters for different types of code (e.g. in the IDE have BMX, C++, HTML , etc code highlighted differently in each tab).

The only thing is that the highlighter at the moment does a lot more than just tokenise. E.g. Highlighting comments, string literals, numeric literals differently. It also does (or should do in the same pass) identify types, functions, etc for populatingt he code structure lists. (and equally could be used for self-highlighting). Another thing I just spotted is that the only separator looks to be " ", you'll need a few more...

What do you reckon to taking this up a level and seeing if we can use it in the Community IDE? I'll take a look at the code and performance over the weekend to see how it might compare...


taxlerendiosk(Posted 2006) [#3]
Thank you, I've been looking forward to trying this...

Do you know why you don't seem to be able to select with the mouse?


ozak(Posted 2006) [#4]
Hi Mark

You can add any seperator you like as per the example.
' Create tokenizer
myTokenizer:TLineTokenizer= New TLineTokenizer
myTokenizer.Init(",= ().")

' Setup highlighter
myHighlighter:TSyntaxHighLighter = New TSyntaxHighLighter
myHighLighter.Init(MyText, myTokenizer)
myHighLighter.SetFont("Courier New",12)
myHighLighter.AddKeyword("print")
myHighLighter.AddKeyword("do")
myHighLighter.AddKeyword("for")
myHighLighter.AddKeyword("end")
myHighLighter.AddKeyword("math")
myHighLighter.AddKeyword("random")


There are huge speedups to be made, but I didn't look to hard at the color coding speed, since my major idea was to only color the line you're currently writing.
Loading the code file just uses the same ColorLine code.

Actually I've been thinking about the community IDE as I'm working on Code Folding too. I got an idea where you can code fold any block of text you like and it just shows a + with the topmost line of code and some ... at the end.

Maybe, but I'm a busy man :)

As to why the mouse selection does not work (you can however, doubleclick on a word and have it selected) it is definately my syntax highlighters fault.
I'll look into this when I get some time for it ;)


klepto2(Posted 2006) [#5]
Nice one, I have tried various things by myself. And had a good working highlighter until i've tried to speed it up (I will have a look at my code later on). It was relativly fast.
was highlighting a code of ~120 kb in ~5 seconds (with ~13000 Keywords).
A little speedup to your highlighter would be to use TMap instead of TList for your Keywords. And a suggestion to enhance your highlighter is to make separate Keyword type, where you store the Color and Style. So you will be able to add different Colors to Keywords.

Here is a TMapped Version of 'syntaxhighlighter.bmx'


The speed increase will be noticable if you have to check alot of Keywords like all BMax Module Keywords.


ozak(Posted 2006) [#6]
Cool!
That was actually one of the speed fixes I had in mind :)


CASO(Posted 2007) [#7]
This is awesome.

I was thinking about starting an IDE project and this is a great syntax highlighting foundation.