Or at least provide a way for developpers to define a new syntax highlighter.
1 Answer, 1 is accepted
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:
publicclassXmlTagger : TaggerBase<ClassificationTag>
{
///<summary>/// Initializes a new instance of the <see cref="XmlTagger"/> class.///</summary>publicXmlTagger(ITextDocumentEditor editor)
: base(editor)
{
}
///<summary>/// Gets the tags.///</summary>publicoverride IEnumerable<TagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
foreach (var span in spans)
{
string spanText = span.GetText();
string stringToMatch = PrepareRegexString();
string[] regexGroups = newstring[] { "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);
yieldreturn tagSpan;
}
}
}
}
yieldbreak;
}
privatestaticstringPrepareRegexString()
{
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 = newstring[] { 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();
}
privatestatic 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.