[PYTHON] Creating async plugins with neovim

I want to do asynchronous processing with vim

--Vim itself can't make another thread --Do your best with vimproc + autocmd --Callback management is difficult


Remote Plugin in neovim

--New features of neovim --Launch the plugin in a separate process --Neovim is safe even if the plugin process dies! --Mutual communication with msgpack-rpc (asynchronous) --In principle, you can write in any language! --Farewell to ʻif_python or ʻif_lua! !!


msgpack-RPC?

--msgpack ~ = Binary version of JSON


neovim server

import mprpc
c = mprpc.RPCClient("localhost", 6666)
_, info = c.call("vim_get_api_info")
for f in info["functions"]:
    print(f["name"], f["parameters"], f["return_type"])

I got a list of functions I can call:

buffer_line_count (('Buffer', 'buffer'),) Integer
buffer_get_line (('Buffer', 'buffer'), ('Integer', 'index')) String
buffer_set_line (('Buffer', 'buffer'), ('Integer', 'index'), ('String', 'line')) void
buffer_del_line (('Buffer', 'buffer'), ('Integer', 'index')) void
buffer_get_line_slice (('Buffer', 'buffer'), ('Integer', 'start'), ('Integer', 'end'), ('Boolean', 'include_start'), ('Boolean', 'include_end')) ArrayOf(String)
buffer_get_lines (('Buffer', 'buffer'), ('Integer', 'start'), ('Integer', 'end'), ('Boolean', 'strict_indexing')) ArrayOf(String)
buffer_set_line_slice (('Buffer', 'buffer'), ('Integer', 'start'), ('Integer', 'end'), ('Boolean', 'include_start'), ('Boolean', 'include_end'), ('ArrayOf(String)', 'replacement')) void
buffer_set_lines (('Buffer', 'buffer'), ('Integer', 'start'), ('Integer', 'end'), ('Boolean', 'strict_indexing'), ('ArrayOf(String)', 'replacement')) void
buffer_get_var (('Buffer', 'buffer'), ('String', 'name')) Object
buffer_set_var (('Buffer', 'buffer'), ('String', 'name'), ('Object', 'value')) Object
buffer_del_var (('Buffer', 'buffer'), ('String', 'name')) Object
buffer_get_option (('Buffer', 'buffer'), ('String', 'name')) Object
buffer_set_option (('Buffer', 'buffer'), ('String', 'name'), ('Object', 'value')) void
buffer_get_number (('Buffer', 'buffer'),) Integer
buffer_get_name (('Buffer', 'buffer'),) String
buffer_set_name (('Buffer', 'buffer'), ('String', 'name')) void
buffer_is_valid (('Buffer', 'buffer'),) Boolean
buffer_insert (('Buffer', 'buffer'), ('Integer', 'lnum'), ('ArrayOf(String)', 'lines')) void
buffer_get_mark (('Buffer', 'buffer'), ('String', 'name')) ArrayOf(Integer, 2)
buffer_add_highlight (('Buffer', 'buffer'), ('Integer', 'src_id'), ('String', 'hl_group'), ('Integer', 'line'), ('Integer', 'col_start'), ('Integer', 'col_end')) Integer
buffer_clear_highlight (('Buffer', 'buffer'), ('Integer', 'src_id'), ('Integer', 'line_start'), ('Integer', 'line_end')) void
tabpage_get_windows (('Tabpage', 'tabpage'),) ArrayOf(Window)
tabpage_get_var (('Tabpage', 'tabpage'), ('String', 'name')) Object
tabpage_set_var (('Tabpage', 'tabpage'), ('String', 'name'), ('Object', 'value')) Object
tabpage_del_var (('Tabpage', 'tabpage'), ('String', 'name')) Object
tabpage_get_window (('Tabpage', 'tabpage'),) Window
tabpage_is_valid (('Tabpage', 'tabpage'),) Boolean
vim_command (('String', 'str'),) void
vim_feedkeys (('String', 'keys'), ('String', 'mode'), ('Boolean', 'escape_csi')) void
vim_input (('String', 'keys'),) Integer
vim_replace_termcodes (('String', 'str'), ('Boolean', 'from_part'), ('Boolean', 'do_lt'), ('Boolean', 'special')) String
vim_command_output (('String', 'str'),) String
vim_eval (('String', 'str'),) Object
vim_call_function (('String', 'fname'), ('Array', 'args')) Object
vim_strwidth (('String', 'str'),) Integer
vim_list_runtime_paths () ArrayOf(String)
vim_change_directory (('String', 'dir'),) void
vim_get_current_line () String
vim_set_current_line (('String', 'line'),) void
vim_del_current_line () void
vim_get_var (('String', 'name'),) Object
vim_set_var (('String', 'name'), ('Object', 'value')) Object
vim_del_var (('String', 'name'),) Object
vim_get_vvar (('String', 'name'),) Object
vim_get_option (('String', 'name'),) Object
vim_set_option (('String', 'name'), ('Object', 'value')) void
vim_out_write (('String', 'str'),) void
vim_err_write (('String', 'str'),) void
vim_report_error (('String', 'str'),) void
vim_get_buffers () ArrayOf(Buffer)
vim_get_current_buffer () Buffer
vim_set_current_buffer (('Buffer', 'buffer'),) void
vim_get_windows () ArrayOf(Window)
vim_get_current_window () Window
vim_set_current_window (('Window', 'window'),) void
vim_get_tabpages () ArrayOf(Tabpage)
vim_get_current_tabpage () Tabpage
vim_set_current_tabpage (('Tabpage', 'tabpage'),) void
vim_subscribe (('String', 'event'),) void
vim_unsubscribe (('String', 'event'),) void
vim_name_to_color (('String', 'name'),) Integer
vim_get_color_map () Dictionary
vim_get_api_info () Array
window_get_buffer (('Window', 'window'),) Buffer
window_get_cursor (('Window', 'window'),) ArrayOf(Integer, 2)
window_set_cursor (('Window', 'window'), ('ArrayOf(Integer, 2)', 'pos')) void
window_get_height (('Window', 'window'),) Integer
window_set_height (('Window', 'window'), ('Integer', 'height')) void
window_get_width (('Window', 'window'),) Integer
window_set_width (('Window', 'window'), ('Integer', 'width')) void
window_get_var (('Window', 'window'), ('String', 'name')) Object
window_set_var (('Window', 'window'), ('String', 'name'), ('Object', 'value')) Object
window_del_var (('Window', 'window'), ('String', 'name')) Object
window_get_option (('Window', 'window'), ('String', 'name')) Object
window_set_option (('Window', 'window'), ('String', 'name'), ('Object', 'value')) void
window_get_position (('Window', 'window'),) ArrayOf(Integer, 2)
window_get_tabpage (('Window', 'window'),) Tabpage
window_is_valid (('Window', 'window'),) Boolean

msgpack-RPC Can I write without knowing?

That's not the case: SDKs for each language are being developed


Make a plugin with neovim / python-client

The main subject is from here. I made toggl.nvim and summarized what I checked (although it was in the middle).

--Put the file in $ runtimepath / rplugin / python3 / --When you put a module, it doesn't work well unless you put an empty file.

rplugin/python3/toggl/...
                toggl.py  #Empty file

--Documents less --Refer to deoplete.nvim --View the source of python-client (relatively easy to see)

--Neovim / python-client will do all the RPC related work | ω ・)


How to use python-client

Excerpt from toggl.nvim

@neovim.plugin
class Toggl(object):

    def __init__(self, nvim):
        self.nvim = nvim
        self.api_token = nvim.eval("g:toggl_api_token")
        self.api = TogglAPI(self.api_token)

    @neovim.autocmd("VimEnter")
    def update(self):
        try:
            self.wid = self.api.workspaces()[0]["id"]
            self.projects = self.get_projects([])
        except ConnectionError:
            self.echo("No network, toggl.nvim is disabled.")

    def echo(self, msg):
        self.nvim.command("echo '{}'".format(msg))

    @neovim.function("TogglAPIToken", sync=True)
    def api_token(self, args):
        return self.api_token

    @neovim.function("TogglGetCurrent", sync=True)
    def get_current(self, args):
        return self.api.time_entries.current()

You can write a vim plugin just by defining a class + function with a decorator

--You can get an nvim instance by adding neovim.plugin to the class. --You can issue autocmd with neovim.autocmd --Neovim.command can be used to create vim commands (arguments are automatically converted to Python variables) --Neovim.function can be used to create vim functions --Since the plug-in side is a separate process, you can freely have the state --Apply both synchronous (sync = True) and asynchronous functions (sync = False, default) in the same way --Python library can be used (requests)

I use Python, but vim script is a little ... Recommended for people!


Finally

--Qiita slide Easy and good --osaka.vim fun

Recommended Posts

Creating async plugins with neovim
Creating GUI tools with pyinstaller
Creating an egg with python
Python: How to use async with
Creating a decision tree with scikit-learn
Creating a simple app with flask
[Python] Asynchronous request with async / await
Async / await with Kivy and tkinter
Use Python in pyenv with NeoVim
Commands for creating SNS with Django
[Python] Creating multiple windows with Tkinter