Completed
Pull Request — master (#209)
by
unknown
25:19
created

Buffers.__contains__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
"""API for working with NVim buffers.
2
3
Conforms to python-buffers, which reads:
4
5
  vim.buffers						*python-buffers*
6
	A mapping object providing access to the list of vim buffers.  The
7
	object supports the following operations: >
8
	    :py b = vim.buffers[i]	# Indexing (read-only)
9
	    :py b in vim.buffers	# Membership test
10
	    :py n = len(vim.buffers)	# Number of elements
11
	    :py for b in vim.buffers:	# Iterating over buffer list
12
  
13
The key for indexing into vim.buffers[] is the buffer number as described in
14
*python-buffer*.
15
"""
16
17
import functools
18
19
__all__ = ['Buffers']
20
21
class Buffers(object):
22
23
  """Remote NVim buffers.
24
  
25
  Currently the interface for interacting with remote NVim buffers is the
26
  `vim_get_buffers` msgpack-rpc function. All methods fetch the list of buffers
27
  from NVim.
28
  """
29
30
  def __init__(self, nvim):
31
    """Initialize a Buffers object with Nvim object `nvim`."""
32
    self._fetch_buffers = functools.partial(nvim.request, 'vim_get_buffers')
33
34
  def __len__(self):
35
    """Return the count of buffers."""
36
    return len(self._fetch_buffers())
37
38
  def __getitem__(self, number):
39
    """Return the Buffer object matching buffer number `number`."""
40
    for b in self._fetch_buffers():
41
      if b.number == number:
42
        return b
43
    raise KeyError("No buffers match buffer number {}".format(number))
44
45
  def __contains__(self, b):
46
    """Return whether Buffer `b` is a valid buffer known to the remote end."""
47
    return b in self._fetch_buffers()
48
49
  def __iter__(self):
50
    """Return an iterator over the list of buffers."""
51
    return self._fetch_buffers().__iter__()
52
53