1 """Decorators used by python host plugin system."""
6 from pynvim.compat import IS_PYTHON3, unicode_errors_default
8 logger = logging.getLogger(__name__)
9 debug, info, warn = (logger.debug, logger.info, logger.warning,)
10 __all__ = ('plugin', 'rpc_export', 'command', 'autocmd', 'function',
11 'encoding', 'decode', 'shutdown_hook')
15 """Tag a class as a plugin.
17 This decorator is required to make the class methods discoverable by the
18 plugin_load method of the host.
20 cls._nvim_plugin = True
21 # the _nvim_bind attribute is set to True by default, meaning that
22 # decorated functions have a bound Nvim instance as first argument.
23 # For methods in a plugin-decorated class this is not required, because
24 # the class initializer will already receive the nvim object.
25 predicate = lambda fn: hasattr(fn, '_nvim_bind')
26 for _, fn in inspect.getmembers(cls, predicate):
30 fn.im_func._nvim_bind = False
34 def rpc_export(rpc_method_name, sync=False):
35 """Export a function or plugin method as a msgpack-rpc request handler."""
37 f._nvim_rpc_method_name = rpc_method_name
38 f._nvim_rpc_sync = sync
40 f._nvim_prefix_plugin_path = False
45 def command(name, nargs=0, complete=None, range=None, count=None, bang=False,
46 register=False, sync=False, allow_nested=False, eval=None):
47 """Tag a function or plugin method as a Nvim command handler."""
49 f._nvim_rpc_method_name = 'command:{}'.format(name)
50 f._nvim_rpc_sync = sync
52 f._nvim_prefix_plugin_path = True
57 opts['range'] = '' if range is True else str(range)
58 elif count is not None:
71 opts['complete'] = complete
76 if not sync and allow_nested:
91 def autocmd(name, pattern='*', sync=False, allow_nested=False, eval=None):
92 """Tag a function or plugin method as a Nvim autocommand handler."""
94 f._nvim_rpc_method_name = 'autocmd:{}:{}'.format(name, pattern)
95 f._nvim_rpc_sync = sync
97 f._nvim_prefix_plugin_path = True
106 if not sync and allow_nested:
121 def function(name, range=False, sync=False, allow_nested=False, eval=None):
122 """Tag a function or plugin method as a Nvim function handler."""
124 f._nvim_rpc_method_name = 'function:{}'.format(name)
125 f._nvim_rpc_sync = sync
127 f._nvim_prefix_plugin_path = True
132 opts['range'] = '' if range is True else str(range)
137 if not sync and allow_nested:
152 def shutdown_hook(f):
153 """Tag a function or method as a shutdown hook."""
154 f._nvim_shutdown_hook = True
159 def decode(mode=unicode_errors_default):
160 """Configure automatic encoding/decoding of strings."""
162 f._nvim_decode = mode
167 def encoding(encoding=True):
168 """DEPRECATED: use pynvim.decode()."""
169 if isinstance(encoding, str):
173 f._nvim_decode = encoding