@@ -1856,12 +1856,10 @@ def _perform_completion(
18561856 ArgparseCompleter ,
18571857 )
18581858
1859- unclosed_quote = ''
1860- command : Optional [str ] = None
1861-
1862- # If custom_settings is None, then we are completing a command's arguments
1859+ # If custom_settings is None, then we are completing a command's argument.
1860+ # Parse the command line to get the command token.
1861+ command = ''
18631862 if custom_settings is None :
1864- # Parse the command line
18651863 statement = self .statement_parser .parse_command_only (line )
18661864 command = statement .command
18671865
@@ -1891,7 +1889,7 @@ def _perform_completion(
18911889 if not tokens : # pragma: no cover
18921890 return
18931891
1894- # Determine the completer function to use
1892+ # Determine the completer function to use for the command's argument
18951893 if custom_settings is None :
18961894 # Check if a macro was entered
18971895 if command in self .macros :
@@ -1923,7 +1921,7 @@ def _perform_completion(
19231921 # Not a recognized macro or command
19241922 else :
19251923 # Check if this command should be run as a shell command
1926- if self .default_to_shell and command in utils .get_exes_in_path (cast ( str , command ) ):
1924+ if self .default_to_shell and command in utils .get_exes_in_path (command ):
19271925 completer_func = self .path_complete
19281926 else :
19291927 completer_func = self .completedefault # type: ignore[assignment]
@@ -1941,11 +1939,15 @@ def _perform_completion(
19411939 # Get the token being completed with any opening quote preserved
19421940 raw_completion_token = raw_tokens [- 1 ]
19431941
1942+ # Used for adding quotes to the completion token
1943+ completion_token_quote = ''
1944+
19441945 # Check if the token being completed has an opening quote
19451946 if raw_completion_token and raw_completion_token [0 ] in constants .QUOTES :
19461947
1947- # Since the token is still being completed, we know the opening quote is unclosed
1948- unclosed_quote = raw_completion_token [0 ]
1948+ # Since the token is still being completed, we know the opening quote is unclosed.
1949+ # Save the quote so we can add a matching closing quote later.
1950+ completion_token_quote = raw_completion_token [0 ]
19491951
19501952 # readline still performs word breaks after a quote. Therefore something like quoted search
19511953 # text with a space would have resulted in begidx pointing to the middle of the token we
@@ -1981,7 +1983,7 @@ def _perform_completion(
19811983 self .display_matches = copy .copy (self .completion_matches )
19821984
19831985 # Check if we need to add an opening quote
1984- if not unclosed_quote :
1986+ if not completion_token_quote :
19851987
19861988 add_quote = False
19871989
@@ -2004,19 +2006,19 @@ def _perform_completion(
20042006 if add_quote :
20052007 # Figure out what kind of quote to add and save it as the unclosed_quote
20062008 if any ('"' in match for match in self .completion_matches ):
2007- unclosed_quote = "'"
2009+ completion_token_quote = "'"
20082010 else :
2009- unclosed_quote = '"'
2011+ completion_token_quote = '"'
20102012
2011- self .completion_matches = [unclosed_quote + match for match in self .completion_matches ]
2013+ self .completion_matches = [completion_token_quote + match for match in self .completion_matches ]
20122014
20132015 # Check if we need to remove text from the beginning of tab completions
20142016 elif text_to_remove :
20152017 self .completion_matches = [match .replace (text_to_remove , '' , 1 ) for match in self .completion_matches ]
20162018
20172019 # If we have one result, then add a closing quote if needed and allowed
2018- if len (self .completion_matches ) == 1 and self .allow_closing_quote and unclosed_quote :
2019- self .completion_matches [0 ] += unclosed_quote
2020+ if len (self .completion_matches ) == 1 and self .allow_closing_quote and completion_token_quote :
2021+ self .completion_matches [0 ] += completion_token_quote
20202022
20212023 def complete ( # type: ignore[override]
20222024 self , text : str , state : int , custom_settings : Optional [utils .CustomCompletionSettings ] = None
0 commit comments