1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
"""Decorators used by python host plugin system."""
import inspect
import logging
import sys
from typing import Any, Callable, Dict, Optional, TypeVar, Union
from pynvim.compat import unicode_errors_default
if sys.version_info < (3, 8):
from typing_extensions import Literal
else:
from typing import Literal
logger = logging.getLogger(__name__)
debug, info, warn = (logger.debug, logger.info, logger.warning,)
__all__ = ('plugin', 'rpc_export', 'command', 'autocmd', 'function',
'encoding', 'decode', 'shutdown_hook')
T = TypeVar('T')
F = TypeVar('F', bound=Callable[..., Any])
def plugin(cls: T) -> T:
"""Tag a class as a plugin.
This decorator is required to make the class methods discoverable by the
plugin_load method of the host.
"""
cls._nvim_plugin = True # type: ignore[attr-defined]
# the _nvim_bind attribute is set to True by default, meaning that
# decorated functions have a bound Nvim instance as first argument.
# For methods in a plugin-decorated class this is not required, because
# the class initializer will already receive the nvim object.
predicate = lambda fn: hasattr(fn, '_nvim_bind')
for _, fn in inspect.getmembers(cls, predicate):
fn._nvim_bind = False
return cls
def rpc_export(rpc_method_name: str, sync: bool = False) -> Callable[[F], F]:
"""Export a function or plugin method as a msgpack-rpc request handler."""
def dec(f: F) -> F:
f._nvim_rpc_method_name = rpc_method_name # type: ignore[attr-defined]
f._nvim_rpc_sync = sync # type: ignore[attr-defined]
f._nvim_bind = True # type: ignore[attr-defined]
f._nvim_prefix_plugin_path = False # type: ignore[attr-defined]
return f
return dec
def command(
name: str,
nargs: Union[str, int] = 0,
complete: Optional[str] = None,
range: Optional[Union[str, int]] = None,
count: Optional[int] = None,
bang: bool = False,
register: bool = False,
sync: bool = False,
allow_nested: bool = False,
eval: Optional[str] = None
) -> Callable[[F], F]:
"""Tag a function or plugin method as a Nvim command handler."""
def dec(f: F) -> F:
f._nvim_rpc_method_name = ( # type: ignore[attr-defined]
'command:{}'.format(name)
)
f._nvim_rpc_sync = sync # type: ignore[attr-defined]
f._nvim_bind = True # type: ignore[attr-defined]
f._nvim_prefix_plugin_path = True # type: ignore[attr-defined]
opts: Dict[str, Any] = {}
if range is not None:
opts['range'] = '' if range is True else str(range)
elif count is not None:
opts['count'] = count
if bang:
opts['bang'] = ''
if register:
opts['register'] = ''
if nargs:
opts['nargs'] = nargs
if complete:
opts['complete'] = complete
if eval:
opts['eval'] = eval
if not sync and allow_nested:
rpc_sync: Union[bool, Literal['urgent']] = "urgent"
else:
rpc_sync = sync
f._nvim_rpc_spec = { # type: ignore[attr-defined]
'type': 'command',
'name': name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec
def autocmd(
name: str,
pattern: str = '*',
sync: bool = False,
allow_nested: bool = False,
eval: Optional[str] = None
) -> Callable[[F], F]:
"""Tag a function or plugin method as a Nvim autocommand handler."""
def dec(f: F) -> F:
f._nvim_rpc_method_name = ( # type: ignore[attr-defined]
'autocmd:{}:{}'.format(name, pattern)
)
f._nvim_rpc_sync = sync # type: ignore[attr-defined]
f._nvim_bind = True # type: ignore[attr-defined]
f._nvim_prefix_plugin_path = True # type: ignore[attr-defined]
opts = {
'pattern': pattern
}
if eval:
opts['eval'] = eval
if not sync and allow_nested:
rpc_sync: Union[bool, Literal['urgent']] = "urgent"
else:
rpc_sync = sync
f._nvim_rpc_spec = { # type: ignore[attr-defined]
'type': 'autocmd',
'name': name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec
def function(
name: str,
range: Union[bool, str, int] = False,
sync: bool = False,
allow_nested: bool = False,
eval: Optional[str] = None
) -> Callable[[F], F]:
"""Tag a function or plugin method as a Nvim function handler."""
def dec(f: F) -> F:
f._nvim_rpc_method_name = ( # type: ignore[attr-defined]
'function:{}'.format(name)
)
f._nvim_rpc_sync = sync # type: ignore[attr-defined]
f._nvim_bind = True # type: ignore[attr-defined]
f._nvim_prefix_plugin_path = True # type: ignore[attr-defined]
opts = {}
if range:
opts['range'] = '' if range is True else str(range)
if eval:
opts['eval'] = eval
if not sync and allow_nested:
rpc_sync: Union[bool, Literal['urgent']] = "urgent"
else:
rpc_sync = sync
f._nvim_rpc_spec = { # type: ignore[attr-defined]
'type': 'function',
'name': name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec
def shutdown_hook(f: F) -> F:
"""Tag a function or method as a shutdown hook."""
f._nvim_shutdown_hook = True # type: ignore[attr-defined]
f._nvim_bind = True # type: ignore[attr-defined]
return f
def decode(mode: str = unicode_errors_default) -> Callable[[F], F]:
"""Configure automatic encoding/decoding of strings."""
def dec(f: F) -> F:
f._nvim_decode = mode # type: ignore[attr-defined]
return f
return dec
def encoding(encoding: Union[bool, str] = True) -> Callable[[F], F]:
"""DEPRECATED: use pynvim.decode()."""
if isinstance(encoding, str):
encoding = True
def dec(f: F) -> F:
f._nvim_decode = encoding # type: ignore[attr-defined]
return f
return dec
|