Sep 22, 2010

Add Syntax Highlighting Control

Background

How to choose free, easy to use, extendable and good quality syntax highlighting control for .NET program?

Scintilla

First, what i have try to use, was ScintillaNet, but i have no luck to get it worked in test program. It need to setup before use, require unmanaged companion library, what make difficult to 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 worked, but result seems unstable and unrepeatable. Personally i not recommend to use Scintilla in .NET application, but possible it good choice for C++ project.

ICSharpCode.TextEditor

After searching i found 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.


Get It Worked

First what need to do is add assembly reference to a project. Next you choose assembly and controls for Visual Studio Toolbox. After that you can put control to the Form. Surprising 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 embedded into control.

License Issues

ICSharpCode.TextEditor available under LGPL license, which means you can use this software component for free in open source or commercial application if you not made own modification to licensed component.

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 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 with rules of LGPL, you have to provide source of modified control. It can make problem for distributing in propiertary software.

Embedding highlight schema

What we need to embed schema into application? You have to realize own schema provider. It is really easy, if you look for original resource schema provider source code. We use it as example. Add new class to 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 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:


 


File, containing SQL schema i took from Paul Kohler's MiniSqlQuery, released under the terms of the Microsoft Public License (http://pksoftware.net/MiniSqlQuery/).

Be carefull to provide right names of embedded resource for embedded files. Generally it [assembly.namespace].[folder].[file.name]

Attach schema provider to control


// Attach to the text editor.
HighlightingManager.Manager.AddSyntaxModeFileProvider(
  new AppSyntaxModeProvider());

Select syntax schema


editorControl.SetHighlighting("SQL");

Putting all together

Example for this article contains good base to experiments. I add Property Editor to play with properties of editor control. Sample application can be used to study features of ICSharpCode.TextEditor


When you set highlighting schema, keep in mind, that name is case sensitive and should exactly match name described in schema files.


Links and credits


SharpDevelop Web Site

SharpDevelop Wiki

Paul Kohler's MiniSqlQuery

Dissecting a C# Application: Inside SharpDevelop


Downloads


Download sample code

3 comments:

  1. The Editors Control for Windows Forms application is flexible data entry controls.
    http://www.kettic.com/winforms_ui/editor.shtml

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Polen ist ein führendes Ziel für IT-Nearshoring. Das Land verfügt über eine große Anzahl hochqualifizierter IT-Fachkräfte und bietet wettbewerbsfähige Kostenstrukturen. Durch die Auslagerung von IT-Projekten nach Polen können Unternehmen von einer verbesserten Effizienz, schnelleren Markteinführungszeiten und qualitativ hochwertigen Lösungen profitieren. Die polnische Regierung hat auch Investitionen in die IT-Infrastruktur und Ausbildungsprogramme getätigt, um den IT-Sektor weiter zu stärken. Mit Polen als IT-Nearshoring-Partner können Unternehmen ihre Leistungsfähigkeit steigern und gleichzeitig ihre Kosten optimieren. lesen Sie diesen Beitrag

    ReplyDelete