Completed
Pull Request — master (#179)
by Björn
40:43 queued 15:44
created

neovim.api.Buffer.get_line_slice()   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
0 ignored issues
show
Configuration introduced by
Unable to import 'common' (unindent does not match any outer indentation level (<string>, line 46))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
14
15
    """A remote Nvim buffer."""
16
17 6
    _api_prefix = "buffer_"
18
19
    def __len__(self):
20
        """Return the number of lines contained in a Buffer."""
21
        return self._session.request('buffer_line_count', self)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
22
23 6
    def __getitem__(self, idx):
24 6
        """Get a buffer line or slice by integer index.
25 6
26
        Indexes may be negative to specify positions from the end of the
27 6
        buffer. For example, -1 is the last line, -2 is the line before that
28
        and so on.
29
30 6
        When retrieving slices, omiting indexes(eg: `buffer[:]`) will bring
31
        the whole buffer.
32 6
        """
33
        if not isinstance(idx, slice):
34 6
            return self._session.request('buffer_get_line', self, idx)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
35
        include_end = False
36
        start = idx.start
37
        end = idx.stop
38
        if start is None:
39
            start = 0
40
        if end is None:
41
            end = -1
42
            include_end = True
43
        return self._session.request('buffer_get_line_slice', self, start, end,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
44 6
                                     True, include_end)
45 6
46 6
    def __setitem__(self, idx, lines):
47 6
        """Replace a buffer line or slice by integer index.
48 6
49 6
        Like with `__getitem__`, indexes may be negative.
50 6
51 6
        When replacing slices, omiting indexes(eg: `buffer[:]`) will replace
52 6
        the whole buffer.
53 6
        """
54 6
        if not isinstance(idx, slice):
55
            if lines is None:
56
                return self._session.request('buffer_del_line', self, idx)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
57 6
            else:
58
                return self._session.request('buffer_set_line', self, idx,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
59
                                             lines)
60
        if lines is None:
61
            lines = []
62
        include_end = False
63
        start = idx.start
64
        end = idx.stop
65 6
        if start is None:
66 6
            start = 0
67 6
        if end is None:
68
            end = -1
69 6
            include_end = True
70
        return self._session.request('buffer_set_line_slice', self, start, end,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
71 6
                                     True, include_end, lines)
72 6
73 6
    def __iter__(self):
74 6
        """Iterate lines of a buffer.
75 6
76 6
        This will retrieve all lines locally before iteration starts. This
77 6
        approach is used because for most cases, the gain is much greater by
78 6
        minimizing the number of API calls by transfering all data needed to
79 6
        work.
80 6
        """
81 6
        lines = self[:]
82
        for line in lines:
83
            yield line
84 6
85
    def __delitem__(self, idx):
86
        """Delete line or slice of lines from the buffer.
87
88
        This is the same as __setitem__(idx, [])
89
        """
90
        if not isinstance(idx, slice):
91
            self.__setitem__(idx, None)
92
        else:
93
            self.__setitem__(idx, [])
94
95
    def get_line_slice(self, start, stop, start_incl, end_incl):
96 6
        """More flexible wrapper for retrieving slices."""
97
        return self._session.request('buffer_get_line_slice', self, start,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
98
                                     stop, start_incl, end_incl)
99
100
    def set_line_slice(self, start, stop, start_incl, end_incl, lines):
101 6
        """More flexible wrapper for replacing slices."""
102 6
        return self._session.request('buffer_set_line_slice', self, start,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
103
                                     stop, start_incl, end_incl, lines)
104 6
105
    def append(self, lines, index=-1):
106 6
        """Append a string or list of lines to the buffer."""
107
        if isinstance(lines, basestring):
108
            lines = [lines]
109
        return self._session.request('buffer_insert', self, index, lines)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
110
111 6
    def mark(self, name):
112
        """Return (row, col) tuple for a named mark."""
113
        return self._session.request('buffer_get_mark', self, name)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
114
115
    def range(self, start, end):
116 6
        """Return a `Range` object, which represents part of the Buffer."""
117
        return Range(self, start, end)
118 6
119 6
    def add_highlight(self, hl_group, line, col_start=0,
120 6
                      col_end=-1, src_id=-1, async=None):
121
        """Add a highlight to the buffer."""
122 6
        if async is None:
123
            async = (src_id != 0)
124 6
        return self._session.request('buffer_add_highlight', self, src_id,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
125
                                     hl_group, line, col_start,
126 6
                                     col_end, async=async)
127
128
    def clear_highlight(self, src_id, line_start=0, line_end=-1, async=True):
129
        """clear highlights from the buffer."""
130 6
        self._session.request('buffer_clear_highlight', self, src_id,
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
131
                              line_start, line_end, async=async)
132
133
    @property
134
    def name(self):
135
        """Get the buffer name."""
136
        return self._session.request('buffer_get_name', self)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
137
138
    @name.setter
139 6
    def name(self, value):
140
        """Set the buffer name. BufFilePre/BufFilePost are triggered."""
141
        return self._session.request('buffer_set_name', self, value)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
142
143
    @property
144 6
    def valid(self):
145
        """Return True if the buffer still exists."""
146
        return self._session.request('buffer_is_valid', self)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
147 6
148
    @property
149 6
    def number(self):
150
        """Get the buffer number."""
151
        return self._session.request('buffer_get_number', self)
0 ignored issues
show
Bug introduced by
The Instance of Buffer does not seem to have a member named _session.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
152 6
153
154 6
class Range(object):
155
    def __init__(self, buffer, start, end):
156
        self._buffer = buffer
157 6
        self.start = start - 1
158
        self.end = end
159 6
160
    def __len__(self):
161
        return self.end - self.start
162 6
163
    def __getitem__(self, idx):
164
        if not isinstance(idx, slice):
165 6
            return self._buffer[self._normalize_index(idx)]
166 6
        start = self._normalize_index(idx.start)
167
        end = self._normalize_index(idx.stop)
168
        if start is None:
169
            start = self.start
170
        if end is None:
171 6
            end = self.end
172
        return self._buffer[start:end]
173
174 6
    def __setitem__(self, idx, lines):
175
        if not isinstance(idx, slice):
176
            self._buffer[self._normalize_index(idx)] = lines
177
            return
178
        start = self._normalize_index(idx.start)
179
        end = self._normalize_index(idx.stop)
180
        if start is None:
181
            start = self.start
182
        if end is None:
183
            end = self.end
184
        self._buffer[start:end] = lines
185 6
186
    def __iter__(self):
187
        for i in range(self.start, self.end):
188
            yield self._buffer[i]
189
190
    def append(self, lines, i=None):
191
        i = self._normalize_index(i)
192
        if i is None:
193
            i = self.end
194
        self._buffer.append(lines, i)
195
196
    def _normalize_index(self, index):
197 6
        if index is None:
198
            return None
199
        if index < 0:
200
            index = self.end - 1
201 6
        else:
202
            index += self.start
203
            if index >= self.end:
204
                index = self.end - 1
205
        return index
206