Completed
Pull Request — master (#156)
by Björn
21:41
created

neovim.api.Buffer.clear_highlight()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1
Metric Value
cc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
"""API for working with Nvim buffers."""
2 6
from .common import Remote, RemoteMap
3 6
from ..compat import IS_PYTHON3
4
5
6 6
__all__ = ('Buffer')
7
8
9 6
if IS_PYTHON3:
10 3
    basestring = str
11
12
13 6
class Buffer(Remote):
14
15
    """A remote Nvim buffer."""
16
17 6
    def __init__(self, session, code_data):
18
        """Initialize from session and code_data immutable object.
19
20
        The `code_data` contains serialization information required for
21
        msgpack-rpc calls. It must be immutable for Buffer equality to work.
22
        """
23 6
        self._session = session
24 6
        self.code_data = code_data
25 6
        self.vars = RemoteMap(session, 'buffer_get_var', 'buffer_set_var',
26
                              self)
27 6
        self.options = RemoteMap(session, 'buffer_get_option',
28
                                 'buffer_set_option', self)
29
30 6
    def __len__(self):
31
        """Return the number of lines contained in a Buffer."""
32 6
        return self._session.request('buffer_line_count', self)
33
34 6
    def __getitem__(self, idx):
35
        """Get a buffer line or slice by integer index.
36
37
        Indexes may be negative to specify positions from the end of the
38
        buffer. For example, -1 is the last line, -2 is the line before that
39
        and so on.
40
41
        When retrieving slices, omiting indexes(eg: `buffer[:]`) will bring
42
        the whole buffer.
43
        """
44 6
        if not isinstance(idx, slice):
45 6
            return self._session.request('buffer_get_line', self, idx)
46 6
        include_end = False
47 6
        start = idx.start
48 6
        end = idx.stop
49 6
        if start is None:
50 6
            start = 0
51 6
        if end is None:
52 6
            end = -1
53 6
            include_end = True
54 6
        return self._session.request('buffer_get_line_slice', self, start, end,
55
                                     True, include_end)
56
57 6
    def __setitem__(self, idx, lines):
58
        """Replace a buffer line or slice by integer index.
59
60
        Like with `__getitem__`, indexes may be negative.
61
62
        When replacing slices, omiting indexes(eg: `buffer[:]`) will replace
63
        the whole buffer.
64
        """
65 6
        if not isinstance(idx, slice):
66 6
            if lines is None:
67 6
                return self._session.request('buffer_del_line', self, idx)
68
            else:
69 6
                return self._session.request('buffer_set_line', self, idx,
70
                                             lines)
71 6
        if lines is None:
72 6
            lines = []
73 6
        include_end = False
74 6
        start = idx.start
75 6
        end = idx.stop
76 6
        if start is None:
77 6
            start = 0
78 6
        if end is None:
79 6
            end = -1
80 6
            include_end = True
81 6
        return self._session.request('buffer_set_line_slice', self, start, end,
82
                                     True, include_end, lines)
83
84 6
    def __iter__(self):
85
        """Iterate lines of a buffer.
86
87
        This will retrieve all lines locally before iteration starts. This
88
        approach is used because for most cases, the gain is much greater by
89
        minimizing the number of API calls by transfering all data needed to
90
        work.
91
        """
92
        lines = self[:]
93
        for line in lines:
94
            yield line
95
96 6
    def __delitem__(self, idx):
97
        """Delete line or slice of lines from the buffer.
98
99
        This is the same as __setitem__(idx, [])
100
        """
101 6
        if not isinstance(idx, slice):
102 6
            self.__setitem__(idx, None)
103
        else:
104 6
            self.__setitem__(idx, [])
105
106 6
    def get_line_slice(self, start, stop, start_incl, end_incl):
107
        """More flexible wrapper for retrieving slices."""
108
        return self._session.request('buffer_get_line_slice', self, start,
109
                                     stop, start_incl, end_incl)
110
111 6
    def set_line_slice(self, start, stop, start_incl, end_incl, lines):
112
        """More flexible wrapper for replacing slices."""
113
        return self._session.request('buffer_set_line_slice', self, start,
114
                                     stop, start_incl, end_incl, lines)
115
116 6
    def append(self, lines, index=-1):
117
        """Append a string or list of lines to the buffer."""
118 6
        if isinstance(lines, basestring):
119 6
            lines = [lines]
120 6
        return self._session.request('buffer_insert', self, index, lines)
121
122 6
    def mark(self, name):
123
        """Return (row, col) tuple for a named mark."""
124 6
        return self._session.request('buffer_get_mark', self, name)
125
126 6
    def range(self, start, end):
127
        """Return a `Range` object, which represents part of the Buffer."""
128
        return Range(self, start, end)
129
130 6
    def add_highlight(self, hl_group, line, col_start, col_end, src_id=-1, async=None):
131
        if async is None:
132
            async = (src_id != 0)
133 6
        return self._session.request('buffer_add_highlight', self, src_id, hl_group, line, col_start, col_end, async=async)
134
135 6
    def clear_highlight(self, src_id, line_start=0, line_end=-1, async=True):
0 ignored issues
show
Unused Code introduced by
The argument line_start seems to be unused.
Loading history...
136
        self._session.request('buffer_clear_highlight', self, src_id, line_end, line_end, async=async)
137
138 6
    @property
139
    def name(self):
140 6
        """Get the buffer name."""
141
        return self._session.request('buffer_get_name', self)
142
143 6
    @name.setter
144
    def name(self, value):
145 6
        """Set the buffer name. BufFilePre/BufFilePost are triggered."""
146
        return self._session.request('buffer_set_name', self, value)
147
148 6
    @property
149
    def valid(self):
150
        """Return True if the buffer still exists."""
151 6
        return self._session.request('buffer_is_valid', self)
152 6
153
    @property
154
    def number(self):
155
        """Get the buffer number."""
156
        return self._session.request('buffer_get_number', self)
157 6
158
159
class Range(object):
160 6
    def __init__(self, buffer, start, end):
161
        self._buffer = buffer
162
        self.start = start - 1
163
        self.end = end
164
165
    def __len__(self):
166
        return self.end - self.start
167
168
    def __getitem__(self, idx):
169
        if not isinstance(idx, slice):
170
            return self._buffer[self._normalize_index(idx)]
171 6
        start = self._normalize_index(idx.start)
172
        end = self._normalize_index(idx.stop)
173
        if start is None:
174
            start = self.start
175
        if end is None:
176
            end = self.end
177
        return self._buffer[start:end]
178
179
    def __setitem__(self, idx, lines):
180
        if not isinstance(idx, slice):
181
            self._buffer[self._normalize_index(idx)] = lines
182
            return
183 6
        start = self._normalize_index(idx.start)
184
        end = self._normalize_index(idx.stop)
185
        if start is None:
186
            start = self.start
187 6
        if end is None:
188
            end = self.end
189
        self._buffer[start:end] = lines
190
191
    def __iter__(self):
192
        for i in range(self.start, self.end):
193 6
            yield self._buffer[i]
194
195
    def append(self, lines, i=None):
196
        i = self._normalize_index(i)
197
        if i is None:
198
            i = self.end
199
        self._buffer.append(lines, i)
200
201
    def _normalize_index(self, index):
202
        if index is None:
203
            return None
204
        if index < 0:
205
            index = self.end - 1
206
        else:
207
            index += self.start
208
            if index >= self.end:
209
                index = self.end - 1
210
        return index
211