Introduction
This post discusses how to choose free, easy to use, extendable and good quality syntax highlighting control for .NET program?
Scintilla
First, what I tried to use was ScintillaNet, but I had no luck to get it working in the test program. It needs to be setup before use, requires unmanaged companion library, which makes it difficult for multiplatform use. After all, I see my Visual Studio C# 2010 Express very unstable, after adding ScintillaNet control to Toolbox. After many experiments I get it working, but the result seems unstable and unrepeatable. Personally I do not recommend using Scintilla in .NET application, but possibly it is a good choice for C++ project.
ICSharpCode.TextEditor
After searching, I found the editor control from a well known open source IDE SharpDevelop. Syntax Highlighting Text Editor Control looks very good and light after Scintilla. It requires less time to load and seems much more stable. You can find the source code and binaries on the SharpDevelop download page.
Get It Working
First, what you need to do is add an assembly reference to a project. Next, you choose assembly and controls for Visual Studio Toolbox. After that, you can put control to the Form. Surprisingly, you will not see Property to choose syntax highlight schema. Instead of this, you have to use method SetHighlighting
. String
parameter sets highlighting schema from available schemas list. These schemas are embedded into the control.
License Issues
ICSharpCode.TextEditor
is available under LGPL license, which means you can use this software component for free in open source or commercial applications if you have not made your own modification to the licensed component. But if you modify the source, you have to provide the modified source code for public access, according to the license rules.
Adding New Highlight Schema
What if list of embedded schemas is not enough for your application? You can add own, using files, as well described in article on SharpDevelop Wiki. But what if you do not like to waste disk space with multiple files or cannot use external files in your application? If you add new schema to control's assembly, according to the rules of LGPL, you have to provide the source of the modified control. It can create a problem for distribution in proprietary software.
Embedding Highlight Schema
What we need to embed schema into an application? You have to realize your own schema provider. It is really easy, if you look for the original resource schema provider source code. We use it as an example. Add a new class to the application:
public class AppSyntaxModeProvider : ISyntaxModeFileProvider
{
List<syntaxmode> syntaxModes = null;
public ICollection<syntaxmode> SyntaxModes {
get {
return syntaxModes;
}
}
public AppSyntaxModeProvider()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream syntaxModeStream = assembly.GetManifestResourceStream(
"WindowsFormsApplication1.Resources.SyntaxModes.xml");
if (syntaxModeStream != null) {
syntaxModes = SyntaxMode.GetSyntaxModes(syntaxModeStream);
} else {
syntaxModes = new List<syntaxmode>();
}
}
public XmlTextReader GetSyntaxModeFile(SyntaxMode syntaxMode)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(
"WindowsFormsApplication1.Resources." + syntaxMode.FileName);
return new XmlTextReader(stream);
}
public void UpdateSyntaxModeList()
{
}
}
Add Schema Files to Embedded Resources
When we finish with the provider, we can embed schema files to application. We will need at least two files: SyntaxModes.xml and schema file, SQL-Mode.xshd for example, if we provide SQL syntax mode. I add to project new folder, named Resources, add files and choose in Properties Build Action - Embedded Resource.
SyntaxModes.xml file should contain all added schema description, in our example only one:
<syntaxmodes version="1.0">
<mode extensions=".sql" file="SQL-Mode.xshd" name="SQL">
</mode></syntaxmodes>
I took the file containing SQL schema from Paul Kohler's MiniSqlQuery, released under the terms of the Microsoft Public License (http://pksoftware.net/MiniSqlQuery/).
Be careful to provide the right names of embedded resource for embedded files. Generally it is[assembly.namespace].[folder].[file.name].
Putting It All Together
An example for this article contains a good base for experiments. I add Property Editor to play with properties of editor control. The sample application can be used to study features of ICSharpCode.TextEditor
.
When you set highlighting schema, keep in mind that the name is case sensitive and should exactly match the name described in schema files.
Links and Credits