[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
   Reporting
  [Expand]Data Grid
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Collapse]Rich Text Editor
   [Expand]Product Information
    Product Class Structure
    Supported Formats
   [Expand]Getting Started
   [Expand]RichEditControl Document
   [Expand]Fields
    Text Formatting
    Import and Export
   [Expand]Page Layout
    Printing
    AutoCorrect
    Mail Merge
    Restrictions and Protection
   [Expand]Visual Elements
    Services
    Events
    Commands
    HTML Tag Support
   [Collapse]Examples
    [Expand]UI Customization
    [Expand]Files
    [Expand]Text
    [Expand]Formatting
    [Expand]Styles
    [Expand]Pictures
    [Expand]Text Boxes
    [Expand]Tables
    [Expand]Document Elements
    [Expand]Layout
    [Collapse]Automation
      How to: Insert a Field to a Document
      How to: Create a Table of Contents
      How to: Create TOC for Special Cases
      How to: Create a Table of Contents in Code
      How to: Handle the HyperlinkClick Event to Invoke the Custom Form
      How to: Use the Visitor-Iterator Pattern to Retrieve the List of Document Fonts
      How to: Highlight Document Syntax
      How to: Insert Dynamic Content
     Spell Check
    [Expand]Export
    [Expand]Printing
    [Expand]Protection
    [Expand]Commands
    [Expand]Miscellaneous
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Printing-Exporting
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Highlight Document Syntax

The RichEditControl allows you to create a custom ISyntaxHighlightService (see ISyntaxHighlightService) implementation to display text in different colors and fonts according to the category of syntax sub-elements. These include keywords, comments, control-flow statements, variables, and other elements. This example describes how to highlight the T-SQL syntax.

IMAGE

Note

The syntax highlight implementation can affect the application's performance.

Expanded Parse Document Into Tokens

A token represents a document range that should be highlighted. You can use third-party libraries or add custom syntax highlight logic to parse a document into tokens and highlight them. You can combine both approaches.

Tip

You can use the DevExpress CodeParser library to parse a document into tokens. Refer to the How to use Syntax Highlight Tokens to implement custom syntax highlighting for a code sample. Note that the library supports limited amount of languages.

Take into account the following requirements when you parse document into tokens:

  • Each range in a document should be marked by a token.
  • Tokens cannot intersect.
  • Tokens are continuous (start after the other token).
  • Tokens cannot mark fields, bookmarks or hyperlinks.

Follow the steps below to parse the document into tokens:

  1. Call the SubDocument.FindAll method to search for keywords or specific symbols.

    You can use regular expressions to search for a syntax. Specify the DocumentSearchOptions.RegExResultMaxGuaranteedLength property to extend the maximum length of a string that can be obtained in a regular expression search. Access the property via the richEditControl.Options.Search notation.

  2. Convert all occurrences to SyntaxHighlightToken objects. You can specify a token's format options in the object constructor.
  3. Check whether the tokens intersect. If not, add them to the tokens collection.
  4. Parse the remaining text into tokens and add them to the same collection.
  5. Sort the objects in the collection according to their position in the original text.
Note

You can use the DocumentRange.Freeze or DocumentRangeExtensions (see DocumentRangeExtensions) class methods to improve performance during syntax highlight.

After one of these methods is called, RichEditControl stops tracking the actual document position for this range, and the target ranges cannot be modified. The frozen document ranges become invalid after the document is modified. Don't use these ranges for further document processing operations.

Expanded Specify Token's Format Options

The SyntaxHighlightProperties (see SyntaxHighlightProperties) class represents a token's format settings. You can pass this class's object to the DevExpress.XtraRichEdit.API.Native.SyntaxHighlightToken.%23ctor(System.Int32,System.Int32,DevExpress.XtraRichEdit.API.Native.SyntaxHighlightProperties) object constructor or use it as the SyntaxHighlightToken.Properties property value.

The code sample below converts keywords occurrences to the highlight tokens and specifies their foreground color:

Expanded Apply Syntax Highlight

Note

The RichEditControl highlights unformatted text syntax only.

Call the SubDocument.ApplySyntaxHighlight within the ISyntaxHighlightService.Execute method to enable syntax highlighting.

The code sample below uses the ParseTokens method shown above to create a list of SyntaxHighlightToken objects and pass it to the ApplySyntaxHighlight method.

In the main class, use the DevExpress.Xpf.RichEdit.RichEditControl.ReplaceService``1 method to register the created implementation.

Expanded See Also

Is this topic helpful?​​​​​​​