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/