File: buffer.py

package info (click to toggle)
python-pynvim 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: python: 3,067; makefile: 4
file content (332 lines) | stat: -rw-r--r-- 10,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""API for working with a Nvim Buffer."""

from __future__ import annotations

from typing import (Any, Iterator, List, Optional, TYPE_CHECKING, Tuple, Union, cast,
                    overload)

from pynvim.api.common import Remote
from pynvim.compat import check_async

if TYPE_CHECKING:
    from pynvim.api import Nvim


__all__ = ('Buffer',)


@overload
def adjust_index(idx: int, default: Optional[int] = None) -> int:
    ...


@overload
def adjust_index(idx: Optional[int], default: int) -> int:
    ...


@overload
def adjust_index(idx: Optional[int], default: Optional[int] = None) -> Optional[int]:
    ...


def adjust_index(idx: Optional[int], default: Optional[int] = None) -> Optional[int]:
    """Convert from python indexing convention to nvim indexing convention."""
    if idx is None:
        return default
    elif idx < 0:
        return idx - 1
    else:
        return idx


class Buffer(Remote):

    """A remote Nvim buffer."""

    _api_prefix = "nvim_buf_"
    _session: "Nvim"

    def __init__(self, session: Nvim, code_data: Tuple[int, Any]):
        """Initialize from Nvim and code_data immutable object."""
        super().__init__(session, code_data)

    def __len__(self) -> int:
        """Return the number of lines contained in a Buffer."""
        return self.request('nvim_buf_line_count')

    @overload
    def __getitem__(self, idx: int) -> str:  # noqa: D105
        ...

    @overload
    def __getitem__(self, idx: slice) -> List[str]:  # noqa: D105
        ...

    def __getitem__(self, idx: Union[int, slice]) -> Union[str, List[str]]:
        """Get a buffer line or slice by integer index.

        Indexes may be negative to specify positions from the end of the
        buffer. For example, -1 is the last line, -2 is the line before that
        and so on.

        When retrieving slices, omitting indexes(eg: `buffer[:]`) will bring
        the whole buffer.
        """
        if not isinstance(idx, slice):
            i = adjust_index(idx)
            return self.request('nvim_buf_get_lines', i, i + 1, True)[0]
        start = adjust_index(idx.start, 0)
        end = adjust_index(idx.stop, -1)
        return self.request('nvim_buf_get_lines', start, end, False)

    @overload
    def __setitem__(self, idx: int, item: Optional[str]) -> None:  # noqa: D105
        ...

    @overload
    def __setitem__(  # noqa: D105
        self, idx: slice, item: Optional[Union[List[str], str]]
    ) -> None:
        ...

    def __setitem__(
        self, idx: Union[int, slice], item: Union[None, str, List[str]]
    ) -> None:
        """Replace a buffer line or slice by integer index.

        Like with `__getitem__`, indexes may be negative.

        When replacing slices, omitting indexes(eg: `buffer[:]`) will replace
        the whole buffer.
        """
        if not isinstance(idx, slice):
            assert not isinstance(item, list)
            i = adjust_index(idx)
            lines = [item] if item is not None else []
            return self.request('nvim_buf_set_lines', i, i + 1, True, lines)
        if item is None:
            lines = []
        elif isinstance(item, str):
            lines = [item]
        else:
            lines = item
        start = adjust_index(idx.start, 0)
        end = adjust_index(idx.stop, -1)
        return self.request('nvim_buf_set_lines', start, end, False, lines)

    def __iter__(self) -> Iterator[str]:
        """Iterate lines of a buffer.

        This will retrieve all lines locally before iteration starts. This
        approach is used because for most cases, the gain is much greater by
        minimizing the number of API calls by transferring all data needed to
        work.
        """
        lines = self[:]
        for line in lines:
            yield line

    def __delitem__(self, idx: Union[int, slice]) -> None:
        """Delete line or slice of lines from the buffer.

        This is the same as __setitem__(idx, [])
        """
        self.__setitem__(idx, None)

    def __ne__(self, other: Any) -> bool:
        """Test inequality of Buffers.

        Necessary for Python 2 compatibility.
        """
        return not self.__eq__(other)

    def append(
        self, lines: Union[str, bytes, List[Union[str, bytes]]], index: int = -1
    ) -> None:
        """Append a string or list of lines to the buffer."""
        if isinstance(lines, (str, bytes)):
            lines = [lines]
        return self.request('nvim_buf_set_lines', index, index, True, lines)

    def mark(self, name: str) -> Tuple[int, int]:
        """Return (row, col) tuple for a named mark."""
        return cast(Tuple[int, int], tuple(self.request('nvim_buf_get_mark', name)))

    def range(self, start: int, end: int) -> Range:
        """Return a `Range` object, which represents part of the Buffer."""
        return Range(self, start, end)

    def add_highlight(
        self,
        hl_group: str,
        line: int,
        col_start: int = 0,
        col_end: int = -1,
        src_id: int = -1,
        async_: Optional[bool] = None,
        **kwargs: Any
    ) -> int:
        """Add a highlight to the buffer."""
        async_ = check_async(async_, kwargs, src_id != 0)
        return self.request(
            "nvim_buf_add_highlight",
            src_id,
            hl_group,
            line,
            col_start,
            col_end,
            async_=async_,
        )

    def clear_highlight(
        self,
        src_id: int,
        line_start: int = 0,
        line_end: int = -1,
        async_: Optional[bool] = None,
        **kwargs: Any
    ) -> None:
        """Clear highlights from the buffer."""
        async_ = check_async(async_, kwargs, True)
        self.request(
            "nvim_buf_clear_highlight", src_id, line_start, line_end, async_=async_
        )

    def update_highlights(
        self,
        src_id: int,
        hls: List[Union[Tuple[str, int], Tuple[str, int, int, int]]],
        clear_start: Optional[int] = None,
        clear_end: int = -1,
        clear: bool = False,
        async_: bool = True,
    ) -> None:
        """Add or update highlights in batch to avoid unnecessary redraws.

        A `src_id` must have been allocated prior to use of this function. Use
        for instance `nvim.new_highlight_source()` to get a src_id for your
        plugin.

        `hls` should be a list of highlight items. Each item should be a list
        or tuple on the form `("GroupName", linenr, col_start, col_end)` or
        `("GroupName", linenr)` to highlight an entire line.

        By default existing highlights are preserved. Specify a line range with
        clear_start and clear_end to replace highlights in this range. As a
        shorthand, use clear=True to clear the entire buffer before adding the
        new highlights.
        """
        if clear and clear_start is None:
            clear_start = 0
        lua = self._session._get_lua_private()
        lua.update_highlights(self, src_id, hls, clear_start, clear_end, async_=async_)

    @property
    def name(self) -> str:
        """Get the buffer name."""
        return self.request('nvim_buf_get_name')

    @name.setter
    def name(self, value: str) -> None:
        """Set the buffer name. BufFilePre/BufFilePost are triggered."""
        return self.request('nvim_buf_set_name', value)

    @property
    def valid(self) -> bool:
        """Return True if the buffer still exists."""
        return self.request('nvim_buf_is_valid')

    @property
    def loaded(self) -> bool:
        """Return True if the buffer is valid and loaded."""
        return self.request('nvim_buf_is_loaded')

    @property
    def number(self) -> int:
        """Get the buffer number."""
        return self.handle


class Range:

    def __init__(self, buffer: Buffer, start: int, end: int):
        self._buffer = buffer
        self.start = start - 1
        self.end = end - 1

    def __len__(self) -> int:
        return self.end - self.start + 1

    @overload
    def __getitem__(self, idx: int) -> str:
        ...

    @overload
    def __getitem__(self, idx: slice) -> List[str]:
        ...

    def __getitem__(self, idx: Union[int, slice]) -> Union[str, List[str]]:
        if not isinstance(idx, slice):
            return self._buffer[self._normalize_index(idx)]
        start = self._normalize_index(idx.start)
        end = self._normalize_index(idx.stop)
        if start is None:
            start = self.start
        if end is None:
            end = self.end + 1
        return self._buffer[start:end]

    @overload
    def __setitem__(self, idx: int, lines: Optional[str]) -> None:
        ...

    @overload
    def __setitem__(self, idx: slice, lines: Optional[List[str]]) -> None:
        ...

    def __setitem__(
        self, idx: Union[int, slice], lines: Union[None, str, List[str]]
    ) -> None:
        if not isinstance(idx, slice):
            assert not isinstance(lines, list)
            self._buffer[self._normalize_index(idx)] = lines
            return
        start = self._normalize_index(idx.start)
        end = self._normalize_index(idx.stop)
        if start is None:
            start = self.start
        if end is None:
            end = self.end
        self._buffer[start:end + 1] = lines

    def __iter__(self) -> Iterator[str]:
        for i in range(self.start, self.end + 1):
            yield self._buffer[i]

    def append(
        self, lines: Union[str, bytes, List[Union[str, bytes]]], i: Optional[int] = None
    ) -> None:
        i = self._normalize_index(i)
        if i is None:
            i = self.end + 1
        self._buffer.append(lines, i)

    @overload
    def _normalize_index(self, index: int) -> int:
        ...

    @overload
    def _normalize_index(self, index: None) -> None:
        ...

    def _normalize_index(self, index: Optional[int]) -> Optional[int]:
        if index is None:
            return None
        if index < 0:
            index = self.end
        else:
            index += self.start
            if index > self.end:
                index = self.end
        return index