Hi,
editor.TextFormatDefinitions.AddLast("Brace", new TextFormatDefinition(null, Brushes.Black));
...
MultilineTags.Add(new TagSpan<ClassificationTag>(tempSnapshotSpan, new ClassificationTag(new ClassificationType("Brace"))));
Nothing special - RadSyntaxEditor and those two lines in WPF. Foreground is working very well - I can highlight any part of my text as I wish. But border and background does not working ... And also unwanted behavior - foreground of other highlighting is reset when I try highlight by background change... Even just border highlighting would be sufficient ... (I want highlight closest braces.)
Update:
Working brace content highlighter:
internal class ExpressionTagger : CSharpTagger
{
const string
openBraces = "([{",
closeBraces = ")]}";
new RadSyntaxEditor Editor => (RadSyntaxEditor)base.Editor;
public ExpressionTagger(RadSyntaxEditor editor) : base(editor)
{
editor.TextFormatDefinitions.AddLast("Class", new TextFormatDefinition(new SolidColorBrush(Color.FromRgb(43, 145, 175))));
editor.TextFormatDefinitions.AddLast("Brace", new TextFormatDefinition(new SolidColorBrush(Color.FromRgb(155, 83, 105))));
AddWord("Field", new ClassificationType("Class"));
AddWord("Custom", new ClassificationType("Class"));
AddWord("Triggers", new ClassificationType("Class"));
StringMatchingRegex = String.Empty; // Nemá smysl, neumí najít co chceme.
editor.CaretPosition.PositionChanged += CaretPosition_PositionChanged;
editor.KeyDown += CaretPosition_PositionChanged;
editor.KeyUp += CaretPosition_PositionChanged;
}
private void CaretPosition_PositionChanged(object sender, EventArgs e)
{
if (Editor.CaretPosition.Index < Document.CurrentSnapshot.Span.End)
{
char currentChar = Document.CurrentSnapshot.GetText(new Span(Editor.CaretPosition.Index, 1))[0];
if (MultilineTags.Any(tag => tag.Tag.ClassificationType.Name == "Brace") ||
(KeyboardModifiers.IsAltDown && (openBraces.Contains(currentChar) || closeBraces.Contains(currentChar))))
{
InvalidateMultilineTags();
}
}
}
protected override void RebuildMultilineTags()
{
base.RebuildMultilineTags();
string text = Editor.Document.CurrentSnapshot.GetText();
foreach (Match literal in Regex.Matches(text, "\"(?:[^\"]|\"{2})*\""))
{
TextSnapshotSpan tempSnapshotSpan = new TextSnapshotSpan(Document.CurrentSnapshot,
new Span(literal.Groups[0].Index, literal.Groups[0].Length));
MultilineTags.Add(new TagSpan<ClassificationTag>(tempSnapshotSpan, new ClassificationTag(ClassificationTypes.StringLiteral)));
}
if (KeyboardModifiers.IsAltDown)
{
int
startIndex = Editor.CaretPosition.Index,
searchIndex = 1,
closeIndex = 0;
char currentChar = Document.CurrentSnapshot.GetText(new Span(Editor.CaretPosition.Index, 1))[0];
int braceIndex = openBraces.IndexOf(currentChar);
if (braceIndex > -1)
{
char
openBrace = currentChar,
closeBrace = closeBraces[braceIndex];
closeIndex = startIndex + 1;
while (searchIndex > 0 && closeIndex < text.Length)
{
currentChar = text[closeIndex];
if (currentChar == openBrace)
{
searchIndex++;
}
if (currentChar == closeBrace)
{
searchIndex--;
}
closeIndex++;
}
closeIndex--;
currentChar = char.MinValue; // reset
}
braceIndex = closeBraces.IndexOf(currentChar);
if (braceIndex > -1)
{
char
openBrace = openBraces[braceIndex],
closeBrace = currentChar;
closeIndex = startIndex;
startIndex--;
while (searchIndex > 0 && startIndex > 0)
{
currentChar = text[startIndex];
if (currentChar == closeBrace)
{
searchIndex++;
}
if (currentChar == openBrace)
{
searchIndex--;
}
startIndex--;
}
startIndex++;
}
if (searchIndex == 0) // Found
{
TextSnapshotSpan tempSnapshotSpan = new TextSnapshotSpan(Document.CurrentSnapshot,
new Span(startIndex, closeIndex - startIndex + 1));
MultilineTags.Add(new TagSpan<ClassificationTag>(tempSnapshotSpan, new ClassificationTag(new ClassificationType("Brace"))));
}
}
}
}
I have tried to recreate the null exception using your code, but to no avail. Would it be possible to send over a runnable project that shows this?
About the TextFormatDefinition and its background and border properties, these are automatically used only with the TextSearchHighlightTagger after calling its UpdateSearchWord method. To change the colors used with the UpdateSearchWord you can add a TextFormatDefinition for the "SearchFormatting" name.
If you want to just to color different words, you can create a custom UI layer.