Gtksourceview2: implement completion provider

The documentation says about SourceCompletionProvider :

 You must implement this interface to provide proposals to SourceCompletion 

What does “implement this interface” mean in context?

Create a new GObject using this ? Any example ?.

When I have a new GObject , how can I add functions to it (for example, sourceCompletionProviderGetName )?

thanks

+3
source share
1 answer

gtk + is written in C, however it uses glib to create an object-oriented style interface. Most gtk + APIs are defined using interfaces in the sense of OO. The actual functionality of gtk + is provided by the “objects” (pointers to structures that follow the glib conventions) that implement these interfaces. Thus, “implementing this interface” means exactly this: write a “class” (in C through the glib system) that implements the desired interface (which you do by setting up the correct glib hooks to handle certain function calls).

In gtk2hs, glib interfaces translate as class classes with the word "class" added. SourceCompletionProviderClass is one of these classes and is designed to accurately represent the glib interface. Unfortunately, the “cool methods” on the C side are not displayed in Haskell. The only method that provides a class type is the casting operation, and this is only to bypass the mismatch between a Haskell-type system and OOP inheritance trees. The glib class method is translated into a regular Haskell function that works with some data that can be selected by the corresponding type. This means that there is no good way to implement a new instance of the class via gtk2hs.

Although theoretically you could train the necessary behavior on the part of Haskell by creating a new GObject, it is very likely that not all the necessary functions (you will need material with a low level of glib) will be exposed, so you will need to link them yourself (I seem to recall that the accompanying gtk2hs post something like this a year or two ago, but cannot find the link now). At this point, it is probably less error prone to implement all this in C on its own. I have code that does this for the cellrenderer interface , which you may find useful as a model. Most interesting things happen in the C header file. You can still assign Haskell functions to the C side using a function pointer.

If you really want to do this completely from Haskell, I would start by looking for a pretty simple interface (like my cellrenderer function or another SourceCompletionProvider ) and running the header through the preprocessor to see what glib macros translate to. Then you can see the necessary definitions for setting up the glib object. The most important functions are * _init, * _finalize, * _get_property and * _set_property, although others are needed. * is a placeholder for the name of your object. IIRC, most functions are configured by default with the G_DEFINE_TYPE macro, which also sets the name prefix.

+5
source

Source: https://habr.com/ru/post/1435349/


All Articles