Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi,

I am making a small, lightweight programming language. I have got a parser and a compiler sorted out and ready to go. Now I just need a text editor for the scripts. I can make a text editor, but I need a syntax highlighter. I need a script which wil search the text for a word and change the colour of it every time I update the text. I usually know how to go about these things but this one really stumped me. Any suggestions??
Posted

1. Use a RichTextBox derived class.
2. Read some articles on the web about RTF specification, especially the part about colors.
3. Create an appropriate Regex to find the keywords and surround them with the appropriate RTF markers.
4. Make sure you run the Regex against the RichTextBox's Rtf property, not Text!
5. Call the code from the overrided OnTextChanged protected method.
6. Have fun with your programming language!

P.S. While I can give you the actual code, I thought it would be better for you to try it yourself first.
If you are unable to do it, put a comment here in a few days and I'll give you the code.
Here, at CodeProject, we encourage programmers to code themselves.
Thank you for understanding.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Apr-11 21:16pm    
Quite a long story. My 5.
I would be curious to see how you solve just one problem: what happens you someone paste RTF into your file which could break the highlight structure; another problem: performance with big files (over 100M).
--SA
Toli Cuturicu 14-Apr-11 22:33pm    
1. Comment from OP: Sorry. I need something simple. I don't want to create an elaborate syntax I just need to highlight a few words.
2. This would not be an all-purpose Control, so pasting RTF can be disabled as it doesn't serve the purpose: a light-weight coding editor.
3. A code file of a few MB!? Never seen such a thing!
Sergey Alexandrovich Kryukov 14-Apr-11 23:02pm    
I asked you, Toli, not OP, so:
1) is irrelevant;
2) danger of paste is underestimated, disabling paste is just lame; one good solution would be converting paste to plain text;
3) I mean 1-10M (as I remember, this size if already a problem even without), sorry. In real life, it happens: if the code is XML or HTML so the code is some more or less big book.
--SA
Toli Cuturicu 15-Apr-11 14:31pm    
1. ok.
2. About paste... This is just for code, i.e. plain text which is colored by the control. It really shouldn't be allowed to paste rtf in it, because it makes no sense. Can you paste rtf in Visual Studio code editor??
3. Yes, it starts to get slower when approaching 1 megabyte, even with compiled Regex. Anyway, for a programming language it should be no problem.
Sergey Alexandrovich Kryukov 15-Apr-11 15:42pm    
I think we understand each other by this point.
Thank you for your comments.
--SA
Hi,

see this CodeProject article that has a control that very well suits your requirements:
Fast Colored TextBox for syntax highlighting[^]
 
Share this answer
 
Comments
Codemonkey3 14-Apr-11 3:51am    
Sorry. I need something simple. I don't want to create an elaborate syntax I just need to highlight a few words.
BobJanova 14-Apr-11 5:50am    
Excellent link.
Espen Harlinn 14-Apr-11 17:11pm    
Good link, my 5
Sergey Alexandrovich Kryukov 14-Apr-11 21:15pm    
Looks very interesting, I'll need to check it out.
--SA
 
Share this answer
 
Comments
Espen Harlinn 14-Apr-11 17:11pm    
Good idea, my 5
Sergey Alexandrovich Kryukov 14-Apr-11 21:13pm    
Agree, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900