Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/remote_plugin.txt`
7fcb41c29289fd77fbe40a203689e7635ba92c693c21a5a60000000100000820
 self.calls = 0

        @pynvim.command('Cmd', range='', nargs='*', sync=True)
        def command_handler(self, args, range):
            self._increment_calls()
            self.vim.current.line = (
                'Command: Called %d times, args: %s, range: %s' % (self.calls,
                                                                   args,
                                                                   range))

        @pynvim.autocmd('BufEnter', pattern='*.py', eval='expand("<afile>")',
                        sync=True)
        def autocmd_handler(self, filename):
            self._increment_calls()
            self.vim.current.line = (
                'Autocmd: Called %s times, file: %s' % (self.calls, filename))

        @pynvim.function('Func')
        def function_handler(self, args):
            self._increment_calls()
            self.vim.current.line = (
                'Function: Called %d times, args: %s' % (self.calls, args))

        def _increment_calls(self):
            if self.calls == 5:
                raise Exception('Too many calls!')
            self.calls += 1
<

As can be seen, the plugin is implemented using idiomatic Python (classes,
methods, and decorators). The translation between these language-specific
idioms to Vimscript occurs while the plugin manifest is being generated (see
the next section).

Notice that the exported command and autocmd are defined with the "sync" flag,
which affects how Nvim calls the plugin: with "sync" the |rpcrequest()|
function is used, which will block Nvim until the handler function returns a
value. Without the "sync" flag, the call is made using a fire and forget
approach with |rpcnotify()|, meaning return values or exceptions raised in the
handler function are ignored.

To test the above plugin, it must be saved in "rplugin/python" in a
'runtimepath' directory (~/.config/nvim/rplugin/python/limit.py for example).
Then, the remote plugin manifest must be generated with
|:UpdateRemotePlugins|.

==============================================================================

Title: Python Remote Plugin Example (Continued)
Summary
This section continues the Python remote plugin example, showing the implementation of a command, autocmd, and function. It highlights the use of decorators to define these elements and explains the significance of the 'sync' flag for controlling the call type (blocking vs. non-blocking). The section also details how to install and test the plugin by saving it to the appropriate directory and generating the remote plugin manifest.