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

Provide syntax highlighting for JSON

1 Answer 549 Views
SyntaxEditor
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
Patrick asked on 21 Oct 2019, 02:08 PM

Hello,

Or at least provide a way for developpers to define a new syntax highlighter.

1 Answer, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 24 Oct 2019, 08:53 AM

Hello Patrick,

The syntax highlighting in the RadSyntaxEditor is done by Taggers. The process of creating a custom tagger involves inheriting the TaggerBase class and implementing the GetTags method. You can check out the following article: Custom Tagger, where this is demonstrated.

As a guidance, when implementing a custom tagger you can take a look at the source code of the XmlTagger class:

public class XmlTagger : TaggerBase<ClassificationTag>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlTagger"/> class.
        /// </summary>
        public XmlTagger(ITextDocumentEditor editor)
            : base(editor)
        {
        }

 

        /// <summary>
        /// Gets the tags.
        /// </summary>
        public override IEnumerable<TagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
        {
            foreach (var span in spans)
            {
                string spanText = span.GetText();
                string stringToMatch = PrepareRegexString();
                string[] regexGroups = new string[] { "attribute", "element", "comment", "tag", "string", "content" };

 

                Regex regularExpression = new Regex(stringToMatch, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
                MatchCollection matches = regularExpression.Matches(spanText);

 

                foreach (Match match in matches)
                {
                    foreach (var description in regexGroups)
                    {
                        if (match.Groups[description].Success)
                        {
                            var start = span.Start + match.Index;
                            var length = match.Length;
                            var textSnapshot = this.Document.CurrentSnapshot;
                            var tagSpan = CreateTagSpan(textSnapshot, start, length, description);

 

                            yield return tagSpan;
                        }
                    }
                }
            }

 

            yield break;
        }

 

        private static string PrepareRegexString()
        {
            string attributes = @"\G(?<attribute>\s*[a-zA-Z][a-zA-Z0-9.:*_]*\s*(?==))";
            string elements = @"\G(?<element>(?<=(<)|(</))[a-zA-Z][a-zA-Z0-9.:*_]*\s*)";
            string comments = @"\G(?<comment><!--\s*[\s\S]*\s*-->\s*)";
            string tags = @"\G(?<tag>(</|<|/>|>)\s*)";
            string strings = "\\G(?<string>=\\s*\"[_=#{}a-zA-Z0-9.:;\\s-/,*]*\\s*\"\\s*)";
            string content = @"\G(?<content>[^<]+\s*)";
            string[] xamlClassifications = new string[] { attributes, elements, comments, tags, strings, content };

 

            StringBuilder builder = new StringBuilder();

 

            builder.Append(@"\s*");
            for (int i = 0; i < xamlClassifications.Count() - 1; i++)
            {
                builder.AppendFormat("{0}|", xamlClassifications[i]);
            }
            builder.AppendFormat("{0}", xamlClassifications[xamlClassifications.Count() - 1]);
            builder.Append(@"\s*");

 

            return builder.ToString();
        }

 

        private static TagSpan<ClassificationTag> CreateTagSpan(TextSnapshot snapshot, int start, int len, string description)
        {
            var textSnapshotSpan = new TextSnapshotSpan(snapshot, new Span(start, len));
            var classificationType = XmlSyntaxHighlightingHelper.GetXmlClassificationType(description);
            var classificationTag = new ClassificationTag(classificationType);
            var tagSpan = new TagSpan<ClassificationTag>(textSnapshotSpan, classificationTag);

 

            return tagSpan;
        }
    }

I hope you find this helpful.

Regards,
Vladimir Stoyanov
Progress Telerik

Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
SyntaxEditor
Asked by
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Vladimir Stoyanov
Telerik team
Share this question
or