.netregexwpfrazoravalonedit

AvalonEdit XSHD Syntax Highlighting - Ending a Span


I'm using AvalonEdit and working on adding syntax highlighting for a file (with syntax a bit like razor). It's my first time using XSHD files so please forgive me if this is actually easy.

The issue I'm experiencing can be seen with the following example code:

Not C#

@{
    public void DoStuff() {
        // C# highlighting here
    }

    public void DoMoreStuff() {
        // C# highlighting here
    }
}

Within the @{ and } I need to apply C# syntax highlighting. So I am doing this in xshd: (simplified)

<Span ruleSet="RazorCode" multiline="true">
    <Begin color="CodeBlockStartEndTags">\@\{</Begin>
    <End color="CodeBlockStartEndTags">\}</End>
</Span>
<RuleSet name="RazorCode">
    <Import ruleSet="C#/" />
</RuleSet>

And this is what I am observing:

enter image description here

Clearly what's happening is the <End color="CodeBlockStartEndTags">\}</End> tag is ending the span as soon as it finds the very next closing brace }. When actually it should skip this brace because there is an open brace before it.

So I need it to count open braces and pair them up with the close braces, to actually end the span on the correct closing brace.

Is this possible in XSHD?

Thanks


Solution

  • The XML syntax highlighting keeps track of the stack of RuleSet activations. You can count braces by activating the RuleSet recursively:

    <Span ruleSet="RazorCode" multiline="true">
        <Begin color="CodeBlockStartEndTags">\@\{</Begin>
        <End color="CodeBlockStartEndTags">\}</End>
    </Span>
    <RuleSet name="RazorCode">
        <Import ruleSet="C#/" />
        <Span ruleSet="RazorCode" multiline="true">
            <Begin>\{</Begin>
            <End>\}</End>
        </Span>
    </RuleSet>