How do I style user-defined function names in gedi

2019-07-04 18:44发布

问题:

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!

回答1:

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:



回答2:

if i understand you correctly first of all you need define new style in your *.lang file

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

then make list of all needed functions like

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

then update block definitions at the end of the file, and add new context

<context ref="functions"/>

its very important correct order, context definition order should bee the same in all file, one element allways gos after another, dont mix them up.

and at the end you need to define style formmat in your styles/*.xml file, in mine case it was

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

where i used php should be your language id, same as in *.lang file.