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?
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.
publicRadForm1()
{
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");
}
publicclassMyTagger : WordTaggerBase
{
privatestaticreadonlyDictionary<string, ClassificationType> WordsToClassificationType = new Dictionary<string, ClassificationType>();
publicMyTagger(RadSyntaxEditorElement editor) : base(editor)
{
}
protectedoverride Dictionary<string, ClassificationType> GetWordsToClassificationTypes()
{
return MyTagger.WordsToClassificationType;
}
internalstaticintGetCharType(char c)
{
if (c == '#')
{
return0;
}
if (char.IsWhiteSpace(c))
{
return1;
}
if (c == '{' || c == '}')
{
return2;
}
if ((char.IsPunctuation(c) || char.IsSymbol(c)))
{
return3;
}
return0;
}
protectedoverride IList<string> SplitIntoWords(stringvalue)
{
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;
}
protectedoverrideboolTryGetClassificationType(string word, out ClassificationType classificationType)
{
int number;
if (int.TryParse(word, out number))
{
classificationType = ClassificationTypes.NumberLiteral;
returntrue;
}
if (word.Contains("{") || word.Contains("}"))
{
classificationType = ClassificationTypes.StringLiteral;
returntrue;
}
returnbase.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