Skip to content

Commit a3af723

Browse files
henadzittarruda
authored andcommitted
Implemented vim.current.range
- Add arguments range_start and range_end to methods python_execute and python_execute_file - Add test for python_execute with range
1 parent 33087a2 commit a3af723

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

neovim/api/nvim.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class Current(object):
199199

200200
def __init__(self, session):
201201
self._session = session
202+
self.range = None
202203

203204
@property
204205
def line(self):

neovim/plugins/script_host.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ def __init__(self, nvim):
2828
sys.modules['vim'] = nvim.with_hook(LegacyEvalHook())
2929
if IS_PYTHON3:
3030
sys.modules['vim'] = sys.modules['vim'].with_hook(DecodeHook(encoding=nvim.options['encoding'].decode('ascii')))
31+
self.legacy_vim = sys.modules['vim']
3132

32-
def python_execute(self, script):
33+
def python_execute(self, script, range_start, range_end):
34+
self._set_current_range(range_start, range_end)
3335
exec(script, self.module.__dict__)
3436

35-
def python_execute_file(self, file_path):
37+
def python_execute_file(self, file_path, range_start, range_end):
38+
self._set_current_range(range_start, range_end)
3639
with open(file_path) as f:
3740
script = compile(f.read(), file_path, 'exec')
3841
exec(script, self.module.__dict__)
3942

43+
def _set_current_range(self, range_start, range_end):
44+
self.legacy_vim.current.range = \
45+
self.legacy_vim.current.buffer.range(range_start, range_end)
46+
4047
def python_do_range(self, start, stop, code):
4148
nvim = self.nvim
4249
start -= 1

test/test_script_host.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def test_python_nested_commands():
4444
eq(vim.vars['set_by_nested_python'], 555)
4545

4646

47+
@with_setup(setup=host_setup, teardown=host_teardown)
48+
def test_python_command_with_range():
49+
vim.feedkeys('iline1\nline2\nline3\nline4\033')
50+
vim.feedkeys('ggjvj:python vim.vars["range"] = vim.current.range[:]\n')
51+
vim.eval('1') # wait for the keys to be processed
52+
eq(vim.vars['range'], ['line2', 'line3'])
53+
54+
4755
@with_setup(setup=host_setup, teardown=host_teardown)
4856
def test_pyfile():
4957
fname = 'pyfile.py'

0 commit comments

Comments
 (0)