Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Syntax highlighting

Tony Edgecombe edited this page Feb 23, 2015 · 1 revision

This document describes the old .xshd file format used in ICSharpCode.TextEditor. AvalonEdit can also read this format, but you should use the new file format instead.

Basics

The Texteditor uses so called "XML Syntaxhightlighting Definition" (*.xshd) files to define a set of syntaxhighlighting rules.

Attach a syntaxhighlighting to the Text editor

To attach a syntaxhighlighting to the editor use following code:

string dir = @"resources\highlighting\\"; // Insert the path to your xshd-files.
FileSyntaxModeProvider fsmProvider; // Provider
if (Directory.Exists(dir))
{
    fsmProvider = new FileSyntaxModeProvider(dir); // Create new provider with the highlighting directory.
    HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmProvider); // Attach to the text editor.
    txtSQL.SetHighlighting("YourHighlighting"); // Activate the highlighting, use the name from the SyntaxDefinition node.
}

Creating custom XSHD-Files

To create custom XSHD-files you might want to know how the structure of such a file is:

Here is an example: The definition file for C#. Explanations will follow.

<?xml version="1.0"?>
<!-- syntaxdefinition for C# 2000 by Mike Krueger -->

<SyntaxDefinition name = "C#" extensions = ".cs">
	
	<Properties>
		<Property name="LineComment" value="//"/>
	</Properties>
	
	<Digits name = "Digits" bold = "false" italic = "false" color = "DarkBlue"/>

	<RuleSets>
		<RuleSet ignorecase="false">
			<Delimiters>&amp;&lt;&gt;~!%^*()-+=|\#/{}[]:;"' ,	.?</Delimiters>
			
			<Span name = "PreprocessorDirectives" rule = "PreprocessorSet" bold="false" italic="false" color="Green" stopateol = "true">
				<Begin>#</Begin>
			</Span>

			<!-- ... -->
		  
			<Span name = "BlockComment" rule = "CommentMarkerSet" bold = "false" italic = "false" color = "Green" stopateol = "false">
				<Begin>/*</Begin>
				<End>*/</End>
			</Span>
			
			<MarkPrevious bold = "true" italic = "false" color = "MidnightBlue">(</MarkPrevious>
			
			<KeyWords name = "Punctuation" bold = "false" italic = "false" color = "DarkGreen">
				<Key word = "?" />
				<Key word = "," />
				<Key word = "." />
				<Key word = ";" />
				<Key word = "(" />
				<Key word = ")" />
				<Key word = "[" />
				<Key word = "]" />
				<Key word = "{" />
				<Key word = "}" />
				<Key word = "+" />
				<Key word = "-" />
				<Key word = "/" />
				<Key word = "%" />
				<Key word = "*" />
				<Key word = "&lt;" />
				<Key word = "&gt;" />
				<Key word = "^" />
				<Key word = "=" />
				<Key word = "~" />
				<Key word = "!" />
				<Key word = "|" />
			<Key word = "&amp;" />
		  	</KeyWords>
		  
			<KeyWords name = "AccessKeywords" bold="true" italic="false" color="Black">
				<Key word = "this" />
				<Key word = "base" />
			</KeyWords>

<!-- ... -->
		</RuleSet>
		
		<RuleSet name = "CommentMarkerSet" ignorecase = "false">
		  	<Delimiters>&lt;&gt;~!@%^*()-+=|\#/{}[]:;"' ,	.?</Delimiters>
			<KeyWords name = "ErrorWords" bold="true" italic="false" color="Red">
				<Key word = "TODO" />
				<Key word = "FIXME" />
	 	 	</KeyWords>
			<KeyWords name = "WarningWords" bold="true" italic="false" color="#EEE0E000">
				<Key word = "HACK" />
				<Key word = "UNDONE" />
	 	 	</KeyWords>
		</RuleSet>
	</RuleSets>
</SyntaxDefinition>

IMPORTANT NOTE: In the following I will only explain the basics. For a full list of all tags and their meanings see XSHD Tags.

Explanation: The root element is called "SyntaxDefinition" which has a name-attribute (used to identify the highlighting definition with the text editor) and a extensions-Attribute (used to attach a file with certain extensions) the extensions can contain a list, each extension is separated by a semicolon. For ex.: .html;.htm.

The next element is the "Properties" element, with properties you can customize the editor window for your programming language or set special properties. For ex.: Change the background color of the editor window.

Next is the "Digits"-Element: You can use it for defining the appearance of all digits. For the list of Attributes see XSHD Tags.

The whole definition is split into different rulesets (use "RuleSets" to introduce the block). The main ruleset does not have a name. Each ruleset can contain spans (like comments or string literals) and KeyWord lists. Each span can reference a ruleset with the "rule"-Attribute. (See XSHD Tags for more information.)

Clone this wiki locally