另一个Neovim的远程插件框架(neovim 插件)

网友投稿 1870 2022-10-11 18:49:00

另一个Neovim的远程插件框架(neovim 插件)

Yet Another Remote Plugin Framework for Neovim

This is my attempt on writing a remote plugin framework without :UpdateRemotePlugins.

Requirements

has('python3')For Vim 8: roxma/vim-hug-neovim-rpcg:python3_host_prog pointed to your python3 executable, or echo exepath('python3') is not empty.pynvim (pip3 install pynvim)

Use case

shougo/deoplete.nvimncm2/ncm2 and most of its plugins

Usage

pythonx/hello.py

import vim, timedef greet(): time.sleep(3) vim.command('echo "Hello world"')

plugin/hello.vim

" Create a python3 process running the hello module. The process is lazy load.let s:hello = yarp#py3('hello')com HelloSync call s:hello.request('greet')com HelloAsync call s:hello.notify('greet')" You could type :Hello greetcom -nargs=1 Hello call s:hello.request()

Debugging

Add logging settigns to your vimrc. Log files will be generated with prefix /tmp/nvim_log. An alternative is to export environment variables before starting vim/nvim.

let $NVIM_PYTHON_LOG_FILE="/tmp/nvim_log"let $NVIM_PYTHON_LOG_LEVEL="DEBUG"

Options

let s:yarp.on_load = function('your_handler')

A handler that is called after your python module is loaded. It is not considered ready for request or notification from vimscript during loading stage. s:yarp.request and s:yarp.notify internally wait until on_load before sending actual request and notification.

Methods

call s:yarp.reqeust({event}, [, {args}...])

Similiar to :help rpcrequest. It sends a request to the rpc channel and returns the result of your python method.

call s:yarp.rpcnotify({event}, [, {args}...])

Similiar to :help rpcnotify. It sends a notification to the rpc channel and returns immediately.

call s:yarp.error(msg)

Print error message.

call s:yarp.warn(msg)

Print warning message.

Example for existing neovim rplugin porting to Vim 8

More realistic examples could be found at nvim-typescript#84, deoplete#553, callmekohei/quickdebug.

Now let's consider the following simple rplugin.

After UpdateRemotePlugins and restarting neovim, you get foobar by :echo Bar().

# rplugin/python3/foo.pyimport pynvim@pynvim.pluginclass Foo(object): def __init__(self, vim): self._vim = vim @pynvim.function("Bar", sync=True) def bar(self, args): return 'hello' + str(args)

For working on Vim 8, you need to add these two files:

" plugin/foo.vimif has('nvim') finishendiflet s:foo = yarp#py3('foo_wrap')func! Bar(v) return s:foo.call('bar',a:v)endfunc

# pythonx/foo_wrap.pyfrom foo import Foo as _Fooimport vim_obj = _Foo(vim)def bar(*args): return _obj.bar(args)

How to use

$ vim: echo bar('world')hello('world',)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ViewPager 中 Fragment 状态保存的小知识
下一篇:隐私计算技术|私有信息检索(PIR)及其应用场景
相关文章