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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctrl="clr-namespace:com.bodurov.WpfControls;assembly=com.bodurov.WpfControls.SourceCodeEditor"
    Title="Source Code Editor" Height="450" Width="650">
    <Grid>
      <ctrl:SourceCodeEditor x:Name="Editor" Grid.Column="0" Grid.Row="0"/>
    </Grid>
</Window>

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.

<ctrl:SourceCodeEditor 
       x:Name="Editor" 
       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: http://blog.bodurov.com/Xml-Code-Editor/

Share this post:   digg     Stumble Upon     del.icio.us     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 http://sourcecodeeditor.codeplex.com/SourceControl/list/changesets and compile it as you like. It's open source :)

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

Nice, but what comment highlighting?

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

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

alister
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: www.minidba.com
Thanks

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

Cool!

Commenting temporarily disabled