This is a WPF control I just put on CodePlex that implements the basic functionality for a source code editor.

You define it in the XAML with something like this (of course after you put the assembly com.bodurov.WpfControls.SourceCodeEditor.dll in your bin folder):

<Window x:Class="YourWindow"
    Title="Source Code Editor" Height="450" Width="650">
      <ctrl:SourceCodeEditor x:Name="Editor" Grid.Column="0" Grid.Row="0"/>

The assembly com.bodurov.WpfControls.SourceCodeEditor.dll is available here.

I was looking at the solution presented by Tamir Khason. If you follow this link you will see an example of usage of the RichTextBox WPF control for basic source code formatting. Unfortunately that solution was quite very slow. I tried to improve the performance by changing the whole approach to the problem and the result is on the CodePlex site. My solution is based more on using linked lists and avoiding formatting multiple times of the lines that were not changed. Use property Text to set the contents.

I tried to abstract the logic for a particular language formatting so it can easily be adapted to a different language with specific keywords. My default implementation is for T-SQL simply because that's what I needed but similar implementation could be done from any other language.

If you want to adapt it to your language you have to create implementation of the IParagraphProcessor. SplitWordsRegex is a regular expression that defines how do you split the words. GetWordTypeID assigns a specific ID for each word. If the ID is 0 no formatting is performed. If the ID is higher than 0 then the element is isolated in Inline text and is passed to the FormatInlineForID method together with the ID. This way you can have different color for different type of keywords. For example I use blue for the SQL keywords and purple for the T-SQL function names.

This is how you can define your implementation of IParagraphProcessor on the control markup.

       ParagraphProcessorType="Your.NameSpace.YourIParagraphProcessor, Your.AssemblyName" />

For now I don't have comment and string formatting implemented. I will look at that as soon as I have some time.

You can also check another similar project I have for Silverlight:

Share this post:   digg     Stumble Upon     E-mail

Aaron DH
Posted on 5/13/2010 11:08:49 AM

Cool editor... Is there any way I could convince you to compile your assemblies on codeplex with a strong name? It would be really helpful.

Vladimir Bodurov
Posted on 5/13/2010 12:19:40 PM

Thanks Aaron,

You can get the source and compile it as you like. It's open source :)

Posted on 5/18/2010 12:03:58 PM

Nice, but what comment highlighting?

Posted on 5/18/2010 12:09:38 PM

I am sorry, I hadn't red last sentence. I hope, that you will continue :)

Posted on 10/25/2014 6:13:38 PM

This is really cool! I modified it a bit and used it in my sql performance tuning app:

Vladimir Bodurov
Posted on 10/26/2014 2:40:41 PM


Please post your comments:

Email (optional): Your email address will not be posted.
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box: