Matching entire string in gtksourceview

2019-07-02 12:02发布

问题:

Take this short XML snippet for defining strings in gtksourceview:

<context id="string" style-ref="string" end-at-line-end="true">
    <start>"</start>
    <end>"</end>
    <include>
        <context id="special-string" style-ref="special">
            <match>(foo|bar)</match>
        </context>
        <context ref="def:line-continue"/>
    </include>
</context>

This highlights any instances of "foo" or "bar" inside a string. Suppose I want to highlight these two when they are the entire string, so that "foo" has the middle three characters highlighted but "foobar" is not highlighted at all. Is this possible in gtksourceview?

I tried using anchors ^...$ and lookahead/lookbehind (?<=") / (?=") but the former didn't work and the latter broke the syntax highlighter entirely.

回答1:

It seems like there is no way to match the beginning (or end) of the parent context in a subcontext. However, if you define special-string as a top-level context and include it before string in the main language context it will work:

<context id="special-string" style-ref="string">
    <match>"(foo|bar)"</match>
    <include>
        <context sub-pattern="1" style-ref="special"/>
    </include>
</context>

<context id="string" style-ref="string" end-at-line-end="true">
    <start>"</start>
    <end>"</end>
</context>