@@ -72,6 +72,26 @@ def shutdown(self):
72
72
self ._unload ()
73
73
self .nvim .stop_loop ()
74
74
75
+ def _wrap_delayed_function (self , cls , delayed_handlers , name , sync ,
76
+ module_handlers , path , * args ):
77
+ # delete the delayed handlers to be sure
78
+ for handler in delayed_handlers :
79
+ method_name = handler ._nvim_registered_name
80
+ if handler ._nvim_rpc_sync :
81
+ del self ._request_handlers [method_name ]
82
+ else :
83
+ del self ._notification_handlers [method_name ]
84
+ # create an instance of the plugin and pass the nvim object
85
+ plugin = cls (self ._configure_nvim_for (cls ))
86
+
87
+ # discover handlers in the plugin instance
88
+ self ._discover_functions (plugin , module_handlers , path , False )
89
+
90
+ if sync :
91
+ self ._request_handlers [name ](* args )
92
+ else :
93
+ self ._notification_handlers [name ](* args )
94
+
75
95
def _wrap_function (self , fn , sync , decode , nvim_bind , name , * args ):
76
96
if decode :
77
97
args = walk (decode_if_bytes , args , decode )
@@ -144,7 +164,7 @@ def _load(self, plugins):
144
164
module = imp .load_module (name , file , pathname , descr )
145
165
handlers = []
146
166
self ._discover_classes (module , handlers , path )
147
- self ._discover_functions (module , handlers , path )
167
+ self ._discover_functions (module , handlers , path , False )
148
168
if not handlers :
149
169
error ('{} exports no handlers' .format (path ))
150
170
continue
@@ -165,7 +185,7 @@ def _unload(self):
165
185
for path , plugin in self ._loaded .items ():
166
186
handlers = plugin ['handlers' ]
167
187
for handler in handlers :
168
- method_name = handler ._nvim_rpc_method_name
188
+ method_name = handler ._nvim_registered_name
169
189
if hasattr (handler , '_nvim_shutdown_hook' ):
170
190
handler ()
171
191
elif handler ._nvim_rpc_sync :
@@ -178,31 +198,35 @@ def _unload(self):
178
198
def _discover_classes (self , module , handlers , plugin_path ):
179
199
for _ , cls in inspect .getmembers (module , inspect .isclass ):
180
200
if getattr (cls , '_nvim_plugin' , False ):
181
- # create an instance of the plugin and pass the nvim object
182
- plugin = cls (self ._configure_nvim_for (cls ))
183
201
# discover handlers in the plugin instance
184
- self ._discover_functions (plugin , handlers , plugin_path )
202
+ self ._discover_functions (cls , handlers , plugin_path , True )
185
203
186
- def _discover_functions (self , obj , handlers , plugin_path ):
204
+ def _discover_functions (self , obj , handlers , plugin_path , delay ):
187
205
def predicate (o ):
188
206
return hasattr (o , '_nvim_rpc_method_name' )
189
207
208
+ cls_handlers = []
190
209
specs = []
191
210
objdecode = getattr (obj , '_nvim_decode' , self ._decode_default )
192
211
for _ , fn in inspect .getmembers (obj , predicate ):
193
- sync = fn ._nvim_rpc_sync
194
- decode = getattr (fn , '_nvim_decode' , objdecode )
195
- nvim_bind = None
196
- if fn ._nvim_bind :
197
- nvim_bind = self ._configure_nvim_for (fn )
198
-
199
212
method = fn ._nvim_rpc_method_name
200
213
if fn ._nvim_prefix_plugin_path :
201
214
method = '{}:{}' .format (plugin_path , method )
215
+ sync = fn ._nvim_rpc_sync
216
+ if delay :
217
+ fn_wrapped = partial (self ._wrap_delayed_function , obj ,
218
+ cls_handlers , method , sync ,
219
+ handlers , plugin_path )
220
+ else :
221
+ decode = getattr (fn , '_nvim_decode' , objdecode )
222
+ nvim_bind = None
223
+ if fn ._nvim_bind :
224
+ nvim_bind = self ._configure_nvim_for (fn )
202
225
203
- fn_wrapped = partial (self ._wrap_function , fn ,
204
- sync , decode , nvim_bind , method )
226
+ fn_wrapped = partial (self ._wrap_function , fn ,
227
+ sync , decode , nvim_bind , method )
205
228
self ._copy_attributes (fn , fn_wrapped )
229
+ fn_wrapped ._nvim_registered_name = method
206
230
# register in the rpc handler dict
207
231
if sync :
208
232
if method in self ._request_handlers :
@@ -217,6 +241,7 @@ def predicate(o):
217
241
if hasattr (fn , '_nvim_rpc_spec' ):
218
242
specs .append (fn ._nvim_rpc_spec )
219
243
handlers .append (fn_wrapped )
244
+ cls_handlers .append (fn_wrapped )
220
245
if specs :
221
246
self ._specs [plugin_path ] = specs
222
247
0 commit comments