Matching entire string in gtksourceview

狂风中的少年 提交于 2019-12-23 16:08:01

问题


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>


来源:https://stackoverflow.com/questions/34237471/matching-entire-string-in-gtksourceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!