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.
Added By:
Martijn Loeffen at:
3/3/2014 8:32:11 AM 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 PM public 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.
Added By:
Andrey (DevExpress Support) at:
6/26/2015 5:25:32 AM 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.
Added By:
Stephan Stauber at:
7/29/2015 7:25:58 AM 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));
Added By:
Oleg (DevExpress Support) at:
7/29/2015 7:44:46 AM Thank you Stephan for sharing your approach here and especially for posting your regular expression.
Added By:
Marc Trudel at:
6/14/2017 3:17:33 PM Hi
Do you have a newer version of this that supports comments ?
-- and /* */ highlighted in green as in SSMS
Thank you Added By:
Ingvar (DevExpress Support) at:
6/15/2017 3:30:19 AM Hi Marc,
This example illustrates how to apply syntax highlighting using custom rules (in this case, for basic constructions available in T-SQL). I.e., this example is not intended to fully support all T-SQL constructions, but to demonstrate how to handle different text elements. If you look at the ParseTokens method, you will see that the document is parsed manually in order to locate required parts (string entries and keywords). You can implement a similar search mechanism for comments and add required tokens to the output collection. To sum it up, all you need to do is to locate "comment" ranges using the Document.FindAll method and create SyntaxHighlightTokens for them. If you encounter any difficulties implementing this approach, please create a separate ticket and provide us with a sample project demonstrating the actual result. We will guide you on how to overcome these difficulties.
Added By:
Thomas Kafenda at:
4/10/2018 2:57:59 AM Thanks for the instructions. It works great.
Here is an extended array with T-SQL keywords. Maybe someone will need it:
[C#]
string[]keywords=newstring[]{"ADD","EXTERNAL","PROCEDURE","ALL","FETCH","PUBLIC","ALTER","FILE","RAISERROR","AND","FILLFACTOR","READ","ANY","FOR","READTEXT","AS","FOREIGN","RECONFIGURE","ASC","FREETEXT","REFERENCES","AUTHORIZATION","FREETEXTTABLE","REPLICATION","BACKUP","FROM","RESTORE","BEGIN","FULL","RESTRICT","BETWEEN","FUNCTION","RETURN","BREAK","GOTO","REVERT","BROWSE","GRANT","REVOKE","BULK","GROUP","RIGHT","BY","HAVING","ROLLBACK","CASCADE","HOLDLOCK","ROWCOUNT","CASE","IDENTITY","ROWGUIDCOL","CHECK","IDENTITY_INSERT","RULE","CHECKPOINT","IDENTITYCOL","SAVE","CLOSE","IF","SCHEMA","CLUSTERED","IN","SECURITYAUDIT","COALESCE","INDEX","SELECT","COLLATE","INNER","SEMANTICKEYPHRASETABLE","COLUMN","INSERT","SEMANTICSIMILARITYDETAILSTABLE","COMMIT","INTERSECT, SEMANTICSIMILARITYTABLE","COMPUTE","INTO","SESSION_USER","CONSTRAINT","IS","SET","CONTAINS","JOIN","SETUSER","CONTAINSTABLE","KEY","SHUTDOWN","CONTINUE","KILL","SOME","CONVERT","LEFT","STATISTICS","CREATE","LIKE","SYSTEM_USER","CROSS","LINENO","TABLE","CURRENT","LOAD","TABLESAMPLE","CURRENT_DATE","MERGE","TEXTSIZE","CURRENT_TIME","NATIONAL","THEN","CURRENT_TIMESTAMP","NOCHECK","TO","CURRENT_USER","NONCLUSTERED","TOP","CURSOR","NOT","TRAN","DATABASE","NULL","TRANSACTION","DBCC","NULLIF","TRIGGER","DEALLOCATE","OF","TRUNCATE","DECLARE","OFF","TRY_CONVERT","DEFAULT","OFFSETS","TSEQUAL","DELETE","ON","UNION","DENY","OPEN","UNIQUE","DESC","OPENDATASOURCE","UNPIVOT","DISK","OPENQUERY","UPDATE","DISTINCT","OPENROWSET","UPDATETEXT","DISTRIBUTED","OPENXML","USE","DOUBLE","OPTION","USER","DROP","OR","VALUES","DUMP","ORDER","VARYING","ELSE","OUTER","VIEW","END","OVER","WAITFOR","ERRLVL","PERCENT","WHEN","ESCAPE","PIVOT","WHERE","EXCEPT","PLAN","WHILE","EXEC","PRECISION","WITH","EXECUTE","PRIMARY","WITHIN GROUP","EXISTS","PRINT","WRITETEXT,"+"EXIT","PROC"};