如何在gedit中设置用户定义的函数名称? [英] How do I style user-defined function names in gedit?

查看:154
本文介绍了如何在gedit中设置用户定义的函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调整gedit风格,以便用户定义的函数具有不同的颜色。



我已经通过 http://library.gnome.org/devel/gtksourceview-2.0/stable/lang-reference。 html ,但我找不到任何东西。



我以为< style name =def:function/> 可能会这样做,但它似乎对gedit没有任何影响。

 <?xml version = 1.0?> 
< style-scheme id =wombatname =Wombatversion =1.0>
< author />
< _description> Wombat主题< / _ description>
< style background =#2d2d2dname =current-line/>
< style background =#857b6fbold =trueforeground =#fff000name =bracket-match/>
< style background =#242424bold =trueforeground =#fff000name =search-match/>
< style background =#656565name =cursor/>
< style background =#242424foreground =#f6f3e8name =text/>
< style background =#272727foreground =#857b6fname =line-numbers/>
< style foreground =#363636italic =truename =def:comment/>
< style foreground =#e5786dname =def:constant/>
< style foreground =#95e454italic =truename =def:string/>
< style foreground =#cae682name =def:identifier/>
< style foreground =#000000name =def:function/>
< style foreground =#cae682name =def:type/>
< style foreground =#8ac6f2name =def:statement/>
< style foreground =#8ac6f2name =def:keyword/>
< style foreground =#e5786dname =def:preprocessor/>
< style foreground =#e5786dname =def:number/>
< style foreground =#e7f6daname =def:specials/>
< / style-scheme>

任何提示?谢谢!

解决方案

您需要编辑语言定义文件以添加新的部分。语言定义是 python.lang ,对于我来说,在 /usr/share/gtksourceview-2.0/language-specs

您首先需要为您要创建的样式ID添加样式:


 < style id =class-name_name =Python Class Namemap-to =def:type/> 


然后,您需要在该文件下添加一个新的上下文< context-id =python section:

 < context id =class-namestyle-ref =class-namestyle-inside =true> 
<开始> \%{字符串前缀} def \< / start>
< end> \(< / end>
< include>
< context ref =python/>
< context ref =format />
< / context>
< / context>
< / code>
<

您需要 style-inside =true不将样式应用于 def ,我们匹配。
style-inside (可选)
如果此属性为true,则突出显示样式将应用于开始和结束匹配之间的区域;否则整个上下文将突出显示。


保存该信息,然后重新启动gedit,函数名称应该与错误相同,例如文本 AttributeError 就可以了,你可以改变语言文件顶部的 map-to 行来改变样式应用于你的函数名称。



重用现有样式而不是为类名定义新样式的好处是,它可以用于将来安装的所有gedit主题 - 它们不需要修改以添加特定于python的函数名称部分。




编辑:
正如评论中所述,这样可以在def上造型。将class-name部分(我的代码如上)移动到已经存在的keywords部分下方,修正了这个问题,因为它将层次结构中的变化降低了。当我有机会的时候会更新图片。



之前:



之后:


I'm trying to tweak a gedit style so that user-defined functions have a different colour.

I've searched through http://library.gnome.org/devel/gtksourceview-2.0/stable/lang-reference.html but I couldn't find anything.

I thought <style name="def:function" /> might do it, but it seems have no effect in gedit.

<?xml version="1.0" ?>
<style-scheme id="wombat" name="Wombat" version="1.0">
        <author/>
        <_description>Wombat theme</_description>
        <style background="#2d2d2d" name="current-line"/>
        <style background="#857b6f" bold="true" foreground="#fff000" name="bracket-match"/>
        <style background="#242424" bold="true" foreground="#fff000" name="search-match"/>
        <style background="#656565" name="cursor"/>
        <style background="#242424" foreground="#f6f3e8" name="text"/>
        <style background="#272727" foreground="#857b6f" name="line-numbers"/>
        <style foreground="#363636" italic="true" name="def:comment"/>
        <style foreground="#e5786d" name="def:constant"/>
        <style foreground="#95e454" italic="true" name="def:string"/>
        <style foreground="#cae682" name="def:identifier"/>
        <style foreground="#000000" name="def:function"/>
        <style foreground="#cae682" name="def:type"/>
        <style foreground="#8ac6f2" name="def:statement"/>
        <style foreground="#8ac6f2" name="def:keyword"/>
        <style foreground="#e5786d" name="def:preprocessor"/>
        <style foreground="#e5786d" name="def:number"/>
        <style foreground="#e7f6da" name="def:specials"/>
    </style-scheme>

Any hints? Thanks!

解决方案

You need to edit the language definition file to add a new section. The language definition is python.lang and for me is in /usr/share/gtksourceview-2.0/language-specs.

You first need to add a style for the style id you're going to create:

<style id="class-name"    _name="Python Class Name"  map-to="def:type"/>

You then need to add a new context to this file under the <context-id="python" section:

<context id="class-name" style-ref="class-name" style-inside="true">
    <start>\%{string-prefix}def\ </start>
    <end>\(</end>
    <include>
        <context ref="python"/>
        <context ref="format"/>
        <context ref="escaped-char"/>
    </include>
</context>

You need the style-inside="true" to not apply styling to the def or ( that we match on. From the docs:

style-inside (optional) If this attribute is "true", then the highlighting style will be applied to the area between start and end matches; otherwise whole context will be highlighted.

Save that, then restart gedit and the function name should be styled the same as an error, e.g. the text of AttributeError would be. You can change the map-to line at the top of the language file to change the style applied to your function names.

The benefit of reusing an existing style rather than defining a new one for class names is that it will work for all of the gedit themes you install in future - they don't need to be altered to add a python-specific function name section.


Edit: As noted in the comments, this nukes the styling on "def". Moving the "class-name" section (my code above) to below the "keywords" section that already exists, fixes this as it moves our change lower in the hierarchy. Will update the image when I get a chance.

Before:

After:

这篇关于如何在gedit中设置用户定义的函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆