Completed
Pull Request — master (#209)
by
unknown
15:09 queued 13:40
created

Buffers.__contains__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 6
import functools
18
19 6
__all__ = ('Buffers')
20
21
22 6
class Buffers(object):
23
24
    """Remote NVim buffers.
25
    
26
    Currently the interface for interacting with remote NVim buffers is the
27
    `vim_get_buffers` msgpack-rpc function. All methods fetch the list of buffers
28
    from NVim.
29
    """
30
31 6
    def __init__(self, nvim):
32
        """Initialize a Buffers object with Nvim object `nvim`."""
33 6
        self._fetch_buffers = functools.partial(nvim.request, 'vim_get_buffers')
34
35 6
    def __len__(self):
36
        """Return the count of buffers."""
37 6
        return len(self._fetch_buffers())
38
39 6
    def __getitem__(self, number):
40
        """Return the Buffer object matching buffer number `number`."""
41 6
        for b in self._fetch_buffers():
42 6
            if b.number == number:
43 6
                return b
44
        raise KeyError("No buffers match buffer number {}".format(number))
45
46 6
    def __contains__(self, b):
47
        """Return whether Buffer `b` is a valid buffer known to the remote end."""
48 6
        return b in self._fetch_buffers()
49
50 6
    def __iter__(self):
51
        """Return an iterator over the list of buffers."""
52
        return self._fetch_buffers().__iter__()
53