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