Import python-neovim_0.1.11.orig.tar.gz
VĂctor Cuadrado Juan
7 years ago
0 | 0 | Metadata-Version: 1.1 |
1 | 1 | Name: neovim |
2 | Version: 0.1.10 | |
2 | Version: 0.1.11 | |
3 | 3 | Summary: Python client to neovim |
4 | 4 | Home-page: http://github.com/neovim/python-client |
5 | 5 | Author: Thiago de Arruda |
7 | 7 | connecting to and scripting Nvim processes through its msgpack-rpc API. |
8 | 8 | |
9 | 9 | #### Installation |
10 | ||
11 | Supports python 2.7, and 3.3 or later. | |
10 | 12 | |
11 | 13 | ```sh |
12 | 14 | pip2 install neovim |
11 | 11 | stdio_session, tcp_session) |
12 | 12 | from .plugin import (Host, autocmd, command, decode, encoding, function, |
13 | 13 | plugin, rpc_export, shutdown_hook) |
14 | from .util import Version | |
14 | 15 | |
15 | 16 | |
16 | 17 | __all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_session', |
17 | 18 | 'start_host', 'autocmd', 'command', 'encoding', 'decode', |
18 | 'function', 'plugin', 'rpc_export', 'Host', 'Nvim', | |
19 | 'function', 'plugin', 'rpc_export', 'Host', 'Nvim', 'VERSION', | |
19 | 20 | 'shutdown_hook', 'attach', 'setup_logging', 'ErrorResponse') |
21 | ||
22 | ||
23 | VERSION = Version(major=0, minor=1, patch=11, prerelease="dev") | |
20 | 24 | |
21 | 25 | |
22 | 26 | def start_host(session=None): |
63 | 67 | |
64 | 68 | if not session: |
65 | 69 | session = stdio_session() |
66 | host = Host(Nvim.from_session(session)) | |
70 | nvim = Nvim.from_session(session) | |
71 | ||
72 | if nvim.version.api_level < 1: | |
73 | sys.stderr.write("This version of the neovim python package " | |
74 | "requires nvim 0.1.6 or later") | |
75 | sys.exit(1) | |
76 | ||
77 | host = Host(nvim) | |
67 | 78 | host.start(plugins) |
68 | 79 | |
69 | 80 | |
106 | 117 | if 'NVIM_PYTHON_LOG_FILE' in os.environ: |
107 | 118 | prefix = os.environ['NVIM_PYTHON_LOG_FILE'].strip() |
108 | 119 | major_version = sys.version_info[0] |
109 | logfile = '{0}_py{1}_{2}'.format(prefix, major_version, name) | |
120 | logfile = '{}_py{}_{}'.format(prefix, major_version, name) | |
110 | 121 | handler = logging.FileHandler(logfile, 'w') |
111 | 122 | handler.formatter = logging.Formatter( |
112 | 123 | '%(asctime)s [%(levelname)s @ ' |
9 | 9 | basestring = str |
10 | 10 | |
11 | 11 | |
12 | def adjust_index(idx, default=None): | |
13 | """Convert from python indexing convention to nvim indexing convention.""" | |
14 | if idx is None: | |
15 | return default | |
16 | elif idx < 0: | |
17 | return idx - 1 | |
18 | else: | |
19 | return idx | |
20 | ||
21 | ||
12 | 22 | class Buffer(Remote): |
13 | 23 | |
14 | 24 | """A remote Nvim buffer.""" |
15 | 25 | |
16 | _api_prefix = "buffer_" | |
26 | _api_prefix = "nvim_buf_" | |
17 | 27 | |
18 | 28 | def __len__(self): |
19 | 29 | """Return the number of lines contained in a Buffer.""" |
30 | 40 | the whole buffer. |
31 | 41 | """ |
32 | 42 | if not isinstance(idx, slice): |
33 | return self._session.request('buffer_get_line', self, idx) | |
34 | include_end = False | |
35 | start = idx.start | |
36 | end = idx.stop | |
37 | if start is None: | |
38 | start = 0 | |
39 | if end is None: | |
40 | end = -1 | |
41 | include_end = True | |
42 | return self._session.request('buffer_get_line_slice', self, start, end, | |
43 | True, include_end) | |
43 | i = adjust_index(idx) | |
44 | return self.request('nvim_buf_get_lines', i, i + 1, True)[0] | |
45 | start = adjust_index(idx.start, 0) | |
46 | end = adjust_index(idx.stop, -1) | |
47 | return self.request('nvim_buf_get_lines', start, end, False) | |
44 | 48 | |
45 | def __setitem__(self, idx, lines): | |
49 | def __setitem__(self, idx, item): | |
46 | 50 | """Replace a buffer line or slice by integer index. |
47 | 51 | |
48 | 52 | Like with `__getitem__`, indexes may be negative. |
51 | 55 | the whole buffer. |
52 | 56 | """ |
53 | 57 | if not isinstance(idx, slice): |
54 | if lines is None: | |
55 | return self._session.request('buffer_del_line', self, idx) | |
56 | else: | |
57 | return self._session.request('buffer_set_line', self, idx, | |
58 | lines) | |
59 | if lines is None: | |
60 | lines = [] | |
61 | include_end = False | |
62 | start = idx.start | |
63 | end = idx.stop | |
64 | if start is None: | |
65 | start = 0 | |
66 | if end is None: | |
67 | end = -1 | |
68 | include_end = True | |
69 | return self._session.request('buffer_set_line_slice', self, start, end, | |
70 | True, include_end, lines) | |
58 | i = adjust_index(idx) | |
59 | lines = [item] if item is not None else [] | |
60 | return self.request('nvim_buf_set_lines', i, i + 1, True, lines) | |
61 | lines = item if item is not None else [] | |
62 | start = adjust_index(idx.start, 0) | |
63 | end = adjust_index(idx.stop, -1) | |
64 | return self.request('buffer_set_lines', start, end, False, lines) | |
71 | 65 | |
72 | 66 | def __iter__(self): |
73 | 67 | """Iterate lines of a buffer. |
86 | 80 | |
87 | 81 | This is the same as __setitem__(idx, []) |
88 | 82 | """ |
89 | if not isinstance(idx, slice): | |
90 | self.__setitem__(idx, None) | |
91 | else: | |
92 | self.__setitem__(idx, []) | |
93 | ||
94 | def get_line_slice(self, start, stop, start_incl, end_incl): | |
95 | """More flexible wrapper for retrieving slices.""" | |
96 | return self._session.request('buffer_get_line_slice', self, start, | |
97 | stop, start_incl, end_incl) | |
98 | ||
99 | def set_line_slice(self, start, stop, start_incl, end_incl, lines): | |
100 | """More flexible wrapper for replacing slices.""" | |
101 | return self._session.request('buffer_set_line_slice', self, start, | |
102 | stop, start_incl, end_incl, lines) | |
83 | self.__setitem__(idx, None) | |
103 | 84 | |
104 | 85 | def append(self, lines, index=-1): |
105 | 86 | """Append a string or list of lines to the buffer.""" |
106 | 87 | if isinstance(lines, (basestring, bytes)): |
107 | 88 | lines = [lines] |
108 | return self._session.request('buffer_insert', self, index, lines) | |
89 | return self.request('nvim_buf_set_lines', index, index, True, lines) | |
109 | 90 | |
110 | 91 | def mark(self, name): |
111 | 92 | """Return (row, col) tuple for a named mark.""" |
112 | return self.request('buffer_get_mark', name) | |
93 | return self.request('nvim_buf_get_mark', name) | |
113 | 94 | |
114 | 95 | def range(self, start, end): |
115 | 96 | """Return a `Range` object, which represents part of the Buffer.""" |
120 | 101 | """Add a highlight to the buffer.""" |
121 | 102 | if async is None: |
122 | 103 | async = (src_id != 0) |
123 | return self.request('buffer_add_highlight', src_id, hl_group, | |
104 | return self.request('nvim_buf_add_highlight', src_id, hl_group, | |
124 | 105 | line, col_start, col_end, async=async) |
125 | 106 | |
126 | 107 | def clear_highlight(self, src_id, line_start=0, line_end=-1, async=True): |
127 | 108 | """Clear highlights from the buffer.""" |
128 | self.request('buffer_clear_highlight', src_id, | |
109 | self.request('nvim_buf_clear_highlight', src_id, | |
129 | 110 | line_start, line_end, async=async) |
130 | 111 | |
131 | 112 | @property |
132 | 113 | def name(self): |
133 | 114 | """Get the buffer name.""" |
134 | return self.request('buffer_get_name') | |
115 | return self.request('nvim_buf_get_name') | |
135 | 116 | |
136 | 117 | @name.setter |
137 | 118 | def name(self, value): |
138 | 119 | """Set the buffer name. BufFilePre/BufFilePost are triggered.""" |
139 | return self.request('buffer_set_name', value) | |
120 | return self.request('nvim_buf_set_name', value) | |
140 | 121 | |
141 | 122 | @property |
142 | 123 | def valid(self): |
143 | 124 | """Return True if the buffer still exists.""" |
144 | return self.request('buffer_is_valid') | |
125 | return self.request('nvim_buf_is_valid') | |
145 | 126 | |
146 | 127 | @property |
147 | 128 | def number(self): |
148 | 129 | """Get the buffer number.""" |
149 | return self.request('buffer_get_number') | |
130 | return self.request('nvim_buf_get_number') | |
150 | 131 | |
151 | 132 | |
152 | 133 | class Range(object): |
12 | 12 | from .tabpage import Tabpage |
13 | 13 | from .window import Window |
14 | 14 | from ..compat import IS_PYTHON3 |
15 | from ..util import format_exc_skip | |
15 | from ..util import Version, format_exc_skip | |
16 | 16 | |
17 | 17 | __all__ = ('Nvim') |
18 | 18 | |
73 | 73 | self._session = session |
74 | 74 | self.channel_id = channel_id |
75 | 75 | self.metadata = metadata |
76 | version = metadata.get("version", {"api_level": 0}) | |
77 | self.version = Version(**version) | |
76 | 78 | self.types = types |
77 | self.api = RemoteApi(self, 'vim_') | |
78 | self.vars = RemoteMap(self, 'vim_get_var', 'vim_set_var') | |
79 | self.api = RemoteApi(self, 'nvim_') | |
80 | self.vars = RemoteMap(self, 'nvim_get_var', 'nvim_set_var') | |
79 | 81 | self.vvars = RemoteMap(self, 'vim_get_vvar', None) |
80 | self.options = RemoteMap(self, 'vim_get_option', 'vim_set_option') | |
82 | self.options = RemoteMap(self, 'nvim_get_option', 'nvim_set_option') | |
81 | 83 | self.buffers = Buffers(self) |
82 | self.windows = RemoteSequence(self, 'vim_get_windows') | |
83 | self.tabpages = RemoteSequence(self, 'vim_get_tabpages') | |
84 | self.windows = RemoteSequence(self, 'nvim_list_wins') | |
85 | self.tabpages = RemoteSequence(self, 'nvim_list_tabpages') | |
84 | 86 | self.current = Current(self) |
85 | 87 | self.session = CompatibilitySession(self) |
86 | 88 | self.funcs = Funcs(self) |
115 | 117 | |
116 | 118 | is equivalent to |
117 | 119 | |
118 | vim.request('vim_err_write', 'ERROR\n', async=True) | |
119 | vim.request('buffer_get_mark', vim.current.buffer, '.') | |
120 | vim.request('nvim_err_write', 'ERROR\n', async=True) | |
121 | vim.request('nvim_buf_get_mark', vim.current.buffer, '.') | |
120 | 122 | |
121 | 123 | |
122 | 124 | Normally a blocking request will be sent. If the `async` flag is |
156 | 158 | result = request_cb(name, args) |
157 | 159 | except Exception: |
158 | 160 | msg = ("error caught in request handler '{} {}'\n{}\n\n" |
159 | .format(name, args, format_exc_skip(1, 5))) | |
161 | .format(name, args, format_exc_skip(1))) | |
160 | 162 | self._err_cb(msg) |
161 | 163 | raise |
162 | 164 | return walk(self._to_nvim, result) |
168 | 170 | notification_cb(name, args) |
169 | 171 | except Exception: |
170 | 172 | msg = ("error caught in notification handler '{} {}'\n{}\n\n" |
171 | .format(name, args, format_exc_skip(1, 5))) | |
173 | .format(name, args, format_exc_skip(1))) | |
172 | 174 | self._err_cb(msg) |
173 | 175 | raise |
174 | 176 | |
204 | 206 | |
205 | 207 | def subscribe(self, event): |
206 | 208 | """Subscribe to a Nvim event.""" |
207 | return self.request('vim_subscribe', event) | |
209 | return self.request('nvim_subscribe', event) | |
208 | 210 | |
209 | 211 | def unsubscribe(self, event): |
210 | 212 | """Unsubscribe to a Nvim event.""" |
211 | return self.request('vim_unsubscribe', event) | |
213 | return self.request('nvim_unsubscribe', event) | |
212 | 214 | |
213 | 215 | def command(self, string, **kwargs): |
214 | 216 | """Execute a single ex command.""" |
215 | return self.request('vim_command', string, **kwargs) | |
217 | return self.request('nvim_command', string, **kwargs) | |
216 | 218 | |
217 | 219 | def command_output(self, string): |
218 | 220 | """Execute a single ex command and return the output.""" |
219 | return self.request('vim_command_output', string) | |
221 | return self.request('nvim_command_output', string) | |
220 | 222 | |
221 | 223 | def eval(self, string, **kwargs): |
222 | 224 | """Evaluate a vimscript expression.""" |
223 | return self.request('vim_eval', string, **kwargs) | |
225 | return self.request('nvim_eval', string, **kwargs) | |
224 | 226 | |
225 | 227 | def call(self, name, *args, **kwargs): |
226 | 228 | """Call a vimscript function.""" |
227 | return self.request('vim_call_function', name, args, **kwargs) | |
229 | return self.request('nvim_call_function', name, args, **kwargs) | |
228 | 230 | |
229 | 231 | def strwidth(self, string): |
230 | 232 | """Return the number of display cells `string` occupies. |
231 | 233 | |
232 | 234 | Tab is counted as one cell. |
233 | 235 | """ |
234 | return self.request('vim_strwidth', string) | |
236 | return self.request('nvim_strwidth', string) | |
235 | 237 | |
236 | 238 | def list_runtime_paths(self): |
237 | 239 | """Return a list of paths contained in the 'runtimepath' option.""" |
238 | return self.request('vim_list_runtime_paths') | |
240 | return self.request('nvim_list_runtime_paths') | |
239 | 241 | |
240 | 242 | def foreach_rtp(self, cb): |
241 | 243 | """Invoke `cb` for each path in 'runtimepath'. |
245 | 247 | are no longer paths. If stopped in case callable returned non-None, |
246 | 248 | vim.foreach_rtp function returns the value returned by callable. |
247 | 249 | """ |
248 | for path in self.request('vim_list_runtime_paths'): | |
250 | for path in self.request('nvim_list_runtime_paths'): | |
249 | 251 | try: |
250 | 252 | if cb(path) is not None: |
251 | 253 | break |
255 | 257 | def chdir(self, dir_path): |
256 | 258 | """Run os.chdir, then all appropriate vim stuff.""" |
257 | 259 | os_chdir(dir_path) |
258 | return self.request('vim_change_directory', dir_path) | |
260 | return self.request('nvim_set_current_dir', dir_path) | |
259 | 261 | |
260 | 262 | def feedkeys(self, keys, options='', escape_csi=True): |
261 | 263 | """Push `keys` to Nvim user input buffer. |
266 | 268 | - 't': Handle keys as if typed; otherwise they are handled as if coming |
267 | 269 | from a mapping. This matters for undo, opening folds, etc. |
268 | 270 | """ |
269 | return self.request('vim_feedkeys', keys, options, escape_csi) | |
271 | return self.request('nvim_feedkeys', keys, options, escape_csi) | |
270 | 272 | |
271 | 273 | def input(self, bytes): |
272 | 274 | """Push `bytes` to Nvim low level input buffer. |
276 | 278 | written(which can be less than what was requested if the buffer is |
277 | 279 | full). |
278 | 280 | """ |
279 | return self.request('vim_input', bytes) | |
281 | return self.request('nvim_input', bytes) | |
280 | 282 | |
281 | 283 | def replace_termcodes(self, string, from_part=False, do_lt=True, |
282 | 284 | special=True): |
297 | 299 | |
298 | 300 | def out_write(self, msg): |
299 | 301 | """Print `msg` as a normal message.""" |
300 | return self.request('vim_out_write', msg) | |
302 | return self.request('nvim_out_write', msg) | |
301 | 303 | |
302 | 304 | def err_write(self, msg, **kwargs): |
303 | 305 | """Print `msg` as an error message.""" |
304 | return self.request('vim_err_write', msg, **kwargs) | |
306 | return self.request('nvim_err_write', msg, **kwargs) | |
305 | 307 | |
306 | 308 | def quit(self, quit_command='qa!'): |
307 | 309 | """Send a quit command to Nvim. |
338 | 340 | fn(*args, **kwargs) |
339 | 341 | except Exception as err: |
340 | 342 | msg = ("error caught while executing async callback:\n" |
341 | "{0!r}\n{1}\n \nthe call was requested at\n{2}" | |
342 | .format(err, format_exc_skip(1, 5), call_point)) | |
343 | "{!r}\n{}\n \nthe call was requested at\n{}" | |
344 | .format(err, format_exc_skip(1), call_point)) | |
343 | 345 | self._err_cb(msg) |
344 | 346 | raise |
345 | 347 | self._session.threadsafe_call(handler) |
358 | 360 | |
359 | 361 | def __init__(self, nvim): |
360 | 362 | """Initialize a Buffers object with Nvim object `nvim`.""" |
361 | self._fetch_buffers = nvim.api.get_buffers | |
363 | self._fetch_buffers = nvim.api.list_bufs | |
362 | 364 | |
363 | 365 | def __len__(self): |
364 | 366 | """Return the count of buffers.""" |
398 | 400 | |
399 | 401 | @property |
400 | 402 | def line(self): |
401 | return self._session.request('vim_get_current_line') | |
403 | return self._session.request('nvim_get_current_line') | |
402 | 404 | |
403 | 405 | @line.setter |
404 | 406 | def line(self, line): |
405 | return self._session.request('vim_set_current_line', line) | |
407 | return self._session.request('nvim_set_current_line', line) | |
406 | 408 | |
407 | 409 | @property |
408 | 410 | def buffer(self): |
409 | return self._session.request('vim_get_current_buffer') | |
411 | return self._session.request('nvim_get_current_buf') | |
410 | 412 | |
411 | 413 | @buffer.setter |
412 | 414 | def buffer(self, buffer): |
413 | return self._session.request('vim_set_current_buffer', buffer) | |
415 | return self._session.request('nvim_set_current_buf', buffer) | |
414 | 416 | |
415 | 417 | @property |
416 | 418 | def window(self): |
417 | return self._session.request('vim_get_current_window') | |
419 | return self._session.request('nvim_get_current_win') | |
418 | 420 | |
419 | 421 | @window.setter |
420 | 422 | def window(self, window): |
421 | return self._session.request('vim_set_current_window', window) | |
423 | return self._session.request('nvim_set_current_win', window) | |
422 | 424 | |
423 | 425 | @property |
424 | 426 | def tabpage(self): |
425 | return self._session.request('vim_get_current_tabpage') | |
427 | return self._session.request('nvim_get_current_tabpage') | |
426 | 428 | |
427 | 429 | @tabpage.setter |
428 | 430 | def tabpage(self, tabpage): |
429 | return self._session.request('vim_set_current_tabpage', tabpage) | |
431 | return self._session.request('nvim_set_current_tabpage', tabpage) | |
430 | 432 | |
431 | 433 | |
432 | 434 | class Funcs(object): |
7 | 7 | class Tabpage(Remote): |
8 | 8 | """A remote Nvim tabpage.""" |
9 | 9 | |
10 | _api_prefix = "tabpage_" | |
10 | _api_prefix = "nvim_tabpage_" | |
11 | 11 | |
12 | 12 | def __init__(self, *args): |
13 | 13 | """Initialize from session and code_data immutable object. |
16 | 16 | msgpack-rpc calls. It must be immutable for Buffer equality to work. |
17 | 17 | """ |
18 | 18 | super(Tabpage, self).__init__(*args) |
19 | self.windows = RemoteSequence(self, 'tabpage_get_windows') | |
19 | self.windows = RemoteSequence(self, 'nvim_tabpage_list_wins') | |
20 | 20 | |
21 | 21 | @property |
22 | 22 | def window(self): |
23 | 23 | """Get the `Window` currently focused on the tabpage.""" |
24 | return self.request('tabpage_get_window') | |
24 | return self.request('nvim_tabpage_get_win') | |
25 | 25 | |
26 | 26 | @property |
27 | 27 | def valid(self): |
28 | 28 | """Return True if the tabpage still exists.""" |
29 | return self.request('tabpage_is_valid') | |
29 | return self.request('nvim_tabpage_is_valid') | |
30 | ||
31 | @property | |
32 | def number(self): | |
33 | """Get the tabpage number.""" | |
34 | return self.request('nvim_tabpage_get_number') |
8 | 8 | |
9 | 9 | """A remote Nvim window.""" |
10 | 10 | |
11 | _api_prefix = "window_" | |
11 | _api_prefix = "nvim_win_" | |
12 | 12 | |
13 | 13 | @property |
14 | 14 | def buffer(self): |
15 | 15 | """Get the `Buffer` currently being displayed by the window.""" |
16 | return self.request('window_get_buffer') | |
16 | return self.request('nvim_win_get_buf') | |
17 | 17 | |
18 | 18 | @property |
19 | 19 | def cursor(self): |
20 | 20 | """Get the (row, col) tuple with the current cursor position.""" |
21 | return self.request('window_get_cursor') | |
21 | return self.request('nvim_win_get_cursor') | |
22 | 22 | |
23 | 23 | @cursor.setter |
24 | 24 | def cursor(self, pos): |
25 | 25 | """Set the (row, col) tuple as the new cursor position.""" |
26 | return self.request('window_set_cursor', pos) | |
26 | return self.request('nvim_win_set_cursor', pos) | |
27 | 27 | |
28 | 28 | @property |
29 | 29 | def height(self): |
30 | 30 | """Get the window height in rows.""" |
31 | return self.request('window_get_height') | |
31 | return self.request('nvim_win_get_height') | |
32 | 32 | |
33 | 33 | @height.setter |
34 | 34 | def height(self, height): |
35 | 35 | """Set the window height in rows.""" |
36 | return self.request('window_set_height', height) | |
36 | return self.request('nvim_win_set_height', height) | |
37 | 37 | |
38 | 38 | @property |
39 | 39 | def width(self): |
40 | 40 | """Get the window width in rows.""" |
41 | return self.request('window_get_width') | |
41 | return self.request('nvim_win_get_width') | |
42 | 42 | |
43 | 43 | @width.setter |
44 | 44 | def width(self, width): |
45 | 45 | """Set the window height in rows.""" |
46 | return self.request('window_set_width', width) | |
46 | return self.request('nvim_win_set_width', width) | |
47 | 47 | |
48 | 48 | @property |
49 | 49 | def row(self): |
50 | 50 | """0-indexed, on-screen window position(row) in display cells.""" |
51 | return self.request('window_get_position')[0] | |
51 | return self.request('nvim_win_get_position')[0] | |
52 | 52 | |
53 | 53 | @property |
54 | 54 | def col(self): |
55 | 55 | """0-indexed, on-screen window position(col) in display cells.""" |
56 | return self.request('window_get_position')[1] | |
56 | return self.request('nvim_win_get_position')[1] | |
57 | 57 | |
58 | 58 | @property |
59 | 59 | def tabpage(self): |
60 | 60 | """Get the `Tabpage` that contains the window.""" |
61 | return self.request('window_get_tabpage') | |
61 | return self.request('nvim_win_get_tabpage') | |
62 | 62 | |
63 | 63 | @property |
64 | 64 | def valid(self): |
65 | 65 | """Return True if the window still exists.""" |
66 | return self.request('window_is_valid') | |
66 | return self.request('nvim_win_is_valid') | |
67 | ||
68 | @property | |
69 | def number(self): | |
70 | """Get the window number.""" | |
71 | return self.request('nvim_win_get_number') |
84 | 84 | self._on_data = None |
85 | 85 | self._error = None |
86 | 86 | self._init() |
87 | getattr(self, '_connect_{0}'.format(transport_type))(*args) | |
87 | getattr(self, '_connect_{}'.format(transport_type))(*args) | |
88 | 88 | self._start_reading() |
89 | 89 | |
90 | 90 | def connect_tcp(self, address, port): |
148 | 148 | debug('Stopped event loop') |
149 | 149 | |
150 | 150 | def _on_signal(self, signum): |
151 | msg = 'Received {0}'.format(self._signames[signum]) | |
151 | msg = 'Received {}'.format(self._signames[signum]) | |
152 | 152 | debug(msg) |
153 | 153 | if signum == signal.SIGINT and self._transport_type == 'stdio': |
154 | 154 | # When the transport is stdio, we are probably running as a Nvim |
20 | 20 | def _on_connect(self, stream, error): |
21 | 21 | self.stop() |
22 | 22 | if error: |
23 | msg = 'Cannot connect to {0}: {1}'.format( | |
23 | msg = 'Cannot connect to {}: {}'.format( | |
24 | 24 | self._connect_address, pyuv.errno.strerror(error)) |
25 | 25 | self._connection_error = IOError(msg) |
26 | 26 | return |
48 | 48 | |
49 | 49 | def _connect_tcp(self, address, port): |
50 | 50 | stream = pyuv.TCP(self._loop) |
51 | self._connect_address = '{0}:{1}'.format(address, port) | |
51 | self._connect_address = '{}:{}'.format(address, port) | |
52 | 52 | stream.connect((address, port), self._on_connect) |
53 | 53 | |
54 | 54 | def _connect_socket(self, path): |
81 | 81 | return |
82 | 82 | |
83 | 83 | if kwargs: |
84 | raise ValueError("request got unsupported keyword argument(s): {0}" | |
84 | raise ValueError("request got unsupported keyword argument(s): {}" | |
85 | 85 | .format(', '.join(kwargs.keys()))) |
86 | 86 | |
87 | 87 | if self._is_running: |
121 | 121 | gr.switch() |
122 | 122 | |
123 | 123 | if self._setup_exception: |
124 | error('Setup error: {0}'.format(self._setup_exception)) | |
124 | error('Setup error: {}'.format(self._setup_exception)) | |
125 | 125 | raise self._setup_exception |
126 | 126 | |
127 | 127 | # Process all pending requests and notifications |
128 | 128 | while self._pending_messages: |
129 | 129 | msg = self._pending_messages.popleft() |
130 | getattr(self, '_on_{0}'.format(msg[0]))(*msg[1:]) | |
130 | getattr(self, '_on_{}'.format(msg[0]))(*msg[1:]) | |
131 | 131 | self._async_session.run(self._on_request, self._on_notification) |
132 | 132 | self._is_running = False |
133 | 133 | self._request_cb = None |
45 | 45 | register=False, sync=False, eval=None): |
46 | 46 | """Tag a function or plugin method as a Nvim command handler.""" |
47 | 47 | def dec(f): |
48 | f._nvim_rpc_method_name = 'command:{0}'.format(name) | |
48 | f._nvim_rpc_method_name = 'command:{}'.format(name) | |
49 | 49 | f._nvim_rpc_sync = sync |
50 | 50 | f._nvim_bind = True |
51 | 51 | f._nvim_prefix_plugin_path = True |
85 | 85 | def autocmd(name, pattern='*', sync=False, eval=None): |
86 | 86 | """Tag a function or plugin method as a Nvim autocommand handler.""" |
87 | 87 | def dec(f): |
88 | f._nvim_rpc_method_name = 'autocmd:{0}:{1}'.format(name, pattern) | |
88 | f._nvim_rpc_method_name = 'autocmd:{}:{}'.format(name, pattern) | |
89 | 89 | f._nvim_rpc_sync = sync |
90 | 90 | f._nvim_bind = True |
91 | 91 | f._nvim_prefix_plugin_path = True |
110 | 110 | def function(name, range=False, sync=False, eval=None): |
111 | 111 | """Tag a function or plugin method as a Nvim function handler.""" |
112 | 112 | def dec(f): |
113 | f._nvim_rpc_method_name = 'function:{0}'.format(name) | |
113 | f._nvim_rpc_method_name = 'function:{}'.format(name) | |
114 | 114 | f._nvim_rpc_sync = sync |
115 | 115 | f._nvim_bind = True |
116 | 116 | f._nvim_prefix_plugin_path = True |
70 | 70 | except Exception: |
71 | 71 | if sync: |
72 | 72 | msg = ("error caught in request handler '{} {}':\n{}" |
73 | .format(name, args, format_exc_skip(1, 5))) | |
73 | .format(name, args, format_exc_skip(1))) | |
74 | 74 | raise ErrorResponse(msg) |
75 | 75 | else: |
76 | 76 | msg = ("error caught in async handler '{} {}'\n{}\n" |
77 | .format(name, args, format_exc_skip(1, 5))) | |
77 | .format(name, args, format_exc_skip(1))) | |
78 | 78 | self._on_async_err(msg + "\n") |
79 | 79 | |
80 | 80 | def _on_request(self, name, args): |
119 | 119 | for path in plugins: |
120 | 120 | err = None |
121 | 121 | if path in self._loaded: |
122 | error('{0} is already loaded'.format(path)) | |
122 | error('{} is already loaded'.format(path)) | |
123 | 123 | continue |
124 | 124 | try: |
125 | 125 | if path == "script_host.py": |
132 | 132 | self._discover_classes(module, handlers, path) |
133 | 133 | self._discover_functions(module, handlers, path) |
134 | 134 | if not handlers: |
135 | error('{0} exports no handlers'.format(path)) | |
135 | error('{} exports no handlers'.format(path)) | |
136 | 136 | continue |
137 | 137 | self._loaded[path] = {'handlers': handlers, 'module': module} |
138 | 138 | except Exception as e: |
178 | 178 | |
179 | 179 | method = fn._nvim_rpc_method_name |
180 | 180 | if fn._nvim_prefix_plugin_path: |
181 | method = '{0}:{1}'.format(plugin_path, method) | |
181 | method = '{}:{}'.format(plugin_path, method) | |
182 | 182 | |
183 | 183 | fn_wrapped = functools.partial(self._wrap_function, fn, |
184 | 184 | sync, decode, nvim_bind, method) |
186 | 186 | # register in the rpc handler dict |
187 | 187 | if sync: |
188 | 188 | if method in self._request_handlers: |
189 | raise Exception(('Request handler for "{0}" is ' + | |
189 | raise Exception(('Request handler for "{}" is ' + | |
190 | 190 | 'already registered').format(method)) |
191 | 191 | self._request_handlers[method] = fn_wrapped |
192 | 192 | else: |
193 | 193 | if method in self._notification_handlers: |
194 | raise Exception(('Notification handler for "{0}" is ' + | |
194 | raise Exception(('Notification handler for "{}" is ' + | |
195 | 195 | 'already registered').format(method)) |
196 | 196 | self._notification_handlers[method] = fn_wrapped |
197 | 197 | if hasattr(fn, '_nvim_rpc_spec'): |
95 | 95 | self._set_current_range(start, stop) |
96 | 96 | nvim = self.nvim |
97 | 97 | start -= 1 |
98 | stop -= 1 | |
99 | 98 | fname = '_vim_pydo' |
100 | 99 | |
101 | 100 | # define the function |
103 | 102 | exec(function_def, self.module.__dict__) |
104 | 103 | # get the function |
105 | 104 | function = self.module.__dict__[fname] |
106 | while start <= stop: | |
105 | while start < stop: | |
107 | 106 | # Process batches of 5000 to avoid the overhead of making multiple |
108 | 107 | # API calls for every line. Assuming an average line length of 100 |
109 | 108 | # bytes, approximately 488 kilobytes will be transferred per batch, |
110 | 109 | # which can be done very quickly in a single API call. |
111 | 110 | sstart = start |
112 | 111 | sstop = min(start + 5000, stop) |
113 | lines = nvim.current.buffer.get_line_slice(sstart, sstop, True, | |
114 | True) | |
112 | lines = nvim.current.buffer.api.get_lines(sstart, sstop, True) | |
115 | 113 | |
116 | 114 | exception = None |
117 | 115 | newlines = [] |
122 | 120 | # Update earlier lines, and skip to the next |
123 | 121 | if newlines: |
124 | 122 | end = sstart + len(newlines) - 1 |
125 | nvim.current.buffer.set_line_slice(sstart, end, | |
126 | True, True, | |
127 | newlines) | |
123 | nvim.current.buffer.api.set_lines(sstart, end, | |
124 | True, newlines) | |
128 | 125 | sstart += len(newlines) + 1 |
129 | 126 | newlines = [] |
130 | 127 | pass |
137 | 134 | break |
138 | 135 | linenr += 1 |
139 | 136 | |
140 | start = sstop + 1 | |
137 | start = sstop | |
141 | 138 | if newlines: |
142 | end = sstart + len(newlines) - 1 | |
143 | nvim.current.buffer.set_line_slice(sstart, end, True, True, | |
144 | newlines) | |
139 | end = sstart + len(newlines) | |
140 | nvim.current.buffer.api.set_lines(sstart, end, True, newlines) | |
145 | 141 | if exception: |
146 | 142 | raise exception |
147 | 143 | # delete the function |
9 | 9 | for i in range(skip): |
10 | 10 | tb = tb.tb_next |
11 | 11 | return ('\n'.join(format_exception(type, val, tb, limit))).rstrip() |
12 | ||
13 | ||
14 | # Taken from SimpleNamespace in python 3 | |
15 | class Version: | |
16 | ||
17 | """Helper class for version info.""" | |
18 | ||
19 | def __init__(self, **kwargs): | |
20 | """Create the Version object.""" | |
21 | self.__dict__.update(kwargs) | |
22 | ||
23 | def __repr__(self): | |
24 | """Return str representation of the Version.""" | |
25 | keys = sorted(self.__dict__) | |
26 | items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) | |
27 | return "{}({})".format(type(self).__name__, ", ".join(items)) | |
28 | ||
29 | def __eq__(self, other): | |
30 | """Check if version is same as other.""" | |
31 | return self.__dict__ == other.__dict__ |
0 | 0 | Metadata-Version: 1.1 |
1 | 1 | Name: neovim |
2 | Version: 0.1.10 | |
2 | Version: 0.1.11 | |
3 | 3 | Summary: Python client to neovim |
4 | 4 | Home-page: http://github.com/neovim/python-client |
5 | 5 | Author: Thiago de Arruda |
18 | 18 | install_requires.append('greenlet') |
19 | 19 | |
20 | 20 | setup(name='neovim', |
21 | version='0.1.10', | |
21 | version='0.1.11', | |
22 | 22 | description='Python client to neovim', |
23 | 23 | url='http://github.com/neovim/python-client', |
24 | 24 | download_url='https://github.com/neovim/python-client/archive/0.1.10.tar.gz', |
82 | 82 | vim.current.buffer.api.set_var('myvar', 'thetext') |
83 | 83 | eq(vim.current.buffer.api.get_var('myvar'), 'thetext') |
84 | 84 | eq(vim.eval('b:myvar'), 'thetext') |
85 | vim.current.buffer.api.set_line_slice(0,-1,True,True,['alpha', 'beta']) | |
86 | eq(vim.current.buffer.api.get_line_slice(0,-1,True,True), ['alpha', 'beta']) | |
85 | vim.current.buffer.api.set_lines(0,-1,True,['alpha', 'beta']) | |
86 | eq(vim.current.buffer.api.get_lines(0,-1,True), ['alpha', 'beta']) | |
87 | 87 | eq(vim.current.buffer[:], ['alpha', 'beta']) |
88 | 88 | |
89 | 89 |
27 | 27 | ok(tabpage.valid) |
28 | 28 | vim.command('tabclose') |
29 | 29 | ok(not tabpage.valid) |
30 | ||
31 | ||
32 | @with_setup(setup=cleanup) | |
33 | def test_number(): | |
34 | curnum = vim.current.tabpage.number | |
35 | vim.command('tabnew') | |
36 | eq(vim.current.tabpage.number, curnum + 1) | |
37 | vim.command('tabnew') | |
38 | eq(vim.current.tabpage.number, curnum + 2) |
93 | 93 | ok(window.valid) |
94 | 94 | vim.command('q') |
95 | 95 | ok(not window.valid) |
96 | ||
97 | ||
98 | @with_setup(setup=cleanup) | |
99 | def test_number(): | |
100 | curnum = vim.current.window.number | |
101 | vim.command('bot split') | |
102 | eq(vim.current.window.number, curnum + 1) | |
103 | vim.command('bot split') | |
104 | eq(vim.current.window.number, curnum + 2) |