Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autocmd KeyError #467

Open
levouh opened this issue Nov 17, 2020 · 0 comments
Open

autocmd KeyError #467

levouh opened this issue Nov 17, 2020 · 0 comments

Comments

@levouh
Copy link

levouh commented Nov 17, 2020

Problem

I'm trying to use an autocmd via the remote-plugin documentation, and running into errors.

$ python -V
Python 3.8.6
$ pip -V
pip 19.3.1 from /usr/lib/python3.8/site-packages/pip (python 3.8) 
$ pip list installed | grep -i pynvim
pynvim          0.4.2 
$ nvim --version
NVIM v0.5.0-812-gd17e50879
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-5 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/travis/build/neovim/bot-ci/build/neovim/build/config -I/home/travis/build/neovim/bot-ci/build/neovim/src -I/home/travis/build/neovim/bot-ci/build/neovim/.deps/usr/include -I/usr/include -I/home/travis/build/neovim/bot-ci/build/neovim/build/src/nvim/auto -I/home/travis/build/neovim/bot-ci/build/neovim/build/include
Compiled by travis@travis-job-317a2489-aba5-414a-b9f3-9551746c326c

Setup

Looking at the generated manifest file /home/user/.local/share/nvim/rplugin.vim:

" perl plugins


" node plugins


" python3 plugins
call remote#host#RegisterPlugin('python3', '/home/user/.config/nvim/rplugin/python3/pluginctl.py', [
      \ {'sync': v:true, 'name': 'Plugin', 'type': 'command', 'opts': {'nargs': '+'}},
      \ {'sync': v:false, 'name': 'PluginClean', 'type': 'command', 'opts': {'nargs': '0'}},
      \ {'sync': v:false, 'name': 'PluginInstall', 'type': 'command', 'opts': {'bang': '', 'nargs': '0'}},
     \ ])
call remote#host#RegisterPlugin('python3', '/home/user/.config/nvim/rplugin/python3/terminalctl.py', [
      \ {'sync': v:true, 'name': 'TabNew', 'type': 'autocmd', 'opts': {'pattern': '*'}},
      \ {'sync': v:true, 'name': 'TerminalToggle', 'type': 'command', 'opts': {'nargs': '0'}},
     \ ])


" ruby plugins


" python plugins

and then loading up a ~/.config/nvim/init.vim which only contains:

runtime! plugin/rplugin.vim | silent! UpdateRemotePlugins

and the relevant parts of /home/user/.config/nvim/rplugin/python3/terminalctl.py:

from pynvim import (
    autocmd,
    command,
    plugin,
)


@plugin
class TerminalCtl(object):
    def __init__(self, nvim):
        self.nvim = nvim

    @command('TerminalToggle', nargs='0', sync=True)
    def _terminal_toggle(
            self,
            args
        ):
       pass

    @autocmd('TabNew', pattern='*', sync=True)
    def _tab_id(
            self,
        ):
        pass

which is a pretty simplistic example. We can also see that Neovim recognizes the autocmd internally:

:autocmd TabNew                                                                                   
--- Autocommands ---
RPC_DEFINE_AUTOCMD_GROUP_1  TabNew
    *         call remote#define#AutocmdBootstrap("python3", "/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*", v:true, "Tab
New", {'group': 'RPC_DEFINE_AUTOCMD_GROUP_1', 'pattern': '*'}, "\"doau RPC_DEFINE_AUTOCMD_GROUP_1 TabNew \".fnameescape(expand(\"<amatch>\"))")

Note that the commands work just fine, it is the autocmds that fail.

Traceback

When I try to run :tabnew, triggering the autocmd, I get the following stack trace:

Error detected while processing function remote#define#request:                                                                                  
line    2:
error caught in request handler '/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:* []'
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pynvim/plugin/host.py", line 129, in _on_request
    rv = handler(*args)
  File "/usr/local/lib/python3.8/site-packages/pynvim/plugin/host.py", line 95, in _wrap_delayed_function
    self._request_handlers[name](*args)
KeyError: '/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*'
Error invoking '/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*' on channel 4 (python3-rplugin-host):
KeyError('/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*')
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pynvim/msgpack_rpc/session.py", line 200, in handler
    rv = self._request_cb(name, args)
  File "/usr/local/lib/python3.8/site-packages/pynvim/api/nvim.py", line 210, in filter_request_cb
    result = request_cb(name, args)
  File "/usr/local/lib/python3.8/site-packages/pynvim/plugin/host.py", line 129, in _on_request
    rv = handler(*args)
  File "/usr/local/lib/python3.8/site-packages/pynvim/plugin/host.py", line 95, in _wrap_delayed_function
    self._request_handlers[name](*args)

so we are getting a KeyError for /home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:* in the file /usr/local/lib/python3.8/site-packages/pynvim/plugin/host.py on line 95. If I open this file, I can do something like:

        if sync:
            with open('/home/user/foo', 'w') as fd:
                fd.write(str(self._request_handlers))

            self._request_handlers[name](*args)
        else:
            self._notification_handlers[name](*args)

which will then write the string to /home/user/foo, opening it:

{
    'poll': <function Host.__init__.<locals>.<lambda> at 0x7fad25255430>,
    'specs': <bound method Host._on_specs_request of <pynvim.plugin.host.Host object at 0x7fad25252e20>>,
    'shutdown': <bound method Host.shutdown of <pynvim.plugin.host.Host object at 0x7fad25252e20>>,
    '/home/user/.config/nvim/rplugin/python3/pluginctl.py:command:Plugin': functools.partial(<bound method Host._wrap_function of <pynvim.plugin.host.Host object at 0x7fad25252e20>>, <bound method PluginCtl._plugin_add of <pluginctl.PluginCtl object at 0x7fad24d123a0>>, True, True, None, '/home/user/.config/nvim/rplugin/python3/pluginctl.py:command:Plugin'),
    '/home/user/.config/nvim/rplugin/python3/terminalctl.py:command:TerminalToggle': functools.partial(<bound method Host._wrap_function of <pynvim.plugin.host.Host object at 0x7fad25252e20>>, <bound method TerminalCtl._terminal_toggle of <terminalctl.TerminalCtl object at 0x7fad24d12940>>, True, True, None, '/home/user/.config/nvim/rplugin/python3/terminalctl.py:command:TerminalToggle')
}

so we can see that the autocmd just isn't stored. I can change the decorator to specify sync=False, and see the same thing, but a different flavor:

Error detected while processing function remote#define#request:                                                                                   
line    2:
Error invoking '/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*' on channel 4 (python3-rplugin-host):
no request handler registered for "/home/user/.config/nvim/rplugin/python3/terminalctl.py:autocmd:TabNew:*"

Fin

So what am I missing here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant