简体   繁体   中英

Autocomplete for gtk3 and pygobject

I am looking for a possibility to add autocompletion for gtk3 libs to vim. I have already tried vim-jedi as it seems the state of the art autocompletion mechanism for vim and python (also recommended here ), but that does not work for the gtk3 stuff wich is imported from the gi.repository

from gi.repository import Gtk, GdkPixbuf, Gdk, GObject

I took a look into the gi.repository site-packages. After that I am not suprised that jedi is not able to introsepect the methods of the Gtk modules, because these modules a generated there with a lot of magic ;-)

Additionally other "real" IDEs like PyCharm also does not support completion for the modules which were imported this way (i tried the Community Edition, but I would bet that the Professional Edition also does not support that).

Does anyone have an idea how to add autocompletion to vim (favored solution) or another Editor/IDE?

The GObject, Gtk modules contain quite a lot methods/classes and it's not easy to remebember everything you need. Furthermore there seems to be only documentation for the C api of gtk3, where the method names are not always the same as in python api...

Neocomplcache / Neocomplete能够完成自省的 GObject 库,但如果您自己没有针对 GTK+ 3 编译 Vim,这很可能会崩溃。在 Ubuntu 14.04 上,Vim 是针对 GTK+ 2 编译的,尝试导入和自动完成 GTK+ 3 库将失败,因为两个工具包不能同时运行。

I recently found a solution using fakegir . It builds a fake Python package from the information found in gir files (eg: /usr/share/gir-1.0/*.gir ). This can provide Gtk autocompletion to your LSP.

Their instructions are straightforward:

  • Run ./fakegir.py to generate the fake package to ~/.cache/fakegir/
  • Create a virtualenv ( mkvirtualenv fakegir or similar)
  • Enter virtualenv directory ( cdvirtualenv fakegir )
  • Enter ./lib/python*/site-packages directory
  • Copy package into the virtualenv: cp -a ~/.cache/fakegir/gi .

The virtualenv must be activated before editing your program in vim. The fake package must also be regenerated when you install new gir files or upgrade. I tested this in neovim v0.8.0-dev with python-lsp-server (pylsp) and suspect it will work for other language servers.

I also came across the stubgen tool provided by mypy which might be useful for generating type hints/signatures in other situations.

Both Jedi and PyCharm support PEP 484 .pyi type stubs. You can add these type annotation stubs for PyGObject to your editor using the PyGObject-stubs library:

python3 -m venv .venv
source .venv/bin/activate
pip install PyGObject-stubs

Both Jedi and PyCharm should pick them up automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM