Syntax Editor Custom Tagger for Handlebars {{ stuff }}

1 Answer 87 Views
SyntaxEditor
Coby
Top achievements
Rank 1
Coby asked on 16 Sep 2022, 11:40 PM

Hi,

What tagger would be best to identify elements contained in double-curly-braces?  Change the forecolor of the curly braces to yellow and the word or syntax between them to orange?  Also is there a way to iterate through all elements that were tagged?

Thanks

 

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Sep 2022, 09:45 AM
Hello, Coby, 

In order to highlight the text wrapped in quotes: {some string}, it would be necessary to create a custom tagger. A sample example is demonstrated in the following help article: https://docs.telerik.com/devtools/winforms/controls/syntax-editor/features/taggers/custom-language 

I have prepared a sample code snippet for your reference demonstrating how to split the words when using "{" and "}". I would like to 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. Please have also in mind that such a parsing implementation wouldn't be easy and it is developer's responsibility to implement the exact logic and cover the possible cases. 
        public RadForm1()
        {
            InitializeComponent();
        
            MyTagger customTagger = new MyTagger(this.radSyntaxEditor1.SyntaxEditorElement);
            this.radSyntaxEditor1.TaggersRegistry.RegisterTagger(customTagger);

            this.radSyntaxEditor1.Document = new Telerik.WinForms.SyntaxEditor.Core.Text.TextDocument("some other text {{person.firstname}} {{person.lastname}} code snippet here");
        }

        public class MyTagger : WordTaggerBase
        {
            private static readonly Dictionary<string, ClassificationType> WordsToClassificationType = new Dictionary<string, ClassificationType>();

            public MyTagger(RadSyntaxEditorElement editor) : base(editor)
            {
            }

            protected override Dictionary<string, ClassificationType> GetWordsToClassificationTypes()
            {
                return MyTagger.WordsToClassificationType;
            }

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

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

                if (c == '{' || c == '}')
                {
                    return 2;
                }

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

                return 0;
            }

            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] == '{' ||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;
            }

            protected override bool TryGetClassificationType(string word, out ClassificationType classificationType)
            {
                int number;
                if (int.TryParse(word, out number))
                {
                    classificationType = ClassificationTypes.NumberLiteral;
                    return true;
                }

                if (word.Contains("{") || word.Contains("}"))
                {
                    classificationType = ClassificationTypes.StringLiteral;
                    return true;
                }
                return base.TryGetClassificationType(word, out classificationType);
            }
        }

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

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

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