SyntaxEditor cannot more, than change Foreground by custom Tagger

0 Answers 69 Views
SyntaxEditor
Matt
Top achievements
Rank 1
Matt asked on 06 Dec 2022, 02:35 PM | edited on 08 Dec 2022, 09:19 AM

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"))));
            }
        }
    }
}
Just it is ugly, because I am able only to change Foreground. How can I at least use Find border boxes or any better highlight option?
Matt
Top achievements
Rank 1
commented on 16 May 2023, 11:36 AM

So, there is no solution?

No answers yet. Maybe you can help?

Tags
SyntaxEditor
Asked by
Matt
Top achievements
Rank 1
Share this question
or