Skip to content

Commit b4c1753

Browse files
committed
Merge pull request #18 from equalsraf/tb-pydo
Fix pydo
2 parents ab02e93 + 73258cf commit b4c1753

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

neovim/plugins/script_host.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import sys
44

55
from ..api.common import SessionHook
6+
from ..compat import IS_PYTHON3
67

78

89
logger = logging.getLogger(__name__)
910
debug, warn = (logger.debug, logger.warn,)
1011

12+
if IS_PYTHON3:
13+
basestring = str
1114

1215
class ScriptHost(object):
1316
"""
@@ -55,13 +58,31 @@ def python_do_range(self, start, stop, code):
5558
sstart = start
5659
sstop = min(start + 5000, stop)
5760
lines = nvim.current.buffer.get_line_slice(sstart, sstop, True, True)
61+
62+
exception = None
63+
newlines = []
64+
linenr = sstart + 1
5865
for i, line in enumerate(lines):
59-
linenr = i + sstart + 1
60-
result = str(function(line, linenr))
61-
if result:
62-
lines[i] = result
66+
result = function(line, linenr)
67+
if result == None:
68+
# Update earlier lines, and skip to the next
69+
if newlines:
70+
nvim.current.buffer.set_line_slice(sstart, sstart + len(newlines) -1, True, True, newlines)
71+
sstart += len(newlines) + 1
72+
newlines = []
73+
pass
74+
elif isinstance(result,basestring):
75+
newlines.append(result)
76+
else:
77+
exception = TypeError('pydo should return a string or None, found %s instead' % result.__class__.__name__)
78+
break
79+
linenr += 1
80+
6381
start = sstop + 1
64-
nvim.current.buffer.set_line_slice(sstart, sstop, True, True, lines)
82+
if newlines:
83+
nvim.current.buffer.set_line_slice(sstart, sstart + len(newlines)-1, True, True, newlines)
84+
if exception:
85+
raise exception
6586
# delete the function
6687
del self.module.__dict__[fname]
6788

test/test_script_host.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ def test_pyfile():
5656

5757
@with_setup(setup=host_setup, teardown=host_teardown)
5858
def test_pydo():
59+
60+
# :pydo 42 returns None for all lines,
61+
# the buffer should not be changed
62+
vim.command('normal :pydo 42\n')
63+
eq(vim.current.buffer.options['mod'], False)
64+
5965
# insert some text
6066
vim.command('normal iabc\ndef\nghi')
6167
eq(vim.current.buffer[:], ['abc', 'def', 'ghi'])
6268
# go to top and select and replace the first two lines
63-
vim.command('normal ggvj:pydo return linenr\n')
69+
vim.command('normal ggvj:pydo return str(linenr)\n')
6470
eq(vim.current.buffer[:], ['1', '2', 'ghi'])
6571

6672

0 commit comments

Comments
 (0)