• Log in or sign up for an account here: https://www.sythe.org/login

Fast syntax highlighting in RTB

Darthatron

Massive Troll

Retired Sectional Moderator
Visual Basic Programmers
Joined
May 22, 2006
Sythe Gold
0
Messages
1,612
Age
35
Location
The internet.
So, I'm making an assembler with a GUI in C#, and I've decided to just make a user control with some syntax highlighting. I'm not very imaginative with names, so I'm just calling it CodeTextBox. Chances are that I'll make it open source once it's done anyway, so I was wondering if anyone was willing to put some effort in to help make it a better control.

So far I've got syntax highlighting working pretty well, and there's no noticeable lag even with hundreds of lines of code. The way I did this was by only updating the syntax on the current line, rather than the entire RTB all at once.

I want it to be simple to change the highlighting that takes place, it currently uses something like this:
Code:
            txtMain.TheSyntax.ClearSyntax();
            txtMain.TheSyntax.AddSyntax(new string[] { ".org", ".word", ".hword", ".align", }, Color.Gray); //Directives
            txtMain.TheSyntax.AddSyntax(new string[] { "push", "pop", "mov", "movs", "ldr", "ldrb", "ldrh", "lsl", "lsr", "b", "bx", "bl", "blx", "bge", "beq", "blt", "cmp", "add", "sub" }, Color.Blue); //Keywords
            txtMain.TheSyntax.AddSyntax(new string[] { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "lr", "sp", "pc" }, Color.DarkCyan); //Registers

Other syntax colors are set pretty easily as well:
Code:
            txtMain.TheSyntax.NumericColor = Color.DarkMagenta;
            txtMain.TheSyntax.HighlightNumbers = true;
            txtMain.TheSyntax.CommentColor = Color.DarkGreen;
            txtMain.TheSyntax.HighlightComments = false;
            txtMain.TheSyntax.StringColor = Color.DarkRed;
            txtMain.TheSyntax.HighlightStrings = false;

And of course you can set the size of the tab key, rather than sending an actual TAB character to the textbox.
Code:
            txtMain.TabSize = 4;

Currently the only thing not complete for Syntax Highlighting is comments, mainly because they are multi-line, so I'm trying to work out a way to make that work well. Ideas?

I'm not really having trouble with anything, but I think it could be a good community project if anyone else is bored. So, who's bored?
 
Hey man, I'll make some changes to this tonight after work\tomorrow morning, heading off to work here in a minute. I have a question though so I don't go changing things on you.

Is there any particular reason you're not inheriting from a RichTextBox, and that on the on change event calling a parser? It'd take out the need to call Win32 functions (well, maybe not entirely but I've only glance over the code atm).

Then by separating out the parser you can easily reuse it for things that aren't specifically this control
 
No real reason, really. I just felt it would be more open if I didn't inherit anything. But if you think it would be better, then fair enough. ^_^

I don't really use C# all that often, so I'm not surprised (and won't be offended) if my code is sloppy.
 
PHP Shit Show 2k26
If you finish this then I may create a version for Java and we could perhaps provide them together on SF?
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
To make things a bit more flexible for the end-user you might want to manage syntax highlighting through XML like what AvalonEdit does
 
Couldn't you have the syntax highlighting for the whole document run in the background? It shouldn't be that hard to properly do it, no matter how many lines the file is.
 
Runescape Gamblingrunescape gambling
Back
Top