MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _perform_completion

Method _perform_completion

cmd2/cmd2.py:2411–2567  ·  view source on GitHub ↗

Perform the actual completion, helper function for complete(). :param text: the string prefix we are attempting to match (all matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text

(
        self, text: str, line: str, begidx: int, endidx: int, custom_settings: utils.CustomCompletionSettings | None = None
    )

Source from the content-addressed store, hash-verified

2409 return parser.ap_completer_type
2410
2411 def _perform_completion(
2412 self, text: str, line: str, begidx: int, endidx: int, custom_settings: utils.CustomCompletionSettings | None = None
2413 ) -> Completions:
2414 """Perform the actual completion, helper function for complete().
2415
2416 :param text: the string prefix we are attempting to match (all matches must begin with it)
2417 :param line: the current input line with leading whitespace removed
2418 :param begidx: the beginning index of the prefix text
2419 :param endidx: the ending index of the prefix text
2420 :param custom_settings: optional prepopulated completion settings
2421 :return: a Completions object
2422 """
2423 # If custom_settings is None, then we are completing a command's argument.
2424 # Parse the command line to get the command token.
2425 command = ""
2426 if custom_settings is None:
2427 partial_statement = self.statement_parser.parse_command_only(line)
2428 command = partial_statement.command
2429
2430 # Malformed command line (e.g. quoted command token)
2431 if not command:
2432 return Completions()
2433
2434 expanded_line = partial_statement.command_and_args
2435
2436 if not expanded_line[-1:].isspace():
2437 # Unquoted trailing whitespace gets stripped by parse_command_only().
2438 # Restore it since line is only supposed to be lstripped when passed
2439 # to completer functions according to the Python cmd docs. Regardless
2440 # of what type of whitespace (' ', \n) was stripped, just append spaces
2441 # since shlex treats whitespace characters the same when splitting.
2442 rstripped_len = len(line) - len(line.rstrip())
2443 expanded_line += " " * rstripped_len
2444
2445 # Fix the index values if expanded_line has a different size than line
2446 if len(expanded_line) != len(line):
2447 diff = len(expanded_line) - len(line)
2448 begidx += diff
2449 endidx += diff
2450
2451 # Overwrite line to pass into completers
2452 line = expanded_line
2453
2454 # Get all tokens through the one being completed
2455 tokens, raw_tokens = self.tokens_for_completion(line, begidx, endidx)
2456 if not tokens: # pragma: no cover
2457 return Completions()
2458
2459 # Determine the completer function to use for the command's argument
2460 completer_func: BoundCompleter
2461 if custom_settings is None:
2462 # Check if a macro was entered
2463 if command in self.macros:
2464 completer_func = self.macro_arg_complete
2465
2466 # Check if a command was entered
2467 elif command in self.get_all_commands():
2468 # Get the completer function for this command

Callers 1

completeMethod · 0.95

Calls 10

tokens_for_completionMethod · 0.95
get_all_commandsMethod · 0.95
get_command_funcMethod · 0.95
_redirect_completeMethod · 0.95
CompletionsClass · 0.85
parse_command_onlyMethod · 0.80
to_stringsMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected