Stores a list of matches and which one is currently selected if any. Also responsible for doing the actual replacement of the original line with the selected match. A MatchesIterator can be `clear`ed to reset match iteration, and `update`ed to set what matches will be iterated over
| 206 | |
| 207 | |
| 208 | class MatchesIterator: |
| 209 | """Stores a list of matches and which one is currently selected if any. |
| 210 | |
| 211 | Also responsible for doing the actual replacement of the original line with |
| 212 | the selected match. |
| 213 | |
| 214 | A MatchesIterator can be `clear`ed to reset match iteration, and |
| 215 | `update`ed to set what matches will be iterated over.""" |
| 216 | |
| 217 | def __init__(self) -> None: |
| 218 | # word being replaced in the original line of text |
| 219 | self.current_word = "" |
| 220 | # possible replacements for current_word |
| 221 | self.matches: list[str] = [] |
| 222 | # which word is currently replacing the current word |
| 223 | self.index = -1 |
| 224 | # cursor position in the original line |
| 225 | self.orig_cursor_offset = -1 |
| 226 | # original line (before match replacements) |
| 227 | self.orig_line = "" |
| 228 | # class describing the current type of completion |
| 229 | self.completer: autocomplete.BaseCompletionType | None = None |
| 230 | self.start: int | None = None |
| 231 | self.end: int | None = None |
| 232 | |
| 233 | def __nonzero__(self) -> bool: |
| 234 | """MatchesIterator is False when word hasn't been replaced yet""" |
| 235 | return self.index != -1 |
| 236 | |
| 237 | def __bool__(self) -> bool: |
| 238 | return self.index != -1 |
| 239 | |
| 240 | @property |
| 241 | def candidate_selected(self) -> bool: |
| 242 | """True when word selected/replaced, False when word hasn't been |
| 243 | replaced yet""" |
| 244 | return bool(self) |
| 245 | |
| 246 | def __iter__(self) -> "MatchesIterator": |
| 247 | return self |
| 248 | |
| 249 | def current(self) -> str: |
| 250 | if self.index == -1: |
| 251 | raise ValueError("No current match.") |
| 252 | return self.matches[self.index] |
| 253 | |
| 254 | def __next__(self) -> str: |
| 255 | self.index = (self.index + 1) % len(self.matches) |
| 256 | return self.matches[self.index] |
| 257 | |
| 258 | def previous(self) -> str: |
| 259 | if self.index <= 0: |
| 260 | self.index = len(self.matches) |
| 261 | self.index -= 1 |
| 262 | |
| 263 | return self.matches[self.index] |
| 264 | |
| 265 | def cur_line(self) -> tuple[int, str]: |