Click here to Skip to main content
Click here to Skip to main content
Go to top

Extending ICSharpCode.TextEditor Syntax Highlighting Schemas

, 25 Sep 2010
Rate this:
Please Sign up or sign in to vote.
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:

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:

<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

License

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

Share

About the Author

Ernest Poletaev
Software Developer (Senior) Enterprise Information Systems, Plc
Thailand Thailand
Senior Software Developer in Enterprise Information Systems, an ERP / CRM / Docflow Software Solution Provider for Russian Local Market.
 
Professional Developer (C, C++, C#), since 1984. Experienced in many programming technologies.
 
Now i resides in Kalasin, Thailand, working remotely for my company

Comments and Discussions

 
QuestionHighlighting variables? PinmemberKuba_B1-Mar-11 23:36 
GeneralNot compatible source code PinmemberMember 381005023-Sep-10 10:11 
GeneralRe: Not compatible source code PinmemberErnest Poletaev25-Sep-10 7:25 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 25 Sep 2010
Article Copyright 2010 by Ernest Poletaev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid