Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / Windows Forms

Extending ICSharpCode.TextEditor Syntax Highlighting Schemas

Rate me:
Please Sign up or sign in to vote.
4.29/5 (5 votes)
25 Sep 2010CPOL3 min read 40K   18   3
How to use ICSharpCode.TextEditor, free, easy to use, extendable and good quality syntax highlighting control for .NET program.

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:

C#
public class AppSyntaxModeProvider : ISyntaxModeFileProvider
{
 List<syntaxmode> syntaxModes = null;
 
 public ICollection<syntaxmode> SyntaxModes {
  get {
   return syntaxModes;
  }
 }

 public AppSyntaxModeProvider()
 {
  Assembly assembly = Assembly.GetExecutingAssembly();

  //enumerate resource names if need
  //foreach (string resourceName in assembly.GetManifestResourceNames()){}

  //load modes list
  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();

  // load syntax schema
  Stream stream = assembly.GetManifestResourceStream(
    "WindowsFormsApplication1.Resources." + syntaxMode.FileName);
  return new XmlTextReader(stream);
 }
 
 public void UpdateSyntaxModeList()
 {
  // resources don't change during runtime
 }
}

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:

XML
<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

This article was originally posted at http://c-sharpening.blogspot.com/feeds/posts/default

License

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


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
QuestionHighlighting variables? Pin
Kuba_B1-Mar-11 23:36
Kuba_B1-Mar-11 23:36 
GeneralNot compatible source code Pin
Member 381005023-Sep-10 10:11
Member 381005023-Sep-10 10:11 
GeneralRe: Not compatible source code Pin
Ernest Poletaev25-Sep-10 7:25
Ernest Poletaev25-Sep-10 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.