Completed
Last Updated: 06 May 2020 10:40 by ADMIN
Release R2 2020
Iprel
Created on: 28 Jan 2020 15:29
Category: SyntaxEditor
Type: Feature Request
1
RadSyntaxEditor: Strings are not colored in C#, VB, JS
How can I create a tagger that recognizes strings (text contained in double quotes: "text") and apply a color to it?
3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 30 Jan 2020 14:24

Hello, Marco,   

As it was previously noted, such a parsing implementation wouldn't be easy and it needs planning for implementing and covering the possible cases. However, we have prepared a sample example how to accomplish coloring of the string values in quotes: 

        public RadForm1()
        {
            InitializeComponent();

            CustomCSharpTagger currentLanguageTagger = new CustomCSharpTagger(this.radSyntaxEditor1.SyntaxEditorElement);
            this.radSyntaxEditor1.TaggersRegistry.RegisterTagger(currentLanguageTagger);
            this.radSyntaxEditor1.TextFormatDefinitions.AddLast(ClassificationTypes.StringLiteral,
                new Telerik.WinForms.Controls.SyntaxEditor.UI.TextFormatDefinition(new SolidBrush(Color.Fuchsia)));

        }

        public class CustomCSharpTagger : CSharpTagger
        {
            public CustomCSharpTagger(RadSyntaxEditorElement editor)
                : base(editor)
            {
            }

            private static readonly string[] STRINGS = new string[]
            {
                "\"",
            };

            private static readonly Dictionary<string, ClassificationType> WordsToClassificationType;

            static CustomCSharpTagger()
            {
                WordsToClassificationType = new Dictionary<string, ClassificationType>();

                foreach (var str in STRINGS)
                {
                    WordsToClassificationType.Add(str, ClassificationTypes.StringLiteral);
                }
            }

            protected override IList<string> SplitIntoWords(string value)
            {
                List<string> words = new List<string>();
                string word;
                int lastCharType = -1;
                int startIndex = 0;
                for (int i = 0; i < value.Length; i++)
                {
                    int charType = GetCharType(value[i]);
                    if (lastCharType == 2)
                    {
                        int start = Math.Max(0, i - 1);
                        StringBuilder res = new StringBuilder();
                        res.Append(value[start]);

                        start = i;
                        while (start < value.Length)
                        {
                            res.Append(value[start]);
                            if (value[start] == '\"')
                            {
                                i = start + 1;
                                break;
                            }

                            start++;
                        }

                        words.Add(res.ToString());
                        startIndex = i;
                        lastCharType = charType;

                        continue;
                    }

                    if (charType != lastCharType)
                    {
                        word = value.Substring(startIndex, i - startIndex);
                        words.Add(word);
                        startIndex = i;
                        lastCharType = charType;
                    }
                }

                word = value.Substring(startIndex, value.Length - startIndex);
                words.Add(word);

                return words;
            }

            internal static int GetCharType(char c)
            {
                if (c == '#')
                {
                    return 0;
                }

                if (char.IsWhiteSpace(c))
                {
                    return 1;
                }

                if (c == '\"')
                {
                    return 2;
                }

                if ((char.IsPunctuation(c) || char.IsSymbol(c)))
                {
                    return 3;
                }

                return 0;
            }

            protected override bool TryGetClassificationType(string word, out ClassificationType classificationType)
            {
                if (word.StartsWith("\""))
                {
                    classificationType = ClassificationTypes.StringLiteral;

                    return true;
                }

                return base.TryGetClassificationType(word, out classificationType);
            }
        }

 

The achieved result is illustrated in the below screenshot. 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:

Should you have further questions please let me know.

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Iprel
Posted on: 29 Jan 2020 11:12
I have seen the document that explains how to create custom taggers, but I have not found a way to recognize strings, can you give me an example?
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 29 Jan 2020 09:20
Hello, Marco,   

Currently, RadSyntaxEditor doesn't highlight the text wrapped in quotes: "some string".  However, it offers an appropriate API for creating custom taggers where you can implement any custom logic for parsing parts of the text which meet a specific condition. Since this type of parsing wouldn't be an easy task and it requires planning, it sounds like a reasonable request for improvement of all predefined taggers: https://docs.telerik.com/devtools/winforms/controls/syntax-editor/features/taggers/overview 

I have logged it in our feedback portal by making this thread public. You can track its progress, subscribe for status changes and add your comments on the following link - feedback item.

I have also updated your Telerik points.

Off topic, I have noticed that you have submitted several tickets about RadSyntaxEditor. Since it has been recently released, it would be greatly appreciated if you can share with us your feedback and first impressions when using RadSyntaxEditor. Our customers' opinion is always extremely valuable.

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.