87
87
import linecache
88
88
89
89
from contextlib import contextmanager
90
+ from rlcompleter import Completer
90
91
from typing import Union
91
92
92
93
@@ -573,20 +574,14 @@ def displayhook(self, obj):
573
574
self .message (repr (obj ))
574
575
575
576
@contextmanager
576
- def _disable_tab_completion (self ):
577
- if self .use_rawinput and self .completekey == 'tab' :
578
- try :
579
- import readline
580
- except ImportError :
581
- yield
582
- return
583
- try :
584
- readline .parse_and_bind ('tab: self-insert' )
585
- yield
586
- finally :
587
- readline .parse_and_bind ('tab: complete' )
588
- else :
577
+ def _disable_command_completion (self ):
578
+ completenames = self .completenames
579
+ try :
580
+ self .completenames = self .completedefault
589
581
yield
582
+ finally :
583
+ self .completenames = completenames
584
+ return
590
585
591
586
def default (self , line ):
592
587
if line [:1 ] == '!' : line = line [1 :].strip ()
@@ -595,7 +590,7 @@ def default(self, line):
595
590
try :
596
591
if (code := codeop .compile_command (line + '\n ' , '<stdin>' , 'single' )) is None :
597
592
# Multi-line mode
598
- with self ._disable_tab_completion ():
593
+ with self ._disable_command_completion ():
599
594
buffer = line
600
595
continue_prompt = "... "
601
596
while (code := codeop .compile_command (buffer , '<stdin>' , 'single' )) is None :
@@ -771,7 +766,10 @@ def completenames(self, text, line, begidx, endidx):
771
766
if commands :
772
767
return commands
773
768
else :
774
- return self ._complete_expression (text , line , begidx , endidx )
769
+ expressions = self ._complete_expression (text , line , begidx , endidx )
770
+ if expressions :
771
+ return expressions
772
+ return self .completedefault (text , line , begidx , endidx )
775
773
776
774
def _complete_location (self , text , line , begidx , endidx ):
777
775
# Complete a file/module/function location for break/tbreak/clear.
@@ -828,6 +826,21 @@ def _complete_expression(self, text, line, begidx, endidx):
828
826
# Complete a simple name.
829
827
return [n for n in ns .keys () if n .startswith (text )]
830
828
829
+ def completedefault (self , text , line , begidx , endidx ):
830
+ if text .startswith ("$" ):
831
+ # Complete convenience variables
832
+ conv_vars = self .curframe .f_globals .get ('__pdb_convenience_variables' , {})
833
+ return [f"${ name } " for name in conv_vars if name .startswith (text [1 :])]
834
+
835
+ # Use rlcompleter to do the completion
836
+ state = 0
837
+ matches = []
838
+ completer = Completer (self .curframe .f_globals | self .curframe_locals )
839
+ while (match := completer .complete (text , state )) is not None :
840
+ matches .append (match )
841
+ state += 1
842
+ return matches
843
+
831
844
# Command definitions, called by cmdloop()
832
845
# The argument is the remaining string on the command line
833
846
# Return true to exit from the command loop
0 commit comments