Provide syntax highlighting for JSON

2 posts, 0 answers
  1. Patrick
    Patrick avatar
    455 posts
    Member since:
    Aug 2012

    Posted 21 Oct 2019 Link to this post

    Hello,

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

  2. Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    539 posts

    Posted 24 Oct 2019 Link to this post

    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.
Back to Top