def path_completer(self, text, line, begidx, endidx): if len(self.pathCompletionItems) == 0: tree = json.loads(self.getMetaData("*")) if 'metadata' in tree: self.vssTree = tree['metadata'] self.pathCompletionItems = [] childTree = self.get_childtree(text) prefix = "" if "." in text: prefix = text[:text.rfind(".")] + "." for key in childTree: child = childTree[key] if isinstance(child, dict): description = "" if 'description' in child: description = "(" + child['description'] + ")" self.pathCompletionItems.append( CompletionItem(prefix + key, description)) if 'children' in child: self.pathCompletionItems.append( CompletionItem(prefix + key + ".", "(children...)")) return basic_complete(text, line, begidx, endidx, self.pathCompletionItems)
def test_basic_completion_nomatch(cmd2_app): text = 'q' line = 'list_food -f {}'.format(text) endidx = len(line) begidx = endidx - len(text) assert utils.basic_complete(text, line, begidx, endidx, food_item_strs) == []
def completer_func(self, text, my_orig_line, org_begidx, orig_endidx): orig_line = my_orig_line line = orig_line.lstrip() num_stripped = len(orig_line) - len(line) begidx = max(org_begidx - num_stripped, 0) endidx = max(orig_endidx - num_stripped, 0) shortcut_to_restore = '' if begidx == 0: for (shortcut, _) in self.statement_parser.shortcuts: if text.startswith(shortcut): # Save the shortcut to restore later shortcut_to_restore = shortcut # Adjust text and where it begins text = text[len(shortcut_to_restore):] begidx += len(shortcut_to_restore) break from cmd2.argparse_completer import _NoResultsError try: if begidx > 0: self._completion_for_command(text, line, begidx, endidx, shortcut_to_restore) else: from cmd2 import utils match_against = self._get_commands_aliases_and_macros_for_completion( ) self.completion_matches = utils.basic_complete( text, line, begidx, endidx, match_against) except _NoResultsError: self.completion_matches = []
def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]: """ A tab completion function not dependent on instance data. Since custom tab completion operations commonly need to modify cmd2's instance variables related to tab completion, it will be rare to need a completer function. completer_method should be used in those cases. """ match_against = ['a', 'dynamic', 'list', 'goes', 'here'] return basic_complete(text, line, begidx, endidx, match_against)
def test_basic_completion_multiple(cmd2_app): text = '' line = 'list_food -f {}'.format(text) endidx = len(line) begidx = endidx - len(text) matches = sorted(utils.basic_complete(text, line, begidx, endidx, food_item_strs)) assert matches == sorted(food_item_strs)
def student_completer(self, text, line, begidx, endidx): return basic_complete(text, line, begidx, endidx, self.students_list)
def completer_takes_arg_tokens(text: str, line: str, begidx: int, endidx: int, arg_tokens: argparse.Namespace) -> List[str]: """Completer function that receives arg_tokens from AutoCompleter""" match_against = [arg_tokens['parent_arg'][0], arg_tokens['subcommand'][0]] return basic_complete(text, line, begidx, endidx, match_against)
def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]: """Tab completion function""" return basic_complete(text, line, begidx, endidx, completions_from_function)
def completer_method(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: """Tab completion method""" return basic_complete(text, line, begidx, endidx, completions_from_method)
def complete_test_multiline(self, text, line, begidx, endidx): return utils.basic_complete(text, line, begidx, endidx, sport_item_strs)
def complete_test_sort_key(self, text, line, begidx, endidx): num_strs = ['2', '11', '1'] return utils.basic_complete(text, line, begidx, endidx, num_strs)
def complete_test_basic(self, text, line, begidx, endidx): return utils.basic_complete(text, line, begidx, endidx, food_item_strs)
def complete_states(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: assert self is complete_states_expected_self return utils.basic_complete(text, line, begidx, endidx, self.states)
def complete_durian(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: return utils.basic_complete(text, line, begidx, endidx, ['stinks', 'smells', 'disgusting'])