linux - 如何在 gedit 中设置用户定义函数名称的样式?

标签 linux gtk syntax-highlighting 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="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>

有什么提示吗?谢谢!

最佳答案

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

您首先需要为要创建的样式 id 添加一个样式:

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

然后您需要在<context-id="python" 下向该文件添加一个新的上下文。部分:

<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>

您需要 style-inside="true"不对 def 应用样式或 (我们匹配的。来自文档:

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.

保存,然后重新启动 gedit,函数名称的样式应与错误相同,例如AttributeError的正文将是。您可以更改 map-to语言文件顶部的行以更改应用于函数名称的样式。

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


编辑: 如评论中所述,这会破坏“def”的样式。将“类名”部分(我上面的代码)移动到已经存在的“关键字”部分下方,修复此问题,因为它将我们的更改移到层次结构中的较低位置。有机会我会更新图片。

之前: enter image description here

之后: enter image description here

关于linux - 如何在 gedit 中设置用户定义函数名称的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4123932/

相关文章:

linux - 无法在 AWS 中进行 SSH

regex - journalctl使用正则表达式搜索元数据

c++ - 按下按钮时 Gtkmm scroll_event 不起作用

c - 如何删除 GtkEntryCompletion 的所有操作

PhpStorm 在项目中打开外部文件并进行检查和突出显示

eclipse - aptana/eclipse 中 scss 的 .scss.erb 扩展语法高亮

c - 如何在 ubuntu 11.10 上调用 setjmp?

c++ - 在线法官的安全问题

python - 如何将 Gtk.StatusIcon 设置为文本

c - VScode : Disable syntax highlighting for a range of lines