Show More
@@ -0,0 +1,40 b'' | |||
|
1 | import pytest | |
|
2 | from IPython.terminal.shortcuts import _apply_autosuggest | |
|
3 | ||
|
4 | from unittest.mock import Mock | |
|
5 | ||
|
6 | ||
|
7 | def make_event(text, cursor, suggestion): | |
|
8 | event = Mock() | |
|
9 | event.current_buffer = Mock() | |
|
10 | event.current_buffer.suggestion = Mock() | |
|
11 | event.current_buffer.cursor_position = cursor | |
|
12 | event.current_buffer.suggestion.text = suggestion | |
|
13 | event.current_buffer.document = Mock() | |
|
14 | event.current_buffer.document.get_end_of_line_position = Mock(return_value=0) | |
|
15 | event.current_buffer.document.text = text | |
|
16 | event.current_buffer.document.cursor_position = cursor | |
|
17 | return event | |
|
18 | ||
|
19 | ||
|
20 | @pytest.mark.parametrize( | |
|
21 | "text, cursor, suggestion, called", | |
|
22 | [ | |
|
23 | ("123456", 6, "123456789", True), | |
|
24 | ("123456", 3, "123456789", False), | |
|
25 | ("123456 \n789", 6, "123456789", True), | |
|
26 | ], | |
|
27 | ) | |
|
28 | def test_autosuggest_at_EOL(text, cursor, suggestion, called): | |
|
29 | """ | |
|
30 | test that autosuggest is only applied at end of line. | |
|
31 | """ | |
|
32 | ||
|
33 | event = make_event(text, cursor, suggestion) | |
|
34 | event.current_buffer.insert_text = Mock() | |
|
35 | _apply_autosuggest(event) | |
|
36 | if called: | |
|
37 | event.current_buffer.insert_text.assert_called() | |
|
38 | else: | |
|
39 | event.current_buffer.insert_text.assert_not_called() | |
|
40 | # event.current_buffer.document.get_end_of_line_position.assert_called() |
@@ -32,6 +32,22 b' def cursor_in_leading_ws():' | |||
|
32 | 32 | return (not before) or before.isspace() |
|
33 | 33 | |
|
34 | 34 | |
|
35 | # Needed for to accept autosuggestions in vi insert mode | |
|
36 | def _apply_autosuggest(event): | |
|
37 | """ | |
|
38 | Apply autosuggestion if at end of line. | |
|
39 | """ | |
|
40 | b = event.current_buffer | |
|
41 | d = b.document | |
|
42 | after_cursor = d.text[d.cursor_position :] | |
|
43 | lines = after_cursor.split("\n") | |
|
44 | end_of_current_line = lines[0].strip() | |
|
45 | suggestion = b.suggestion | |
|
46 | if (suggestion is not None) and (suggestion.text) and (end_of_current_line == ""): | |
|
47 | b.insert_text(suggestion.text) | |
|
48 | else: | |
|
49 | nc.end_of_line(event) | |
|
50 | ||
|
35 | 51 | def create_ipython_shortcuts(shell): |
|
36 | 52 | """Set up the prompt_toolkit keyboard shortcuts for IPython""" |
|
37 | 53 | |
@@ -267,15 +283,6 b' def create_ipython_shortcuts(shell):' | |||
|
267 | 283 | |
|
268 | 284 | focused_insert_vi = has_focus(DEFAULT_BUFFER) & vi_insert_mode |
|
269 | 285 | |
|
270 | # Needed for to accept autosuggestions in vi insert mode | |
|
271 | def _apply_autosuggest(event): | |
|
272 | b = event.current_buffer | |
|
273 | suggestion = b.suggestion | |
|
274 | if suggestion is not None and suggestion.text: | |
|
275 | b.insert_text(suggestion.text) | |
|
276 | else: | |
|
277 | nc.end_of_line(event) | |
|
278 | ||
|
279 | 286 | @kb.add("end", filter=has_focus(DEFAULT_BUFFER) & (ebivim | ~vi_insert_mode)) |
|
280 | 287 | def _(event): |
|
281 | 288 | _apply_autosuggest(event) |
General Comments 0
You need to be logged in to leave comments.
Login now