Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Collapse]Rich Text Editor
    Overview
   [Expand]Getting Started
   [Expand]Fundamentals
   [Collapse]Concepts
     Text Formatting
     Import and Export
     Mail Merge
     Restrictions and Protection
     Line Numbering
     Table of Contents
     Check Spelling
     AutoCorrect
     Printing
     Syntax Highlighting
     Traversing the Document
   [Expand]Visual Elements
   [Expand]Examples
   [Expand]Member Tables
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Syntax Highlighting

Syntax highlighting enables you to display text in different colors and fonts according to the category of text tokens.

Expanded Overview

To accomplish this task in the RichEditControl, you should perform the following steps:

  • Make sure that the RichEditControl's document contains only plain text.
  • Parse text into tokens according to certain terms or specific grammar.
  • Sort the collection of tokens so that the tokens are ordered by their start position in the document.
  • Pass the ordered collection to the SubDocument.ApplySyntaxHighlight method.

A correct implementation involves the use of a custom service that implements the ISyntaxHighlightService interface. Its ISyntaxHighlightService.Execute method should create a collection of tokens for the current document and call the ApplySyntaxHighlight method. The custom syntax highlighting service is registered as usual, by using the RichEditControl.ReplaceService<T> method.

Expanded What is Token?

A token used in syntax highlighting is a range of text (defined by its start position and length) with format settings. Format settings specify how this range of text should be displayed. A token is an object of the SyntaxHighlightToken type, its format settings are contained within the object of the SyntaxHighlightProperties type.

The token itself does not contain any portion of document's text. Only start position and length matter. It means that the original text should not contain complex structures such as fields, which can shift the document position. Bookmarks or hyperlinks are also not allowed.

Note

Do not load formatted text in the RichEditControl with syntax highlighting.

The following piece of code illustrates how the plain text can be parsed into tokens. We search for text enclosed with quotation marks, then for keywords. The remaining parts of the text are marked as plain tokens to fill gaps in token continuity. The resulting token collection should be ordered by its position in the text.

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4139.

Expanded DevExpress.CodeParser Library

A custom search for keywords is not the fastest way to parse text into tokens. You can use the DevExpress.CodeParser library to accomplish this task more effectively.

The code parser library is contained within the DevExpress.CodeParser.v17.1.dll assembly. Currently, it can be utilized to parse text in the following languages:

  • C#
  • Visual Basic
  • JavaScript
  • HTML
  • XAML
  • CSS

The library contains token category helper objects designed to parse text into a collection of tokens. The helper object is different for different languages, but exposes the same DevExpress.CodeParser.ITokenCategoryHelper interface. To obtain the helper object, use the static methods of the DevExpress.CodeParser.TokenCategoryHelperFactory class. The CreateHelperForFileExtensions(string extension) method creates a helper for the language determined by the file extension. The CreateHelper(ParserLanguageID languageID) method requires a language ID to create a proper helper. You can use another helper class, the DevExpress.CodeParser.ParserLanguage and its FromFileExtension method to obtain a language ID for the specified extension.

This technique is illustrated by the following code snippet.

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E2993.

Expanded Apply Syntax Highlight

When the text is parsed into tokens, you should specify the desired color and other formatting for each token according to its type, via the SyntaxHighlightToken.Properties. After that, the collection of tokens has to be ordered by the token position in the original text (a mandatory requirement). To display the text as the token formatting suggests, you should call a special method named SubDocument.ApplySyntaxHighlight.

This method changes formatting for the text visible in the RichEditControl's view. A special ISyntaxHighlightService service operates to ensure that formatting is always applied to the text in situations when the text is modified or the visible region is changed. You should call the ApplySyntaxHighlight method within the ISyntaxHighlightService.Execute method of this service, as illustrated in the DevExpress.CodeParser Library section earlier in this document.

To accomplish this task, you should create a custom service inherited from the ISyntaxHighlightService and implement the Execute and the ForceExecute methods. To register a custom service, use the RichEditControl.ReplaceService<T> method.

The picture below illustrates syntax highlighting for the text in C# displayed within the RichEditControl's SimpleView with custom line numbering.

Expanded See Also

How would you rate this topic?​​​​​​​