|
84 | 84 | MAX_EVENTS_POSSIBLY_NOT_PASTE = 20 |
85 | 85 |
|
86 | 86 |
|
| 87 | +class SearchMode(Enum): |
| 88 | + NO_SEARCH = 0 |
| 89 | + INCREMENTAL_SEARCH = 1 |
| 90 | + REVERSE_INCREMENTAL_SEARCH = 2 |
| 91 | + |
| 92 | + |
87 | 93 | class LineType(Enum): |
88 | 94 | """Used when adding a tuple to all_logical_lines, to get input / output values |
89 | 95 | having to actually type/know the strings""" |
@@ -452,9 +458,7 @@ def __init__( |
452 | 458 | # whether auto reloading active |
453 | 459 | self.watching_files = config.default_autoreload |
454 | 460 |
|
455 | | - # 'reverse_incremental_search', 'incremental_search' or None |
456 | | - self.incr_search_mode = None |
457 | | - |
| 461 | + self.incr_search_mode = SearchMode.NO_SEARCH |
458 | 462 | self.incr_search_target = "" |
459 | 463 |
|
460 | 464 | self.original_modules = set(sys.modules.keys()) |
@@ -758,7 +762,7 @@ def process_key_event(self, e: str) -> None: |
758 | 762 | self.incremental_search() |
759 | 763 | elif ( |
760 | 764 | e in (("<BACKSPACE>",) + key_dispatch[self.config.backspace_key]) |
761 | | - and self.incr_search_mode |
| 765 | + and self.incr_search_mode != SearchMode.NO_SEARCH |
762 | 766 | ): |
763 | 767 | self.add_to_incremental_search(self, backspace=True) |
764 | 768 | elif e in self.edit_keys.cut_buffer_edits: |
@@ -808,7 +812,7 @@ def process_key_event(self, e: str) -> None: |
808 | 812 | elif e in key_dispatch[self.config.edit_current_block_key]: |
809 | 813 | self.send_current_block_to_external_editor() |
810 | 814 | elif e in ("<ESC>",): |
811 | | - self.incr_search_mode = None |
| 815 | + self.incr_search_mode = SearchMode.NO_SEARCH |
812 | 816 | elif e in ("<SPACE>",): |
813 | 817 | self.add_normal_character(" ") |
814 | 818 | elif e in CHARACTER_PAIR_MAP.keys(): |
@@ -852,7 +856,11 @@ def insert_char_pair_start(self, e): |
852 | 856 | if not can_lookup_next |
853 | 857 | else self._current_line[self._cursor_offset] |
854 | 858 | ) |
855 | | - if start_of_line or end_of_line or next_char in "})] ": |
| 859 | + if ( |
| 860 | + start_of_line |
| 861 | + or end_of_line |
| 862 | + or (next_char is not None and next_char in "})] ") |
| 863 | + ): |
856 | 864 | self.add_normal_character( |
857 | 865 | CHARACTER_PAIR_MAP[e], narrow_search=False |
858 | 866 | ) |
@@ -891,7 +899,7 @@ def get_last_word(self): |
891 | 899 | ) |
892 | 900 |
|
893 | 901 | def incremental_search(self, reverse=False, include_current=False): |
894 | | - if self.incr_search_mode is None: |
| 902 | + if self.incr_search_mode == SearchMode.NO_SEARCH: |
895 | 903 | self.rl_history.enter(self.current_line) |
896 | 904 | self.incr_search_target = "" |
897 | 905 | else: |
@@ -920,9 +928,9 @@ def incremental_search(self, reverse=False, include_current=False): |
920 | 928 | clear_special_mode=False, |
921 | 929 | ) |
922 | 930 | if reverse: |
923 | | - self.incr_search_mode = "reverse_incremental_search" |
| 931 | + self.incr_search_mode = SearchMode.REVERSE_INCREMENTAL_SEARCH |
924 | 932 | else: |
925 | | - self.incr_search_mode = "incremental_search" |
| 933 | + self.incr_search_mode = SearchMode.INCREMENTAL_SEARCH |
926 | 934 |
|
927 | 935 | def readline_kill(self, e): |
928 | 936 | func = self.edit_keys[e] |
@@ -1172,7 +1180,7 @@ def toggle_file_watch(self): |
1172 | 1180 | def add_normal_character(self, char, narrow_search=True): |
1173 | 1181 | if len(char) > 1 or is_nop(char): |
1174 | 1182 | return |
1175 | | - if self.incr_search_mode: |
| 1183 | + if self.incr_search_mode != SearchMode.NO_SEARCH: |
1176 | 1184 | self.add_to_incremental_search(char) |
1177 | 1185 | else: |
1178 | 1186 | self._set_current_line( |
@@ -1209,9 +1217,9 @@ def add_to_incremental_search(self, char=None, backspace=False): |
1209 | 1217 | self.incr_search_target = self.incr_search_target[:-1] |
1210 | 1218 | else: |
1211 | 1219 | self.incr_search_target += char |
1212 | | - if self.incr_search_mode == "reverse_incremental_search": |
| 1220 | + if self.incr_search_mode == SearchMode.REVERSE_INCREMENTAL_SEARCH: |
1213 | 1221 | self.incremental_search(reverse=True, include_current=True) |
1214 | | - elif self.incr_search_mode == "incremental_search": |
| 1222 | + elif self.incr_search_mode == SearchMode.INCREMENTAL_SEARCH: |
1215 | 1223 | self.incremental_search(include_current=True) |
1216 | 1224 | else: |
1217 | 1225 | raise ValueError("add_to_incremental_search not in a special mode") |
@@ -1419,7 +1427,7 @@ def current_line_formatted(self): |
1419 | 1427 | fs = bpythonparse( |
1420 | 1428 | pygformat(self.tokenize(self.current_line), self.formatter) |
1421 | 1429 | ) |
1422 | | - if self.incr_search_mode: |
| 1430 | + if self.incr_search_mode != SearchMode.NO_SEARCH: |
1423 | 1431 | if self.incr_search_target in self.current_line: |
1424 | 1432 | fs = fmtfuncs.on_magenta(self.incr_search_target).join( |
1425 | 1433 | fs.split(self.incr_search_target) |
@@ -1467,12 +1475,12 @@ def display_line_with_prompt(self): |
1467 | 1475 | """colored line with prompt""" |
1468 | 1476 | prompt = func_for_letter(self.config.color_scheme["prompt"]) |
1469 | 1477 | more = func_for_letter(self.config.color_scheme["prompt_more"]) |
1470 | | - if self.incr_search_mode == "reverse_incremental_search": |
| 1478 | + if self.incr_search_mode == SearchMode.REVERSE_INCREMENTAL_SEARCH: |
1471 | 1479 | return ( |
1472 | 1480 | prompt(f"(reverse-i-search)`{self.incr_search_target}': ") |
1473 | 1481 | + self.current_line_formatted |
1474 | 1482 | ) |
1475 | | - elif self.incr_search_mode == "incremental_search": |
| 1483 | + elif self.incr_search_mode == SearchMode.INCREMENTAL_SEARCH: |
1476 | 1484 | return prompt(f"(i-search)`%s': ") + self.current_line_formatted |
1477 | 1485 | return ( |
1478 | 1486 | prompt(self.ps1) if self.done else more(self.ps2) |
@@ -1905,7 +1913,7 @@ def _set_cursor_offset( |
1905 | 1913 | if reset_rl_history: |
1906 | 1914 | self.rl_history.reset() |
1907 | 1915 | if clear_special_mode: |
1908 | | - self.incr_search_mode = None |
| 1916 | + self.incr_search_mode = SearchMode.NO_SEARCH |
1909 | 1917 | self._cursor_offset = offset |
1910 | 1918 | if update_completion: |
1911 | 1919 | self.update_completion() |
|
0 commit comments