gtksourceview2:实现完成提供程序 [英] gtksourceview2: implement completion provider

查看:144
本文介绍了gtksourceview2:实现完成提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档指出 SourceCompletionProvider
$ b

 您必须执行这个接口向SourceCompletion提供建议

在实现上下文时,实现这个接口是什么意思? p>

我应该使用 GObject /glib/0.12.3.1/doc/html/System-Glib-GObject.htmlrel =nofollow>这个函数?任何示例?。



当我有新的 GObject 时,我该如何添加函数(如 sourceCompletionProviderGetName )到它?



谢谢

解决方案 gtk +是用C编写的,但它使用 glib 来提供一个面向对象的风格界面。大多数gtk + API都是使用OO意义上的接口定义的。实际的gtk +功能由实现这些接口的对象(指向遵循glib约定的结构的指针)提供。所以实现这个接口的意思就是 - 写一个实现所需接口的类(通过glib系统在C中)(你通过设置合适的glib钩子来处理特定的函数调用)。



在gtk2hs中,glib接口被翻译为类型类,并在末尾添加单词class。 SourceCompletionProviderClass 是其中的一个类,用于准确表示glib接口。不幸的是,C端类方法在Haskell中并未真正公开。类类提供的唯一方法是转换操作,这只是解决Haskell类型系统和OOP继承树之间的不匹配问题。 glib-class-method被转换为一个常规的Haskell函数,该函数对一些可以转换为适当类型的数据进行操作。这意味着没有好的方法来通过gtk2hs来实现一个新的类实例。



尽管从理论上讲,您可能通过创建Haskell方面来强化必要的行为一个新的GObject,很可能并不是所有必要的函数(你需要一些低级的glib东西)都暴露出来,所以你需要自己绑定它们(我似乎回忆起gtk2hs的维护者发布这样一年的东西或两个前,但现在找不到链接)。在那个时候,它可能不太容易出错,你自己用C来实现它。我有一个代码库,可以在 cellrenderer接口上执行此操作,您可能会将其用作模型。大部分有趣的东西都发生在C头文件中。你仍然可以通过函数指针设置从C端调用的Haskell函数。



如果你真的想试图完全从Haskell完成,我会先从找到一个相当简单的接口(比如我的cellrenderer函数,或者另一个 SourceCompletionProvider )并通过preprocesser运行头来查看glib宏转换的内容。然后你可以看到设置glib对象的必要定义。最重要的功能是* _init,* _finalize,* _get_property和* _set_property,但也需要一些其他功能。 * 是您的对象名称的占位符。 IIRC,大部分默认功能都是由 G_DEFINE_TYPE 宏设置的,它也设置了名称前缀。


The documentation states about SourceCompletionProvider:

You must implement this interface to provide proposals to SourceCompletion

What does "implement this interface" means in the context?

Should I create new GObject using this functions? Any example?.

When I have new GObject, then how can I add functions (like sourceCompletionProviderGetName) to it?

Thanks

解决方案

gtk+ is written in C, however it uses glib to provide an object-oriented style interface. Much of the gtk+ API is defined using interfaces in the OO sense. The actual gtk+ functionality is provided by "objects" (pointers to structs that follow the glib conventions) that implement those interfaces. So "implement this interface" means exactly that - write a "class" (in C via the glib system) that implements the desired interface (which you do by setting up the proper glib hooks to handle a specific function calls).

In gtk2hs, glib interfaces are translated as type classes with the word "class" appended to the end. SourceCompletionProviderClass is one of these classes, and is meant to exactly represent the glib interface. Unfortunately, the C-side "class methods" aren't really exposed in Haskell. The only method the type class provides is a casting operation, and that is only to work around a mismatch between the Haskell type system and OOP inheritance trees. A glib-class-method is translated to a regular Haskell function operating on some data that can be casted to the appropriate type. This means that there's no good way to implement a new class instance via gtk2hs.

Although in theory you might be able to shoehorn the necessary behavior in from the Haskell side by creating a new GObject, it's highly likely that not all necessary functions (you'd need some low-level glib stuff) are exposed, so you'd need to bind them yourself (I seem to recall the gtk2hs maintainers posting something like this a year or two ago, but can't find the link now). At that point, it's probably less error-prone to implement it all in C yourself. I have a codebase that does this for the cellrenderer interface, which you might find useful as a model. Most of the interesting stuff happens in the C header file. You can still set Haskell functions to be called from the C side via a function pointer.

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

这篇关于gtksourceview2:实现完成提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆