Show More
@@ -357,6 +357,12 b' def create_ipython_shortcuts(shell, for_all_platforms: bool = False):' | |||
|
357 | 357 | kb.add("right", filter=has_suggestion & has_focus(DEFAULT_BUFFER))( |
|
358 | 358 | auto_suggest.accept_character |
|
359 | 359 | ) |
|
360 | kb.add("left", filter=has_suggestion & has_focus(DEFAULT_BUFFER))( | |
|
361 | auto_suggest.accept_and_keep_cursor | |
|
362 | ) | |
|
363 | kb.add("backspace", filter=has_suggestion & has_focus(DEFAULT_BUFFER))( | |
|
364 | auto_suggest.backspace_and_resume_hint | |
|
365 | ) | |
|
360 | 366 | |
|
361 | 367 | # Simple Control keybindings |
|
362 | 368 | key_cmd_dict = { |
@@ -37,6 +37,8 b' class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory):' | |||
|
37 | 37 | |
|
38 | 38 | def connect(self, pt_app: PromptSession): |
|
39 | 39 | self._connected_apps.append(pt_app) |
|
40 | # note: `on_text_changed` could be used for a bit different behaviour | |
|
41 | # on character deletion (i.e. reseting history position on backspace) | |
|
40 | 42 | pt_app.default_buffer.on_text_insert.add_handler(self.reset_history_position) |
|
41 | 43 | |
|
42 | 44 | def get_suggestion( |
@@ -113,35 +115,35 b' class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory):' | |||
|
113 | 115 | # Needed for to accept autosuggestions in vi insert mode |
|
114 | 116 | def accept_in_vi_insert_mode(event: KeyPressEvent): |
|
115 | 117 | """Apply autosuggestion if at end of line.""" |
|
116 | b = event.current_buffer | |
|
117 | d = b.document | |
|
118 | buffer = event.current_buffer | |
|
119 | d = buffer.document | |
|
118 | 120 | after_cursor = d.text[d.cursor_position :] |
|
119 | 121 | lines = after_cursor.split("\n") |
|
120 | 122 | end_of_current_line = lines[0].strip() |
|
121 | suggestion = b.suggestion | |
|
123 | suggestion = buffer.suggestion | |
|
122 | 124 | if (suggestion is not None) and (suggestion.text) and (end_of_current_line == ""): |
|
123 | b.insert_text(suggestion.text) | |
|
125 | buffer.insert_text(suggestion.text) | |
|
124 | 126 | else: |
|
125 | 127 | nc.end_of_line(event) |
|
126 | 128 | |
|
127 | 129 | |
|
128 | 130 | def accept(event: KeyPressEvent): |
|
129 | 131 | """Accept autosuggestion""" |
|
130 | b = event.current_buffer | |
|
131 | suggestion = b.suggestion | |
|
132 | buffer = event.current_buffer | |
|
133 | suggestion = buffer.suggestion | |
|
132 | 134 | if suggestion: |
|
133 | b.insert_text(suggestion.text) | |
|
135 | buffer.insert_text(suggestion.text) | |
|
134 | 136 | else: |
|
135 | 137 | nc.forward_char(event) |
|
136 | 138 | |
|
137 | 139 | |
|
138 | 140 | def accept_word(event: KeyPressEvent): |
|
139 | 141 | """Fill partial autosuggestion by word""" |
|
140 | b = event.current_buffer | |
|
141 | suggestion = b.suggestion | |
|
142 | buffer = event.current_buffer | |
|
143 | suggestion = buffer.suggestion | |
|
142 | 144 | if suggestion: |
|
143 | 145 | t = re.split(r"(\S+\s+)", suggestion.text) |
|
144 | b.insert_text(next((x for x in t if x), "")) | |
|
146 | buffer.insert_text(next((x for x in t if x), "")) | |
|
145 | 147 | else: |
|
146 | 148 | nc.forward_word(event) |
|
147 | 149 | |
@@ -154,6 +156,33 b' def accept_character(event: KeyPressEvent):' | |||
|
154 | 156 | b.insert_text(suggestion.text[0]) |
|
155 | 157 | |
|
156 | 158 | |
|
159 | def accept_and_keep_cursor(event: KeyPressEvent): | |
|
160 | """Accept autosuggestion and keep cursor in place""" | |
|
161 | buffer = event.current_buffer | |
|
162 | old_position = buffer.cursor_position | |
|
163 | suggestion = buffer.suggestion | |
|
164 | if suggestion: | |
|
165 | buffer.insert_text(suggestion.text) | |
|
166 | buffer.cursor_position = old_position | |
|
167 | else: | |
|
168 | nc.backward_char(event) | |
|
169 | ||
|
170 | ||
|
171 | def backspace_and_resume_hint(event: KeyPressEvent): | |
|
172 | """Resume autosuggestions after deleting last character""" | |
|
173 | current_buffer = event.current_buffer | |
|
174 | ||
|
175 | def resume_hinting(buffer: Buffer): | |
|
176 | if buffer.auto_suggest: | |
|
177 | suggestion = buffer.auto_suggest.get_suggestion(buffer, buffer.document) | |
|
178 | if suggestion: | |
|
179 | buffer.suggestion = suggestion | |
|
180 | current_buffer.on_text_changed.remove_handler(resume_hinting) | |
|
181 | ||
|
182 | current_buffer.on_text_changed.add_handler(resume_hinting) | |
|
183 | nc.backward_delete_char(event) | |
|
184 | ||
|
185 | ||
|
157 | 186 | def accept_token(event: KeyPressEvent): |
|
158 | 187 | """Fill partial autosuggestion by token""" |
|
159 | 188 | b = event.current_buffer |
General Comments 0
You need to be logged in to leave comments.
Login now