Thursday, 14 June 2012

Enhancing gedit for PL/SQL Development

Most text editors allow customisation of syntax highlighting and the like, and having recently moved to using gedit, thought i'd enhance it a bit. In a fresh installation, it already has reasonable support for oracle based keywords, but could do with a bit of an update.

The object that does the syntax highlighting is gtksourceview - depending on the version of gedit, either version 2 or 3. 

By going to the project page on gnome, you will see the howto for adding a new language: (Linux only) Since package source code is typically in files with the extension pks and pkb, it is first necessary to update the system to recognise that those files are of the mime type text/x-sql (or a custom one if you really want). So, create an xml file with the following:

<mime-info xmlns="">
  <mime-type type="text/x-sql">
    <comment>SQL Source including PL/SQL</comment>
    <glob pattern="*.sql"></glob>
    <glob pattern="*.pks"></glob>
    <glob pattern="*.pkb"></glob>

This can be saved in two locations. Either:

  • /usr/share/mime/packages
  • ~/.local/share/mime/packages
I saved mine into the former path. Once done, you need to update the mime database:

sudo update-mime-database /usr/share/mime

Now, when you open those two new file types in gedit, it will recognise them as text/x-sql files, and apply the correct language.

The next thing you'll notice, is that there are a few keywords that aren't having syntax highlighting applied. We can update this word list so particular words are highlighted accordingly. Make a backup of the sql.lang file and edit the sql.lang file to add new words:

sudo cp /usr/share/gtksourceview-3.0/language-specs/sql.lang /usr/share/gtksourceview-3.0/language-specs/sql.lang.backup1
sudo gedit /usr/share/gtksourceview-3.0/language-specs/sql.lang

The first section of the file lists the metadata for which files this configuration should apply to. Update the globs property to the file pattern as specified in the mime type configuration file above, so it should read:

    <property name="mimetypes">text/x-sql</property>
    <property name="globs">*.sql;*.pks;*.pkb</property>
    <property name="line-comment-start">--</property>

Then, we need a new context section with the list of words that should get highlighted. To do this, I ran the following query:

  '<keyword>' || keyword|| '</keyword>' keyword
  length(keyword) > 2
order by

I then exported that data, enclosed in the xml property:

<context id="oracle-updated-keywords" style-ref="keyword">

Added it to the file we already have opened (sql.lang), and updated the context with the same id as the language, so it has the entry within the include property (at the bottom of the config file). So it should be looking like:

<context id="sql" class="no-spell-check">
        <context ref="oracle-built-in-datatypes"/>
        <!-- Other contexts removed for readability of this article -->
        <context ref="oracle-updated-keywords"/>

Restart gedit and open a file with one of those extensions (or manually set the language) and it should now have those keywords highlighted.

More than likely, that list of keywords have been specified earlier in the language file - from what I can tell, gtksourceview takes the first style for a particular keyword it finds. This could obviously be extended further to include packages, procedures and functions.

It's worth noting, a custom language file could also be placed into: ~/.local/share/gtksourceview-3.0/language-specs.

I've chucked the files onto github:

Other language files:

No comments:

Post a Comment