This is a migrated thread and some comments may be shown as answers.

SQL comment & keyword in non-keyword

1 Answer 80 Views
SyntaxEditor
This is a migrated thread and some comments may be shown as answers.
Jong
Top achievements
Rank 1
Veteran
Jong asked on 09 Feb 2021, 08:38 PM

I have two problems with SyntaxEditor.

First problem is when any special characters are followed by -- other than a space, it is not treated as comment.

How can I make it so that it understands the second comment below is a comment line? (Any way to override a comment REGEX?)

-- testing working comment
--- testing not working comment

 

Second problem is when the keywords wrapped around with any special characters in a non-keyword are recognized as a keyword.

In other words, the word TEST_TABLE is one word and is NOT a keyword, but because TABLE is a keyword and it is wrapped with _ and a end of line, TABLE is highlighted.

How can I make it so that those keywords to be highlighted only when it is wrapped with start of line, end of line or a space? 

SELECT * from TEST_TABLE

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Feb 2021, 08:18 AM

Hi, John,

The default implementation that the SQLTagger offers uses the following keywords and comments:

        private static readonly string[] SqlKeywords = new string[]
        {
            "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", 
            "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", "CROSS",
        };

        private static readonly string[] COMMENTS = new string[]
        {
            "--",
        };

That is why you obtain the following coloring:

If you want to exclude "TABLE" from the keywords or add "---" as a comment, you can construct your own SQLTagger. A sample approach for creating custom languages/taggers is available here: https://docs.telerik.com/devtools/winforms/controls/syntax-editor/features/taggers/custom-language 

I have prepared a sample implementation for your reference: 

        public class CustomSqlTagger : SqlTagger
        {
            public CustomSqlTagger(RadSyntaxEditorElement editor) : base(editor)
            {
            }
            protected override bool TryGetClassificationType(string word, out ClassificationType classificationType)
            {
                if (word == "TABLE")
                {
                    classificationType = null;
                    return false;
                }
                else if (word == "---")
                {
                    classificationType = ClassificationTypes.Comment;
                    return true;
                }
                return base.TryGetClassificationType(word, out classificationType);
            }
        }

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
SyntaxEditor
Asked by
Jong
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or