@@ -92,6 +92,7 @@ def __init__(self, session, channel_id, metadata, types,
92
92
self .current = Current (self )
93
93
self .session = CompatibilitySession (self )
94
94
self .funcs = Funcs (self )
95
+ self .lua = LuaFuncs (self )
95
96
self .error = NvimError
96
97
self ._decode = decode
97
98
self ._err_cb = err_cb
@@ -253,6 +254,27 @@ def call(self, name, *args, **kwargs):
253
254
"""Call a vimscript function."""
254
255
return self .request ('nvim_call_function' , name , args , ** kwargs )
255
256
257
+ def exec_lua (self , code , * args , ** kwargs ):
258
+ """Execute lua code.
259
+
260
+ Additional parameters are available as `...` inside the lua chunk.
261
+ Only statements are executed. To evaluate an expression, prefix it
262
+ with `return`: `return my_function(...)`
263
+
264
+ There is a shorthand syntax to call lua functions with arguments:
265
+
266
+ nvim.lua.func(1,2)
267
+ nvim.lua.mymod.myfunction(data, async_=True)
268
+
269
+ is equivalent to
270
+
271
+ nvim.exec_lua("return func(...)", 1, 2)
272
+ nvim.exec_lua("mymod.myfunction(...)", data, async_=True)
273
+
274
+ Note that with `async_=True` there is no return value.
275
+ """
276
+ return self .request ('nvim_execute_lua' , code , args , ** kwargs )
277
+
256
278
def strwidth (self , string ):
257
279
"""Return the number of display cells `string` occupies.
258
280
@@ -467,5 +489,29 @@ def __getattr__(self, name):
467
489
return partial (self ._nvim .call , name )
468
490
469
491
492
+ class LuaFuncs (object ):
493
+
494
+ """Wrapper to allow lua functions to be called like python methods."""
495
+
496
+ def __init__ (self , nvim , name = "" ):
497
+ self ._nvim = nvim
498
+ self .name = name
499
+
500
+ def __getattr__ (self , name ):
501
+ """Return wrapper to named api method."""
502
+ prefix = self .name + "." if self .name else ""
503
+ return LuaFuncs (self ._nvim , prefix + name )
504
+
505
+ def __call__ (self , * args , ** kwargs ):
506
+ # first new function after keyword rename, be a bit noisy
507
+ if 'async' in kwargs :
508
+ raise ValueError ('"async" argument is not allowed. '
509
+ 'Use "async_" instead.' )
510
+ async_ = kwargs .get ('async_' , False )
511
+ pattern = "return {}(...)" if not async_ else "{}(...)"
512
+ code = pattern .format (self .name )
513
+ return self ._nvim .exec_lua (code , * args , ** kwargs )
514
+
515
+
470
516
class NvimError (Exception ):
471
517
pass
0 commit comments