Skip to content

Commit 222023f

Browse files
committed
implement vim_call_function wrapper
1 parent 605897b commit 222023f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

neovim/api/nvim.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Main Nvim interface."""
2+
import functools
23
import os
34

45
from msgpack import ExtType
@@ -73,6 +74,7 @@ def __init__(self, session, channel_id, metadata):
7374
self.windows = RemoteSequence(session, 'vim_get_windows')
7475
self.tabpages = RemoteSequence(session, 'vim_get_tabpages')
7576
self.current = Current(session)
77+
self.funcs = Funcs(self)
7678
self.error = NvimError
7779

7880
def with_hook(self, hook):
@@ -124,6 +126,10 @@ def eval(self, string, async=False):
124126
"""Evaluate a vimscript expression."""
125127
return self._session.request('vim_eval', string, async=async)
126128

129+
def call(self, name, *args):
130+
"""Call a vimscript function."""
131+
return self._session.request('vim_call_function', name, args)
132+
127133
def strwidth(self, string):
128134
"""Return the number of display cells `string` occupies.
129135
@@ -257,6 +263,17 @@ def tabpage(self, tabpage):
257263
return self._session.request('vim_set_current_tabpage', tabpage)
258264

259265

266+
class Funcs(object):
267+
268+
"""Helper class for functional vimscript interface."""
269+
270+
def __init__(self, nvim):
271+
self._nvim = nvim
272+
273+
def __getattr__(self, name):
274+
return functools.partial(self._nvim.call, name)
275+
276+
260277
class ExtHook(SessionHook):
261278
def __init__(self, types):
262279
self.types = types

test/test_vim.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
from nose.tools import with_setup, eq_ as eq, ok_ as ok
44
from common import vim, cleanup
55

6+
def source(code):
7+
fd, fname = tempfile.mkstemp()
8+
with os.fdopen(fd,'w') as f:
9+
f.write(code)
10+
vim.command('source '+fname)
11+
os.unlink(fname)
12+
613

714
@with_setup(setup=cleanup)
815
def test_command():
@@ -29,6 +36,16 @@ def test_eval():
2936
vim.command('let g:v2 = [1, 2, {"v3": 3}]')
3037
eq(vim.eval('g:'), {'v1': 'a', 'v2': [1, 2, {'v3': 3}]})
3138

39+
@with_setup(setup=cleanup)
40+
def test_call():
41+
eq(vim.funcs.join(['first', 'last'], ', '), 'first, last')
42+
source("""
43+
function! Testfun(a,b)
44+
return string(a:a).":".a:b
45+
endfunction
46+
""")
47+
eq(vim.funcs.Testfun(3, 'alpha'), '3:alpha')
48+
3249

3350
@with_setup(setup=cleanup)
3451
def test_strwidth():

0 commit comments

Comments
 (0)