gtkgtk3gtksourceview

How to use custom language highlight syntax in Gtk SourceView?


I'm trying to create my own language definition, and use it for highlighting the syntax in my app.

The issue I have is that, when trying to access the language definition from my app's data folder (/usr/share/myapp/), even using the c.lang file copied from /usr/share/gtksourceview-3.0/language-specs/, just to test, I get this error at runtime:

GtkSourceView-WARNING **: Failed to load '/usr/share/myapp/c.lang': could not find the RelaxNG schema file

So it's asking for some schema file? So I went forward and copied every file from the language-specs folder that isn't a lang file, which includes: language.dtd, language.rng and language2.rng.

Now, when I run again, I get these errors:

GtkSourceView-WARNING **: in file /usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang: style 'def:comment' not defined

GtkSourceView-WARNING **: Failed to load '/usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang': unable to resolve language 'def'

What does that even mean? Is that something namespace-related? It is very peculiar, because the exact same file is working perfectly when in gtksourceview folder.

Any clues? Do these "RelaxNG" files work only in their original directories? If so, how can I change that? I've looked into the files, but failed to find any reference to their original folder...

This is the source-code (in Vala) related to the issue:

var manager = Gtk.SourceLanguageManager.get_default ();
string search_paths[] = {"/usr/share/myapp", null};
manager.set_search_path (search_paths);
var buffer = new Gtk.SourceBuffer.with_language (manager.get_language ("c"));

The application does in fact find the language "c", I've checked by debugging.


Solution

  • You have to preserve the Gtk.SourceLanguageManager's original search path when you add your own. Append to it instead of replacing it.