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

Buffers.__getitem__()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 3.072
rs 9.4285
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
28
    buffers 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,
34
                                                'vim_get_buffers')
35
36 6
    def __len__(self):
37
        """Return the count of buffers."""
38 6
        return len(self._fetch_buffers())
39
40 6
    def __getitem__(self, number):
41
        """Return the Buffer object matching buffer number `number`."""
42 6
        for b in self._fetch_buffers():
43 6
            if b.number == number:
44 6
                return b
45
        raise KeyError("No buffers match buffer number {}".format(number))
46
47 6
    def __contains__(self, b):
48
        """Return whether Buffer `b` is a known valid buffer."""
49 6
        return b in self._fetch_buffers()
50
51 6
    def __iter__(self):
52
        """Return an iterator over the list of buffers."""
53
        return self._fetch_buffers().__iter__()
54