Skip to content

Commit 496f555

Browse files
committed
Merge pull request #209 from asankah/buffer-indices
vim.buffers is a map not a list.
2 parents e58cd58 + 8eb6ba7 commit 496f555

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ bridge](http://vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-vim)):
169169
# Create a python API session attached to unix domain socket created above:
170170
>>> nvim = attach('socket', path='/tmp/nvim')
171171
# Now do some work.
172-
>>> buffer = nvim.buffers[0] # Get the first buffer
172+
>>> buffer = nvim.current.buffer # Get the current buffer
173173
>>> buffer[0] = 'replace first line'
174174
>>> buffer[:] = ['replace whole buffer']
175175
>>> nvim.command('vsplit')

neovim/api/buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""API for working with Nvim buffers."""
1+
"""API for working with a Nvim Buffer."""
22
from .common import Remote
33
from ..compat import IS_PYTHON3
44

neovim/api/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ class RemoteSequence(object):
113113
sequences(of lines, buffers, windows and tabpages) with an API that
114114
is similar to the one provided by the python-vim interface.
115115
116-
For example, the 'buffers' property of the `Nvim class is a RemoteSequence
117-
sequence instance, and the expression `nvim.buffers[0]` is translated to
118-
session.request('vim_get_buffers')[0].
116+
For example, the 'windows' property of the `Nvim` class is a RemoteSequence
117+
sequence instance, and the expression `nvim.windows[0]` is translated to
118+
session.request('vim_get_windows')[0].
119119
120120
It can also receive an optional self_obj that will be passed as first
121121
argument of the request. For example, `tabpage.windows[0]` is translated

neovim/api/nvim.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, session, channel_id, metadata, types,
7979
self.vars = RemoteMap(self, 'vim_get_var', 'vim_set_var')
8080
self.vvars = RemoteMap(self, 'vim_get_vvar', None)
8181
self.options = RemoteMap(self, 'vim_get_option', 'vim_set_option')
82-
self.buffers = RemoteSequence(self, 'vim_get_buffers')
82+
self.buffers = Buffers(self)
8383
self.windows = RemoteSequence(self, 'vim_get_windows')
8484
self.tabpages = RemoteSequence(self, 'vim_get_tabpages')
8585
self.current = Current(self)
@@ -331,6 +331,41 @@ def handler():
331331
self._session.threadsafe_call(handler)
332332

333333

334+
class Buffers(object):
335+
336+
"""Remote NVim buffers.
337+
338+
Currently the interface for interacting with remote NVim buffers is the
339+
`vim_get_buffers` msgpack-rpc function. Most methods fetch the list of
340+
buffers from NVim.
341+
342+
Conforms to *python-buffers*.
343+
"""
344+
345+
def __init__(self, nvim):
346+
"""Initialize a Buffers object with Nvim object `nvim`."""
347+
self._fetch_buffers = nvim.api.get_buffers
348+
349+
def __len__(self):
350+
"""Return the count of buffers."""
351+
return len(self._fetch_buffers())
352+
353+
def __getitem__(self, number):
354+
"""Return the Buffer object matching buffer number `number`."""
355+
for b in self._fetch_buffers():
356+
if b.number == number:
357+
return b
358+
raise KeyError(number)
359+
360+
def __contains__(self, b):
361+
"""Return whether Buffer `b` is a known valid buffer."""
362+
return isinstance(b, Buffer) and b.valid
363+
364+
def __iter__(self):
365+
"""Return an iterator over the list of buffers."""
366+
return iter(self._fetch_buffers())
367+
368+
334369
class CompatibilitySession(object):
335370

336371
"""Helper class for API compatibility."""

test/test_vim.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,29 @@ def test_options():
9292

9393
@with_setup(setup=cleanup)
9494
def test_buffers():
95+
buffers = []
96+
97+
# Number of elements
9598
eq(len(vim.buffers), 1)
96-
eq(vim.buffers[0], vim.current.buffer)
99+
100+
# Indexing (by buffer number)
101+
eq(vim.buffers[vim.current.buffer.number], vim.current.buffer)
102+
103+
buffers.append(vim.current.buffer)
97104
vim.command('new')
98105
eq(len(vim.buffers), 2)
99-
eq(vim.buffers[1], vim.current.buffer)
100-
vim.current.buffer = vim.buffers[0]
101-
eq(vim.buffers[0], vim.current.buffer)
106+
buffers.append(vim.current.buffer)
107+
eq(vim.buffers[vim.current.buffer.number], vim.current.buffer)
108+
vim.current.buffer = buffers[0]
109+
eq(vim.buffers[vim.current.buffer.number], buffers[0])
110+
111+
# Membership test
112+
ok(buffers[0] in vim.buffers)
113+
ok(buffers[1] in vim.buffers)
114+
ok({} not in vim.buffers)
115+
116+
# Iteration
117+
eq(buffers, list(vim.buffers))
102118

103119

104120
@with_setup(setup=cleanup)

0 commit comments

Comments
 (0)