Completed
Pull Request — master (#156)
by Björn
29:44
created

neovim.api.Buffer.clear_highlight()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1
Metric Value
cc 1
dl 0
loc 4
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=0,
131
                      col_end=-1, src_id=-1, async=None):
132
        """Add a highlight to the buffer."""
133 6
        if async is None:
134
            async = (src_id != 0)
135 6
        return self._session.request('buffer_add_highlight', self, src_id,
136
                                     hl_group, line, col_start,
137
                                     col_end, async=async)
138 6
139
    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...
140 6
        """clear highlights from the buffer."""
141
        self._session.request('buffer_clear_highlight', self, src_id,
142
                              line_end, line_end, async=async)
143 6
144
    @property
145 6
    def name(self):
146
        """Get the buffer name."""
147
        return self._session.request('buffer_get_name', self)
148 6
149
    @name.setter
150
    def name(self, value):
151 6
        """Set the buffer name. BufFilePre/BufFilePost are triggered."""
152 6
        return self._session.request('buffer_set_name', self, value)
153
154
    @property
155
    def valid(self):
156
        """Return True if the buffer still exists."""
157 6
        return self._session.request('buffer_is_valid', self)
158
159
    @property
160 6
    def number(self):
161
        """Get the buffer number."""
162
        return self._session.request('buffer_get_number', self)
163
164
165
class Range(object):
166
    def __init__(self, buffer, start, end):
167
        self._buffer = buffer
168
        self.start = start - 1
169
        self.end = end
170
171 6
    def __len__(self):
172
        return self.end - self.start
173
174
    def __getitem__(self, idx):
175
        if not isinstance(idx, slice):
176
            return self._buffer[self._normalize_index(idx)]
177
        start = self._normalize_index(idx.start)
178
        end = self._normalize_index(idx.stop)
179
        if start is None:
180
            start = self.start
181
        if end is None:
182
            end = self.end
183 6
        return self._buffer[start:end]
184
185
    def __setitem__(self, idx, lines):
186
        if not isinstance(idx, slice):
187 6
            self._buffer[self._normalize_index(idx)] = lines
188
            return
189
        start = self._normalize_index(idx.start)
190
        end = self._normalize_index(idx.stop)
191
        if start is None:
192
            start = self.start
193 6
        if end is None:
194
            end = self.end
195
        self._buffer[start:end] = lines
196
197
    def __iter__(self):
198
        for i in range(self.start, self.end):
199
            yield self._buffer[i]
200
201
    def append(self, lines, i=None):
202
        i = self._normalize_index(i)
203
        if i is None:
204
            i = self.end
205
        self._buffer.append(lines, i)
206
207
    def _normalize_index(self, index):
208
        if index is None:
209
            return None
210
        if index < 0:
211
            index = self.end - 1
212
        else:
213
            index += self.start
214
            if index >= self.end:
215
                index = self.end - 1
216
        return index
217