Fill partial autosuggestion by token
(event: KeyPressEvent)
| 562 | |
| 563 | |
| 564 | def accept_token(event: KeyPressEvent): |
| 565 | """Fill partial autosuggestion by token""" |
| 566 | b = event.current_buffer |
| 567 | suggestion = b.suggestion |
| 568 | |
| 569 | if suggestion: |
| 570 | prefix = _get_query(b.document) |
| 571 | text = prefix + suggestion.text |
| 572 | |
| 573 | tokens: List[Optional[str]] = [None, None, None] |
| 574 | substrings = [""] |
| 575 | i = 0 |
| 576 | |
| 577 | for token in generate_tokens(StringIO(text).readline): |
| 578 | if token.type == tokenize.NEWLINE: |
| 579 | index = len(text) |
| 580 | else: |
| 581 | index = text.index(token[1], len(substrings[-1])) |
| 582 | substrings.append(text[:index]) |
| 583 | tokenized_so_far = substrings[-1] |
| 584 | if tokenized_so_far.startswith(prefix): |
| 585 | if i == 0 and len(tokenized_so_far) > len(prefix): |
| 586 | tokens[0] = tokenized_so_far[len(prefix) :] |
| 587 | substrings.append(tokenized_so_far) |
| 588 | i += 1 |
| 589 | tokens[i] = token[1] |
| 590 | if i == 2: |
| 591 | break |
| 592 | i += 1 |
| 593 | |
| 594 | if tokens[0]: |
| 595 | to_insert: str |
| 596 | insert_text = substrings[-2] |
| 597 | if tokens[1] and len(tokens[1]) == 1: |
| 598 | insert_text = substrings[-1] |
| 599 | to_insert = insert_text[len(prefix) :] |
| 600 | b.insert_text(to_insert) |
| 601 | return |
| 602 | |
| 603 | nc.forward_word(event) |
| 604 | |
| 605 | |
| 606 | Provider = Union[AutoSuggestFromHistory, NavigableAutoSuggestFromHistory, None] |
searching dependent graphs…