1
1
"""Main Nvim interface."""
2
2
import functools
3
3
import os
4
+ import sys
4
5
5
6
from traceback import format_exc , format_stack
6
7
@@ -65,9 +66,10 @@ def from_session(cls, session):
65
66
def from_nvim (cls , nvim ):
66
67
"""Create a new Nvim instance from an existing instance."""
67
68
return cls (nvim ._session , nvim .channel_id , nvim .metadata ,
68
- nvim .types , nvim ._decodehook )
69
+ nvim .types , nvim ._decodehook , nvim . _err_cb )
69
70
70
- def __init__ (self , session , channel_id , metadata , types , decodehook = None ):
71
+ def __init__ (self , session , channel_id , metadata , types ,
72
+ decodehook = None , err_cb = None ):
71
73
"""Initialize a new Nvim instance. This method is module-private."""
72
74
self ._session = session
73
75
self .channel_id = channel_id
@@ -84,6 +86,7 @@ def __init__(self, session, channel_id, metadata, types, decodehook=None):
84
86
self .funcs = Funcs (self )
85
87
self .error = NvimError
86
88
self ._decodehook = decodehook
89
+ self ._err_cb = err_cb
87
90
88
91
def _from_nvim (self , obj ):
89
92
if type (obj ) is ExtType :
@@ -132,7 +135,8 @@ def next_message(self):
132
135
if msg :
133
136
return walk (self ._from_nvim , msg )
134
137
135
- def run_loop (self , request_cb , notification_cb , setup_cb = None ):
138
+ def run_loop (self , request_cb , notification_cb ,
139
+ setup_cb = None , err_cb = None ):
136
140
"""Run the event loop to receive requests and notifications from Nvim.
137
141
138
142
This should not be called from a plugin running in the host, which
@@ -146,6 +150,10 @@ def filter_request_cb(name, args):
146
150
def filter_notification_cb (name , args ):
147
151
notification_cb (self ._from_nvim (name ), walk (self ._from_nvim , args ))
148
152
153
+ if err_cb is None :
154
+ err_cb = sys .stderr .write
155
+ self ._err_cb = err_cb
156
+
149
157
self ._session .run (filter_request_cb , filter_notification_cb , setup_cb )
150
158
151
159
def stop_loop (self ):
@@ -155,7 +163,7 @@ def stop_loop(self):
155
163
def with_decodehook (self , hook ):
156
164
"""Initialize a new Nvim instance."""
157
165
return Nvim (self ._session , self .channel_id ,
158
- self .metadata , self .types , hook )
166
+ self .metadata , self .types , hook , self . _err_cb )
159
167
160
168
def ui_attach (self , width , height , rgb ):
161
169
"""Register as a remote UI.
@@ -316,9 +324,9 @@ def handler():
316
324
fn (* args , ** kwargs )
317
325
except Exception as err :
318
326
msg = ("error caught while executing async callback:\n "
319
- "{!r}\n {}\n \n the call was requested at\n {}"
327
+ "{0 !r}\n {1 }\n \n the call was requested at\n {2 }"
320
328
.format (err , format_exc (5 ), call_point ))
321
- self .err_write (msg , async = True )
329
+ self ._err_cb (msg )
322
330
raise
323
331
self ._session .threadsafe_call (handler )
324
332
0 commit comments