Skip to content

Commit

Permalink
Merge pull request #190 from bfredl/oob
Browse files Browse the repository at this point in the history
update to new get_lines/set_lines API
  • Loading branch information
bfredl committed Sep 24, 2016
2 parents 1047bd8 + 87b5e53 commit 4dbfbd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 55 deletions.
69 changes: 25 additions & 44 deletions neovim/api/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
basestring = str


def adjust_index(idx, default=None):
"""Convert from python indexing convention to nvim indexing convention."""
if idx is None:
return default
elif idx < 0:
return idx - 1
else:
return idx


class Buffer(Remote):

"""A remote Nvim buffer."""
Expand All @@ -31,19 +41,13 @@ def __getitem__(self, idx):
the whole buffer.
"""
if not isinstance(idx, slice):
return self._session.request('buffer_get_line', self, idx)
include_end = False
start = idx.start
end = idx.stop
if start is None:
start = 0
if end is None:
end = -1
include_end = True
return self._session.request('buffer_get_line_slice', self, start, end,
True, include_end)
i = adjust_index(idx)
return self.request('buffer_get_lines', i, i + 1, True)[0]
start = adjust_index(idx.start, 0)
end = adjust_index(idx.stop, -1)
return self.request('buffer_get_lines', start, end, False)

def __setitem__(self, idx, lines):
def __setitem__(self, idx, item):
"""Replace a buffer line or slice by integer index.
Like with `__getitem__`, indexes may be negative.
Expand All @@ -52,23 +56,13 @@ def __setitem__(self, idx, lines):
the whole buffer.
"""
if not isinstance(idx, slice):
if lines is None:
return self._session.request('buffer_del_line', self, idx)
else:
return self._session.request('buffer_set_line', self, idx,
lines)
if lines is None:
lines = []
include_end = False
start = idx.start
end = idx.stop
if start is None:
start = 0
if end is None:
end = -1
include_end = True
return self._session.request('buffer_set_line_slice', self, start, end,
True, include_end, lines)
i = adjust_index(idx)
lines = [item] if item is not None else []
return self.request('buffer_set_lines', i, i + 1, True, lines)
lines = item if item is not None else []
start = adjust_index(idx.start, 0)
end = adjust_index(idx.stop, -1)
return self.request('buffer_set_lines', start, end, False, lines)

def __iter__(self):
"""Iterate lines of a buffer.
Expand All @@ -87,26 +81,13 @@ def __delitem__(self, idx):
This is the same as __setitem__(idx, [])
"""
if not isinstance(idx, slice):
self.__setitem__(idx, None)
else:
self.__setitem__(idx, [])

def get_line_slice(self, start, stop, start_incl, end_incl):
"""More flexible wrapper for retrieving slices."""
return self._session.request('buffer_get_line_slice', self, start,
stop, start_incl, end_incl)

def set_line_slice(self, start, stop, start_incl, end_incl, lines):
"""More flexible wrapper for replacing slices."""
return self._session.request('buffer_set_line_slice', self, start,
stop, start_incl, end_incl, lines)
self.__setitem__(idx, None)

def append(self, lines, index=-1):
"""Append a string or list of lines to the buffer."""
if isinstance(lines, (basestring, bytes)):
lines = [lines]
return self._session.request('buffer_insert', self, index, lines)
return self.request('buffer_set_lines', index, index, True, lines)

def mark(self, name):
"""Return (row, col) tuple for a named mark."""
Expand Down
18 changes: 7 additions & 11 deletions neovim/plugin/script_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,21 @@ def python_do_range(self, start, stop, code):
self._set_current_range(start, stop)
nvim = self.nvim
start -= 1
stop -= 1
fname = '_vim_pydo'

# define the function
function_def = 'def %s(line, linenr):\n %s' % (fname, code,)
exec(function_def, self.module.__dict__)
# get the function
function = self.module.__dict__[fname]
while start <= stop:
while start < stop:
# Process batches of 5000 to avoid the overhead of making multiple
# API calls for every line. Assuming an average line length of 100
# bytes, approximately 488 kilobytes will be transferred per batch,
# which can be done very quickly in a single API call.
sstart = start
sstop = min(start + 5000, stop)
lines = nvim.current.buffer.get_line_slice(sstart, sstop, True,
True)
lines = nvim.current.buffer.api.get_lines(sstart, sstop, True)

exception = None
newlines = []
Expand All @@ -123,9 +121,8 @@ def python_do_range(self, start, stop, code):
# Update earlier lines, and skip to the next
if newlines:
end = sstart + len(newlines) - 1
nvim.current.buffer.set_line_slice(sstart, end,
True, True,
newlines)
nvim.current.buffer.api.set_lines(sstart, end,
True, newlines)
sstart += len(newlines) + 1
newlines = []
pass
Expand All @@ -138,11 +135,10 @@ def python_do_range(self, start, stop, code):
break
linenr += 1

start = sstop + 1
start = sstop
if newlines:
end = sstart + len(newlines) - 1
nvim.current.buffer.set_line_slice(sstart, end, True, True,
newlines)
end = sstart + len(newlines)
nvim.current.buffer.api.set_lines(sstart, end, True, newlines)
if exception:
raise exception
# delete the function
Expand Down

0 comments on commit 4dbfbd1

Please sign in to comment.