Skip to content

Commit be6e96e

Browse files
committed
handle decoding for rplugin methods the same as for api calls
1 parent 1395c05 commit be6e96e

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

neovim/api/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ def _decode_if_bytes(self, obj, session, method, kind):
180180
return obj.decode(self.encoding, errors=self.encoding_errors)
181181
return obj
182182

183+
def walk(self, obj):
184+
"""Decode bytes found in obj (any msgpack object).
185+
186+
Uses encoding and policy specified in constructor.
187+
"""
188+
return walk(self._decode_if_bytes, obj, None, None, None)
189+
183190

184191
class SessionFilter(object):
185192

neovim/plugin/decorators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def dec(f):
7373
if eval:
7474
opts['eval'] = eval
7575

76-
f.nvim_rpc_spec = {
76+
f._nvim_rpc_spec = {
7777
'type': 'command',
7878
'name': name,
7979
'sync': sync,
@@ -98,7 +98,7 @@ def dec(f):
9898
if eval:
9999
opts['eval'] = eval
100100

101-
f.nvim_rpc_spec = {
101+
f._nvim_rpc_spec = {
102102
'type': 'autocmd',
103103
'name': name,
104104
'sync': sync,
@@ -124,7 +124,7 @@ def dec(f):
124124
if eval:
125125
opts['eval'] = eval
126126

127-
f.nvim_rpc_spec = {
127+
f._nvim_rpc_spec = {
128128
'type': 'function',
129129
'name': name,
130130
'sync': sync,

neovim/plugin/host.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,22 @@ def _discover_functions(self, obj, handlers, plugin_path):
127127
def predicate(o):
128128
return hasattr(o, '_nvim_rpc_method_name')
129129
specs = []
130+
objenc = getattr(obj, '_nvim_encoding', None)
130131
for _, fn in inspect.getmembers(obj, predicate):
132+
enc = getattr(fn, '_nvim_encoding', objenc)
131133
if fn._nvim_bind:
132134
# bind a nvim instance to the handler
133135
fn2 = functools.partial(fn, self._configure_nvim_for(fn))
134136
# copy _nvim_* attributes from the original function
135-
for attr in dir(fn):
136-
if attr.startswith('_nvim_'):
137-
setattr(fn2, attr, getattr(fn, attr))
137+
self._copy_attributes(fn, fn2)
138138
fn = fn2
139+
decodehook = self._decodehook_for(enc)
140+
if decodehook is not None:
141+
decoder = lambda fn, hook, *args: fn(*hook.walk(args))
142+
fn2 = functools.partial(decoder, fn, decodehook)
143+
self._copy_attributes(fn, fn2)
144+
fn = fn2
145+
139146
# register in the rpc handler dict
140147
method = fn._nvim_rpc_method_name
141148
if fn._nvim_prefix_plugin_path:
@@ -150,25 +157,36 @@ def predicate(o):
150157
raise Exception(('Notification handler for "{0}" is ' +
151158
'already registered').format(method))
152159
self._notification_handlers[method] = fn
153-
if hasattr(fn, 'nvim_rpc_spec'):
154-
specs.append(fn.nvim_rpc_spec)
160+
if hasattr(fn, '_nvim_rpc_spec'):
161+
specs.append(fn._nvim_rpc_spec)
155162
handlers.append(fn)
156163
if specs:
157164
self._specs[plugin_path] = specs
158165

166+
def _copy_attributes(self, fn, fn2):
167+
# Copy _nvim_* attributes from the original function
168+
for attr in dir(fn):
169+
if attr.startswith('_nvim_'):
170+
setattr(fn2, attr, getattr(fn, attr))
171+
159172
def _on_specs_request(self, path):
160173
if IS_PYTHON3 and isinstance(path, bytes):
161174
path = path.decode(self._nvim_encoding)
162175
return self._specs.get(path, [])
163176

164-
def _configure_nvim_for(self, obj):
165-
# Configure a nvim instance for obj(checks encoding configuration)
166-
nvim = self.nvim
167-
encoding = getattr(obj, '_nvim_encoding', None)
177+
def _decodehook_for(self, encoding):
168178
if IS_PYTHON3 and encoding is None:
169179
encoding = True
170180
if encoding is True:
171181
encoding = self._nvim_encoding
172182
if encoding:
173-
nvim = nvim.with_hook(DecodeHook(encoding))
183+
return DecodeHook(encoding)
184+
185+
def _configure_nvim_for(self, obj):
186+
# Configure a nvim instance for obj (checks encoding configuration)
187+
nvim = self.nvim
188+
encoding = getattr(obj, '_nvim_encoding', None)
189+
hook = self._decodehook_for(encoding)
190+
if hook is not None:
191+
nvim = nvim.with_hook(hook)
174192
return nvim

0 commit comments

Comments
 (0)