【问题标题】:How do I style user-defined function names in gedit?如何在 gedit 中设置用户定义函数名称的样式?
【发布时间】:2010-11-08 12:45:51
【问题描述】:

我正在尝试调整 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>

有什么提示吗?谢谢!

【问题讨论】:

  • 您希望def foo(a,b,c): 中的foo 以不同的颜色突出显示吗?这是我的理解,也是我所追求的。
  • 我刚刚注意到我假设您使用的是 Python。我敢肯定,无论您尝试使用哪种语言,您都可以提出类似的规则。

标签: linux gtk syntax-highlighting gedit


【解决方案1】:

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

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

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

然后您需要在&lt;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(可选) 如果此属性为“true”,则突出显示样式将应用于开始匹配和结束匹配之间的区域;否则将突出显示整个上下文。

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

重用现有样式而不是为类名定义新样式的好处是,它将适用于您将来安装的所有 gedit 主题 - 无需更改它们即可添加特定于 python 的功能名称部分。


编辑: 正如 cmets 中所指出的,这会破坏“def”上的样式。将“class-name”部分(我上面的代码)移动到已经存在的“keywords”部分下方,修复了这个问题,因为它将我们的更改移动到层次结构中的较低位置。有机会会更新图片。

之前:

之后:

【讨论】:

  • 不过,您会注意到,这会删除 'def' 关键字的突出显示。不太确定如何解决这个问题,但您可能想看看 Vim 或 Sublime Text 的语言定义。
  • @jozzas 提供的答案适用于 Gtksourceview 2.0,但请允许我为 3.0 版本免去您的头痛:
  • 您创建的 从 Gedit 主题 .xml 文件中调用您的样式
【解决方案2】:

如果我理解正确,首先你需要在 *.lang 文件中定义新样式

<style id="function" _name="Functions"/>

然后列出所有需要的功能,例如

<context id="functions" style-ref="function">
    <keyword>abs</keyword>
    <keyword>acos</keyword>
    <keyword>acosh</keyword>
</context>

然后更新文件末尾的块定义,并添加新的上下文

<context ref="functions"/>

它非常重要的正确顺序,上下文定义顺序在所有文件中应该是相同的,一个元素总是一个接一个,不要混淆它们。

最后你需要在你的样式/*.xml 文件中定义样式格式,在我的情况下是

<style name="php:function" foreground="#0000ee" bold="false"/>

我使用 php 的地方应该是您的语言 id,与 *.lang 文件中的相同。

【讨论】:

  • 不,您需要匹配“def”,因为我相信发帖人希望def foo(a,b,c): 中的foo 以不同的颜色突出显示。您无法匹配foo,因为用户可以随心所欲地调用函数。你需要一个正则表达式来捕获之后 def 和左括号( 之前的任何内容。我不知道该怎么做。我也想要这个,因为它有助于更​​轻松地在文件中查找函数。
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 2020-09-09
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多