Handle completion for an input line. :param text: the current word that user is typing :param line: current input line :param begidx: beginning index of text :param endidx: ending index of text :param custom_settings: used when not completing the main command
(
self,
text: str,
line: str,
begidx: int,
endidx: int,
custom_settings: utils.CustomCompletionSettings | None = None,
)
| 2567 | return dataclasses.replace(completions, _add_opening_quote=_add_opening_quote, _quote_char=_quote_char) |
| 2568 | |
| 2569 | def complete( |
| 2570 | self, |
| 2571 | text: str, |
| 2572 | line: str, |
| 2573 | begidx: int, |
| 2574 | endidx: int, |
| 2575 | custom_settings: utils.CustomCompletionSettings | None = None, |
| 2576 | ) -> Completions: |
| 2577 | """Handle completion for an input line. |
| 2578 | |
| 2579 | :param text: the current word that user is typing |
| 2580 | :param line: current input line |
| 2581 | :param begidx: beginning index of text |
| 2582 | :param endidx: ending index of text |
| 2583 | :param custom_settings: used when not completing the main command line |
| 2584 | :return: a Completions object |
| 2585 | """ |
| 2586 | try: |
| 2587 | # lstrip the original line |
| 2588 | orig_line = line |
| 2589 | line = orig_line.lstrip() |
| 2590 | num_stripped = len(orig_line) - len(line) |
| 2591 | |
| 2592 | # Calculate new indexes for the stripped line. If the cursor is at a position before the end of a |
| 2593 | # line of spaces, then the following math could result in negative indexes. Enforce a max of 0. |
| 2594 | begidx = max(begidx - num_stripped, 0) |
| 2595 | endidx = max(endidx - num_stripped, 0) |
| 2596 | |
| 2597 | # Shortcuts are not word break characters when completing. Therefore, shortcuts become part |
| 2598 | # of the text variable if there isn't a word break, like a space, after it. We need to remove it |
| 2599 | # from text and update the indexes. This only applies if we are at the beginning of the command line. |
| 2600 | shortcut_to_restore = "" |
| 2601 | if begidx == 0 and custom_settings is None: |
| 2602 | for shortcut, _ in self.statement_parser.shortcuts: |
| 2603 | if text.startswith(shortcut): |
| 2604 | # Save the shortcut to restore later |
| 2605 | shortcut_to_restore = shortcut |
| 2606 | |
| 2607 | # Adjust text and where it begins |
| 2608 | text = text[len(shortcut_to_restore) :] |
| 2609 | begidx += len(shortcut_to_restore) |
| 2610 | break |
| 2611 | else: |
| 2612 | # No shortcut was found. Complete the command token. |
| 2613 | parser = argparse_utils.DEFAULT_ARGUMENT_PARSER(add_help=False) |
| 2614 | parser.add_argument( |
| 2615 | "command", |
| 2616 | metavar="COMMAND", |
| 2617 | help="command, alias, or macro name", |
| 2618 | choices=self._get_commands_aliases_and_macros_choices(), |
| 2619 | ) |
| 2620 | custom_settings = utils.CustomCompletionSettings(parser) |
| 2621 | |
| 2622 | completions = self._perform_completion(text, line, begidx, endidx, custom_settings) |
| 2623 | |
| 2624 | # Check if we need to restore a shortcut in the completion text |
| 2625 | # so it doesn't get erased from the command line. |
| 2626 | if completions and shortcut_to_restore: |