|
| 1 | +"""API for working with Nvim buffers.""" |
| 2 | +from .common import Remote, RemoteMap |
| 3 | +from ..compat import IS_PYTHON3 |
| 4 | + |
| 5 | + |
| 6 | +__all__ = ('Buffer') |
| 7 | + |
| 8 | + |
| 9 | +if IS_PYTHON3: |
| 10 | + basestring = str |
| 11 | + |
| 12 | + |
| 13 | +class Buffer(Remote): |
| 14 | + |
| 15 | + """A remote Nvim buffer.""" |
| 16 | + |
| 17 | + 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 | + self._session = session |
| 24 | + self.code_data = code_data |
| 25 | + self.vars = RemoteMap(session, 'buffer_get_var', 'buffer_set_var', |
| 26 | + self) |
| 27 | + self.options = RemoteMap(session, 'buffer_get_option', |
| 28 | + 'buffer_set_option', self) |
| 29 | + |
| 30 | + def __len__(self): |
| 31 | + """Return the number of lines contained in a Buffer.""" |
| 32 | + return self._session.request('buffer_get_length', self) |
| 33 | + |
| 34 | + 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 | + if not isinstance(idx, slice): |
| 45 | + return self._session.request('buffer_get_line', self, idx) |
| 46 | + include_end = False |
| 47 | + start = idx.start |
| 48 | + end = idx.stop |
| 49 | + if start is None: |
| 50 | + start = 0 |
| 51 | + if end is None: |
| 52 | + end = -1 |
| 53 | + include_end = True |
| 54 | + return self._session.request('buffer_get_slice', self, start, end, |
| 55 | + True, include_end) |
| 56 | + |
| 57 | + 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 | + if not isinstance(idx, slice): |
| 66 | + if lines is None: |
| 67 | + return self._session.request('buffer_del_line', self, idx) |
| 68 | + else: |
| 69 | + return self._session.request('buffer_set_line', self, idx, |
| 70 | + lines) |
| 71 | + if lines is None: |
| 72 | + lines = [] |
| 73 | + include_end = False |
| 74 | + start = idx.start |
| 75 | + end = idx.stop |
| 76 | + if start is None: |
| 77 | + start = 0 |
| 78 | + if end is None: |
| 79 | + end = -1 |
| 80 | + include_end = True |
| 81 | + return self._session.request('buffer_set_slice', self, start, end, |
| 82 | + True, include_end, lines) |
| 83 | + |
| 84 | + 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 | + def get_slice(self, start, stop, start_incl, end_incl): |
| 97 | + """More flexible wrapper for retrieving slices.""" |
| 98 | + return self._session.request('buffer_get_slice', self, start, stop, |
| 99 | + start_incl, end_incl) |
| 100 | + |
| 101 | + def set_slice(self, start, stop, start_incl, end_incl, lines): |
| 102 | + """More flexible wrapper for replacing slices.""" |
| 103 | + return self._session.request('buffer_set_slice', self, start, stop, |
| 104 | + start_incl, end_incl, lines) |
| 105 | + |
| 106 | + def append(self, lines, index=-1): |
| 107 | + """Append a string or list of lines to the buffer.""" |
| 108 | + if isinstance(lines, basestring): |
| 109 | + lines = [lines] |
| 110 | + return self._session.request('buffer_insert', self, index, lines) |
| 111 | + |
| 112 | + def mark(self, name): |
| 113 | + """Return (row, col) tuple for a named mark.""" |
| 114 | + return self._session.request('buffer_get_mark', self, name) |
| 115 | + |
| 116 | + def range(self, start, end): |
| 117 | + """Return a `Range` object, which represents part of the Buffer.""" |
| 118 | + return Range(self, start, end) |
| 119 | + |
| 120 | + @property |
| 121 | + def name(self): |
| 122 | + """Get the buffer name.""" |
| 123 | + return self._session.request('buffer_get_name', self) |
| 124 | + |
| 125 | + @name.setter |
| 126 | + def name(self, value): |
| 127 | + """Set the buffer name. BufFilePre/BufFilePost are triggered.""" |
| 128 | + return self._session.request('buffer_set_name', self, value) |
| 129 | + |
| 130 | + @property |
| 131 | + def valid(self): |
| 132 | + """Return True if the buffer still exists.""" |
| 133 | + return self._session.request('buffer_is_valid', self) |
| 134 | + |
| 135 | + @property |
| 136 | + def number(self): |
| 137 | + """Get the buffer number.""" |
| 138 | + return self._session.request('buffer_get_number', self) |
| 139 | + |
| 140 | + |
| 141 | +class Range(object): |
| 142 | + def __init__(self, buffer, start, end): |
| 143 | + self._buffer = buffer |
| 144 | + self.start = start - 1 |
| 145 | + self.end = end |
| 146 | + |
| 147 | + def __len__(self): |
| 148 | + return self.end - self.start |
| 149 | + |
| 150 | + def __getitem__(self, idx): |
| 151 | + if not isinstance(idx, slice): |
| 152 | + return self._buffer[self._normalize_index(idx)] |
| 153 | + start = self._normalize_index(idx.start) |
| 154 | + end = self._normalize_index(idx.stop) |
| 155 | + if start is None: |
| 156 | + start = self.start |
| 157 | + if end is None: |
| 158 | + end = self.end |
| 159 | + return self._buffer[start:end] |
| 160 | + |
| 161 | + def __setitem__(self, idx, lines): |
| 162 | + if not isinstance(idx, slice): |
| 163 | + self._buffer[self._normalize_index(idx)] = lines |
| 164 | + return |
| 165 | + start = self._normalize_index(idx.start) |
| 166 | + end = self._normalize_index(idx.stop) |
| 167 | + if start is None: |
| 168 | + start = self.start |
| 169 | + if end is None: |
| 170 | + end = self.end |
| 171 | + self._buffer[start:end] = lines |
| 172 | + |
| 173 | + def __iter__(self): |
| 174 | + for i in range(self.start, self.end): |
| 175 | + yield self._buffer[i] |
| 176 | + |
| 177 | + def append(self, lines, i=None): |
| 178 | + i = self._normalize_index(i) |
| 179 | + if i is None: |
| 180 | + i = self.end |
| 181 | + self._buffer.append(lines, i) |
| 182 | + |
| 183 | + def _normalize_index(self, index): |
| 184 | + if index is None: |
| 185 | + return None |
| 186 | + if index < 0: |
| 187 | + index = self.end - 1 |
| 188 | + else: |
| 189 | + index += self.start |
| 190 | + if index >= self.end: |
| 191 | + index = self.end - 1 |
| 192 | + return index |
0 commit comments