This example illustrates how to implement simplified syntax highlighting for the T-SQL language by registering the ISyntaxHighlightService. Note that we do not use the DevExpress.CodeParser library in this example. Text is parsed into tokens (a list of SyntaxHighlightToken Class instances) manually in the CustomSyntaxHighlightService.ParseTokens() method. The resulting list is sorted (see Documentation - It is not mentioned that a list passed to the SubDocument.ApplySyntaxHighlight method should be sorted) and passed to the SubDocument.ApplySyntaxHighlight Method.
See Also:
Syntax highlighting for C# and VB code using DevExpress CodeParser and Syntax Highlight tokens - Syntax highlighting for C# and VB code using DevExpress CodeParser and Syntax Highlight tokens
Question Comments
Added By: Roland Radlmair at: 6/6/2013 1:35:59 AM
Hi,
could you also add the functionality for highlighting comments like "// this is a comment".
That would be great!
Thanks.
This example throws an exception when running with DevExpress 2013.1.9 and 2013.2.7. A workaround is to replace the order of the folowing lines:
richEditControl1.ReplaceService<ISyntaxHighlightService>(new CustomSyntaxHighlightService(richEditControl1.Document));
richEditControl1.LoadDocument("CarsXtraScheduling.sql");
There still remains an issue, because another document cannot be loaded: it still throws an exception.
Added By: Anders Wang at: 6/25/2015 8:47:48 PMpublic void Execute() {
document.ApplySyntaxHighlight(ParseTokens());
}
Hi,
it is not so correct way to call ParseTokens() each time when need ApplySyntaxHighlight. You can try it with increase the keywords array to above 200-500. The tokens should be initialized when the docment loaded. After then, it should be updated incrementally and should not be build each time.
Hello,
I have discussed this behavior with our developers and we have come to the conclusion that this behavior is by design.
For now, we are not planning to change it in the near future.
Instead of
// search for quotation marks
ranges = document.FindAll("'", SearchOptions.None);
for (int i = 0; i < ranges.Length / 2; i++) {
tokens.Add(new SyntaxHighlightToken(ranges[i * 2].Start.ToInt(),
ranges[i * 2 + 1].Start.ToInt() - ranges[i * 2].Start.ToInt() + 1, stringSettings));
it's better to use a Regex, especially if you want to highlight "double quoted strings" as well as 'single quoted strings'. We tried to search first for all single quotes and then for double quotes. not a good idea.
We had many errors, when a quotation was not ended (open quote exists without end quote) inside another quote (i.e. "this's a quote, "where "single 'quote' is not closed")
...
stringRegex = new Regex(@"((""(.|/[[:blank:]]/)*?"")|('(.|/[[:blank:]]/)*?'))"); //original pattern: (("(.|/[[:blank:]]/)*?")|('(.|/[[:blank:]]/)*?'))
...
// search for quoted strings
ranges = document.FindAll(stringRegex);
foreach (var range in ranges)
tokens.Add(new SyntaxHighlightToken(range.Start.ToInt(), range.Length, stringSettings));
Thank you Stephan for sharing your approach here and especially for posting your regular expression.