1
|
|
|
"""Decorators used by python host plugin system.""" |
2
|
|
|
|
3
|
6 |
|
import inspect |
4
|
6 |
|
import logging |
5
|
|
|
|
6
|
6 |
|
from ..compat import IS_PYTHON3 |
7
|
|
|
|
8
|
6 |
|
logger = logging.getLogger(__name__) |
9
|
6 |
|
debug, info, warn = (logger.debug, logger.info, logger.warning,) |
10
|
6 |
|
__all__ = ('plugin', 'rpc_export', 'command', 'autocmd', 'function', |
11
|
|
|
'encoding', 'shutdown_hook') |
12
|
|
|
|
13
|
|
|
|
14
|
6 |
|
def plugin(cls): |
15
|
|
|
"""Tag a class as a plugin. |
16
|
|
|
|
17
|
|
|
This decorator is required to make the a class methods discoverable by the |
18
|
|
|
plugin_load method of the host. |
19
|
|
|
""" |
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): |
27
|
|
|
if IS_PYTHON3: |
28
|
|
|
fn._nvim_bind = False |
|
|
|
|
29
|
|
|
else: |
30
|
|
|
fn.im_func._nvim_bind = False |
|
|
|
|
31
|
|
|
return cls |
32
|
|
|
|
33
|
|
|
|
34
|
6 |
|
def rpc_export(rpc_method_name, sync=False): |
35
|
|
|
"""Export a function or plugin method as a msgpack-rpc request handler.""" |
36
|
|
|
def dec(f): |
37
|
|
|
f._nvim_rpc_method_name = rpc_method_name |
|
|
|
|
38
|
|
|
f._nvim_rpc_sync = sync |
|
|
|
|
39
|
|
|
f._nvim_bind = True |
|
|
|
|
40
|
|
|
f._nvim_prefix_plugin_path = False |
|
|
|
|
41
|
|
|
return f |
42
|
|
|
return dec |
43
|
|
|
|
44
|
|
|
|
45
|
6 |
|
def command(name, nargs=0, complete=None, range=None, count=None, bang=False, |
|
|
|
|
46
|
|
|
register=False, sync=False, eval=None): |
|
|
|
|
47
|
|
|
"""Tag a function or plugin method as a Nvim command handler.""" |
48
|
|
|
def dec(f): |
49
|
|
|
f._nvim_rpc_method_name = 'command:{0}'.format(name) |
|
|
|
|
50
|
|
|
f._nvim_rpc_sync = sync |
|
|
|
|
51
|
|
|
f._nvim_bind = True |
|
|
|
|
52
|
|
|
f._nvim_prefix_plugin_path = True |
|
|
|
|
53
|
|
|
|
54
|
|
|
opts = {} |
55
|
|
|
|
56
|
|
|
if range is not None: |
57
|
|
|
opts['range'] = '' if range is True else str(range) |
58
|
|
|
elif count: |
59
|
|
|
opts['count'] = count |
60
|
|
|
|
61
|
|
|
if bang: |
62
|
|
|
opts['bang'] = True |
63
|
|
|
|
64
|
|
|
if register: |
65
|
|
|
opts['register'] = True |
66
|
|
|
|
67
|
|
|
if nargs: |
68
|
|
|
opts['nargs'] = nargs |
69
|
|
|
|
70
|
|
|
if complete: |
71
|
|
|
opts['complete'] = complete |
72
|
|
|
|
73
|
|
|
if eval: |
74
|
|
|
opts['eval'] = eval |
75
|
|
|
|
76
|
|
|
f._nvim_rpc_spec = { |
|
|
|
|
77
|
|
|
'type': 'command', |
78
|
|
|
'name': name, |
79
|
|
|
'sync': sync, |
80
|
|
|
'opts': opts |
81
|
|
|
} |
82
|
|
|
return f |
83
|
|
|
return dec |
84
|
|
|
|
85
|
|
|
|
86
|
6 |
|
def autocmd(name, pattern='*', sync=False, eval=None): |
|
|
|
|
87
|
|
|
"""Tag a function or plugin method as a Nvim autocommand handler.""" |
88
|
|
|
def dec(f): |
89
|
|
|
f._nvim_rpc_method_name = 'autocmd:{0}:{1}'.format(name, pattern) |
|
|
|
|
90
|
|
|
f._nvim_rpc_sync = sync |
|
|
|
|
91
|
|
|
f._nvim_bind = True |
|
|
|
|
92
|
|
|
f._nvim_prefix_plugin_path = True |
|
|
|
|
93
|
|
|
|
94
|
|
|
opts = { |
95
|
|
|
'pattern': pattern |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if eval: |
99
|
|
|
opts['eval'] = eval |
100
|
|
|
|
101
|
|
|
f._nvim_rpc_spec = { |
|
|
|
|
102
|
|
|
'type': 'autocmd', |
103
|
|
|
'name': name, |
104
|
|
|
'sync': sync, |
105
|
|
|
'opts': opts |
106
|
|
|
} |
107
|
|
|
return f |
108
|
|
|
return dec |
109
|
|
|
|
110
|
|
|
|
111
|
6 |
|
def function(name, range=False, sync=False, eval=None): |
|
|
|
|
112
|
|
|
"""Tag a function or plugin method as a Nvim function handler.""" |
113
|
|
|
def dec(f): |
114
|
|
|
f._nvim_rpc_method_name = 'function:{0}'.format(name) |
|
|
|
|
115
|
|
|
f._nvim_rpc_sync = sync |
|
|
|
|
116
|
|
|
f._nvim_bind = True |
|
|
|
|
117
|
|
|
f._nvim_prefix_plugin_path = True |
|
|
|
|
118
|
|
|
|
119
|
|
|
opts = {} |
120
|
|
|
|
121
|
|
|
if range: |
122
|
|
|
opts['range'] = '' if range is True else str(range) |
123
|
|
|
|
124
|
|
|
if eval: |
125
|
|
|
opts['eval'] = eval |
126
|
|
|
|
127
|
|
|
f._nvim_rpc_spec = { |
|
|
|
|
128
|
|
|
'type': 'function', |
129
|
|
|
'name': name, |
130
|
|
|
'sync': sync, |
131
|
|
|
'opts': opts |
132
|
|
|
} |
133
|
|
|
return f |
134
|
|
|
return dec |
135
|
|
|
|
136
|
|
|
|
137
|
6 |
|
def shutdown_hook(f): |
138
|
|
|
"""Tag a function or method as a shutdown hook.""" |
139
|
|
|
f._nvim_shutdown_hook = True |
|
|
|
|
140
|
|
|
f._nvim_bind = True |
|
|
|
|
141
|
|
|
return f |
142
|
|
|
|
143
|
|
|
|
144
|
6 |
|
def encoding(encoding=True): |
|
|
|
|
145
|
|
|
"""Configure automatic encoding/decoding of strings.""" |
146
|
|
|
def dec(f): |
147
|
|
|
f._nvim_encoding = encoding |
|
|
|
|
148
|
|
|
return f |
149
|
|
|
return dec |
150
|
|
|
|
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: