3

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.

4

1 回答 1

3

似乎没有办法匹配子上下文中父上下文的开头(或结尾)。但是,如果您将特殊字符串定义为顶级上下文并将其包含在主要语言上下文中的字符串之前,它将起作用:

<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>
于 2016-01-12T10:03:14.050 回答