Skip to content

Commit 68aa352

Browse files
committed
rplugin: conditionally enable nested notifications
1 parent a703b47 commit 68aa352

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ below.
7676
computations. Intensive computations should be done in a separate thread (or
7777
process), and `vim.async_call` can be used to send results back to nvim.
7878

79-
* Some methods accept an `async` keyword argument: `vim.eval`,
80-
`vim.command`, `vim.request` as well as the `vim.funcs` and `vim.api` wrappers.
81-
The python host will not wait for nvim to complete the request (which also
82-
means that the return value is unavailable).
79+
* Some methods accept an `async` keyword argument: `vim.eval`, `vim.command`,
80+
`vim.request` as well as the `vim.funcs` and `vim.api` wrappers. When
81+
`async=True` is passed the client will not wait for nvim to complete the
82+
request (which also means that the return value is unavailable).
8383

8484
#### Remote (new-style) plugins
8585

@@ -114,6 +114,14 @@ If `sync=True` is supplied nvim will wait for the handler to finish (this is
114114
required for function return values), but by default handlers are executed
115115
asynchronously.
116116

117+
Normally async handlers (`sync=False`, the default) are blocked while a
118+
synchronous handler is running. This ensures that async handlers can call
119+
requests without nvim confusing these requests with requests from a synchronous
120+
handler. To execute an asynchronous handler even when other handlers are
121+
running, add `allow_nested=True` to the decorator. The handler must then not
122+
make synchronous nvim requests, but it can make asynchronous requests, i e
123+
passing `async=True`.
124+
117125
You need to run `:UpdateRemotePlugins` in nvim for changes in the specifications
118126
to have effect. For details see `:help remote-plugin` in nvim.
119127

neovim/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ def setup_logging(name):
126126
logging.root.addHandler(handler)
127127
level = logging.INFO
128128
if 'NVIM_PYTHON_LOG_LEVEL' in os.environ:
129-
l = getattr(logging,
130-
os.environ['NVIM_PYTHON_LOG_LEVEL'].strip(),
131-
level)
132-
if isinstance(l, int):
133-
level = l
129+
lvl = getattr(logging,
130+
os.environ['NVIM_PYTHON_LOG_LEVEL'].strip(),
131+
level)
132+
if isinstance(lvl, int):
133+
level = lvl
134134
logger.setLevel(level)
135135

136136

neovim/api/nvim.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import functools
33
import os
44
import sys
5-
65
from traceback import format_stack
76

87
from msgpack import ExtType
@@ -298,9 +297,9 @@ def replace_termcodes(self, string, from_part=False, do_lt=True,
298297
return self.request('nvim_replace_termcodes', string,
299298
from_part, do_lt, special)
300299

301-
def out_write(self, msg):
300+
def out_write(self, msg, **kwargs):
302301
"""Print `msg` as a normal message."""
303-
return self.request('nvim_out_write', msg)
302+
return self.request('nvim_out_write', msg, **kwargs)
304303

305304
def err_write(self, msg, **kwargs):
306305
"""Print `msg` as an error message."""

neovim/msgpack_rpc/session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Synchronous msgpack-rpc session layer."""
22
import logging
33
from collections import deque
4-
54
from traceback import format_exc
65

76
import greenlet

neovim/plugin/decorators.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def dec(f):
4343

4444

4545
def command(name, nargs=0, complete=None, range=None, count=None, bang=False,
46-
register=False, sync=False, eval=None):
46+
register=False, sync=False, allow_nested=False, eval=None):
4747
"""Tag a function or plugin method as a Nvim command handler."""
4848
def dec(f):
4949
f._nvim_rpc_method_name = 'command:{}'.format(name)
@@ -73,17 +73,22 @@ def dec(f):
7373
if eval:
7474
opts['eval'] = eval
7575

76+
if not sync and allow_nested:
77+
rpc_sync = "urgent"
78+
else:
79+
rpc_sync = sync
80+
7681
f._nvim_rpc_spec = {
7782
'type': 'command',
7883
'name': name,
79-
'sync': sync,
84+
'sync': rpc_sync,
8085
'opts': opts
8186
}
8287
return f
8388
return dec
8489

8590

86-
def autocmd(name, pattern='*', sync=False, eval=None):
91+
def autocmd(name, pattern='*', sync=False, allow_nested=False, eval=None):
8792
"""Tag a function or plugin method as a Nvim autocommand handler."""
8893
def dec(f):
8994
f._nvim_rpc_method_name = 'autocmd:{}:{}'.format(name, pattern)
@@ -98,17 +103,22 @@ def dec(f):
98103
if eval:
99104
opts['eval'] = eval
100105

106+
if not sync and allow_nested:
107+
rpc_sync = "urgent"
108+
else:
109+
rpc_sync = sync
110+
101111
f._nvim_rpc_spec = {
102112
'type': 'autocmd',
103113
'name': name,
104-
'sync': sync,
114+
'sync': rpc_sync,
105115
'opts': opts
106116
}
107117
return f
108118
return dec
109119

110120

111-
def function(name, range=False, sync=False, eval=None):
121+
def function(name, range=False, sync=False, allow_nested=False, eval=None):
112122
"""Tag a function or plugin method as a Nvim function handler."""
113123
def dec(f):
114124
f._nvim_rpc_method_name = 'function:{}'.format(name)
@@ -124,10 +134,15 @@ def dec(f):
124134
if eval:
125135
opts['eval'] = eval
126136

137+
if not sync and allow_nested:
138+
rpc_sync = "urgent"
139+
else:
140+
rpc_sync = sync
141+
127142
f._nvim_rpc_spec = {
128143
'type': 'function',
129144
'name': name,
130-
'sync': sync,
145+
'sync': rpc_sync,
131146
'opts': opts
132147
}
133148
return f

neovim/plugin/host.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Implements a Nvim host for python plugins."""
2-
import functools
32
import imp
43
import inspect
54
import logging
65
import os
76
import os.path
87
import re
9-
8+
from functools import partial
109
from traceback import format_exc
1110

1211
from . import script_host
@@ -181,8 +180,8 @@ def predicate(o):
181180
if fn._nvim_prefix_plugin_path:
182181
method = '{}:{}'.format(plugin_path, method)
183182

184-
fn_wrapped = functools.partial(self._wrap_function, fn,
185-
sync, decode, nvim_bind, method)
183+
fn_wrapped = partial(self._wrap_function, fn,
184+
sync, decode, nvim_bind, method)
186185
self._copy_attributes(fn, fn_wrapped)
187186
# register in the rpc handler dict
188187
if sync:

0 commit comments

Comments
 (0)