|
3 | 3 | import sys
|
4 | 4 |
|
5 | 5 | from ..api.common import SessionHook
|
| 6 | +from ..compat import IS_PYTHON3 |
6 | 7 |
|
7 | 8 |
|
8 | 9 | logger = logging.getLogger(__name__)
|
9 | 10 | debug, warn = (logger.debug, logger.warn,)
|
10 | 11 |
|
| 12 | +if IS_PYTHON3: |
| 13 | + basestring = str |
11 | 14 |
|
12 | 15 | class ScriptHost(object):
|
13 | 16 | """
|
@@ -55,13 +58,31 @@ def python_do_range(self, start, stop, code):
|
55 | 58 | sstart = start
|
56 | 59 | sstop = min(start + 5000, stop)
|
57 | 60 | lines = nvim.current.buffer.get_line_slice(sstart, sstop, True, True)
|
| 61 | + |
| 62 | + exception = None |
| 63 | + newlines = [] |
| 64 | + linenr = sstart + 1 |
58 | 65 | 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 | + |
63 | 81 | 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 |
65 | 86 | # delete the function
|
66 | 87 | del self.module.__dict__[fname]
|
67 | 88 |
|
|
0 commit comments