remote_plugin.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. *remote_plugin.txt* Nvim
  2. NVIM REFERENCE MANUAL by Thiago de Arruda
  3. Nvim support for remote plugins *remote-plugin*
  4. Type |gO| to see the table of contents.
  5. ==============================================================================
  6. 1. Introduction *remote-plugin-intro*
  7. Extensibility is a primary goal of Nvim. Any programming language may be used
  8. to extend Nvim without changes to Nvim itself. This is achieved with remote
  9. plugins, coprocesses that have a direct communication channel (via |RPC|) with
  10. the Nvim process.
  11. Even though these plugins run in separate processes they can call, be called,
  12. and receive events just as if the plugin's code were executed in the main
  13. process.
  14. ==============================================================================
  15. 2. Plugin hosts *remote-plugin-hosts*
  16. While plugins can be implemented as arbitrary programs that communicate
  17. directly with the high-level Nvim API and are called via |rpcrequest()| and
  18. |rpcnotify()|, that is not the best approach. Instead, developers should first
  19. check whether a plugin host is available for their chosen programming language.
  20. Plugin hosts are programs that provide a high-level environment for plugins,
  21. taking care of most boilerplate involved in defining commands, autocmds, and
  22. functions implemented over |RPC| connections. Hosts are loaded only when one
  23. of their registered plugins require it, keeping Nvim's startup as fast as
  24. possible, even if many plugins/hosts are installed.
  25. ==============================================================================
  26. 3. Example *remote-plugin-example*
  27. The best way to learn about remote plugins is with an example, so let's see
  28. what a Python plugin looks like. This plugin exports a command, a function, and
  29. an autocmd. The plugin is called 'Limit', and all it does is limit the number
  30. of requests made to it. Here's the plugin source code: >python
  31. import pynvim
  32. @pynvim.plugin
  33. class Limit(object):
  34. def __init__(self, vim):
  35. self.vim = vim
  36. self.calls = 0
  37. @pynvim.command('Cmd', range='', nargs='*', sync=True)
  38. def command_handler(self, args, range):
  39. self._increment_calls()
  40. self.vim.current.line = (
  41. 'Command: Called %d times, args: %s, range: %s' % (self.calls,
  42. args,
  43. range))
  44. @pynvim.autocmd('BufEnter', pattern='*.py', eval='expand("<afile>")',
  45. sync=True)
  46. def autocmd_handler(self, filename):
  47. self._increment_calls()
  48. self.vim.current.line = (
  49. 'Autocmd: Called %s times, file: %s' % (self.calls, filename))
  50. @pynvim.function('Func')
  51. def function_handler(self, args):
  52. self._increment_calls()
  53. self.vim.current.line = (
  54. 'Function: Called %d times, args: %s' % (self.calls, args))
  55. def _increment_calls(self):
  56. if self.calls == 5:
  57. raise Exception('Too many calls!')
  58. self.calls += 1
  59. <
  60. As can be seen, the plugin is implemented using idiomatic Python (classes,
  61. methods, and decorators). The translation between these language-specific
  62. idioms to Vimscript occurs while the plugin manifest is being generated (see
  63. the next section).
  64. Notice that the exported command and autocmd are defined with the "sync" flag,
  65. which affects how Nvim calls the plugin: with "sync" the |rpcrequest()|
  66. function is used, which will block Nvim until the handler function returns a
  67. value. Without the "sync" flag, the call is made using a fire and forget
  68. approach with |rpcnotify()|, meaning return values or exceptions raised in the
  69. handler function are ignored.
  70. To test the above plugin, it must be saved in "rplugin/python" in a
  71. 'runtimepath' directory (~/.config/nvim/rplugin/python/limit.py for example).
  72. Then, the remote plugin manifest must be generated with
  73. |:UpdateRemotePlugins|.
  74. ==============================================================================
  75. 4. Remote plugin manifest *remote-plugin-manifest*
  76. *:UpdateRemotePlugins*
  77. Just installing remote plugins to "rplugin/{host}" isn't enough for them to be
  78. automatically loaded when required. You must execute |:UpdateRemotePlugins|
  79. every time a remote plugin is installed, updated, or deleted.
  80. |:UpdateRemotePlugins| generates the remote plugin manifest, a special
  81. Vimscript file containing declarations for all Vimscript entities
  82. (commands/autocommands/functions) defined by all remote plugins, with each
  83. entity associated with the host and plugin path.
  84. Manifest declarations are just calls to the `remote#host#RegisterPlugin`
  85. function, which takes care of bootstrapping the host as soon as the declared
  86. command, autocommand, or function is used for the first time.
  87. The manifest generation step is necessary to keep Nvim's startup fast in
  88. situations where a user has remote plugins with different hosts. For example,
  89. say a user has three plugins, for Python, Java and .NET hosts respectively. If
  90. we were to load all three plugins at startup, then three language runtimes
  91. would also be spawned, which could take seconds!
  92. With the manifest, each host will only be loaded when required. Continuing with
  93. the example, say the Java plugin is a semantic completion engine for Java code.
  94. If it defines the autocommand `BufEnter *.java`, then the Java host is spawned
  95. only when Nvim loads a buffer matching "*.java".
  96. If the explicit call to |:UpdateRemotePlugins| seems inconvenient, try to see
  97. it like this: It's a way to provide IDE capabilities in Nvim while still
  98. keeping it fast and lightweight for general use. It's also analogous to the
  99. |:helptags| command.
  100. *$NVIM_RPLUGIN_MANIFEST*
  101. Unless $NVIM_RPLUGIN_MANIFEST is set the manifest will be written to a file
  102. named `rplugin.vim` at:
  103. Unix ~
  104. $XDG_DATA_HOME/nvim/ or ~/.local/share/nvim/
  105. Windows ~
  106. $LOCALAPPDATA/nvim/ or ~/AppData/Local/nvim/
  107. ==============================================================================
  108. vim:tw=78:ts=8:noet:ft=help:norl: