self.message(repr(obj))
@contextmanager
- def _disable_command_completion(self):
+ def _enable_multiline_completion(self):
completenames = self.completenames
try:
- self.completenames = self.completedefault
+ self.completenames = self.complete_multiline_names
yield
finally:
self.completenames = completenames
buffer = line
if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
# Multi-line mode
- with self._disable_command_completion():
+ with self._enable_multiline_completion():
buffer = line
continue_prompt = "... "
while (code := codeop.compile_command(buffer, '<stdin>', 'single')) is None:
# Complete a simple name.
return [n for n in ns.keys() if n.startswith(text)]
+ def _complete_indentation(self, text, line, begidx, endidx):
+ try:
+ import readline
+ except ImportError:
+ return []
+ # Fill in spaces to form a 4-space indent
+ return [' ' * (4 - readline.get_begidx() % 4)]
+
+ def complete_multiline_names(self, text, line, begidx, endidx):
+ # If text is space-only, the user entered <tab> before any text.
+ # That normally means they want to indent the current line.
+ if not text.strip():
+ return self._complete_indentation(text, line, begidx, endidx)
+ return self.completedefault(text, line, begidx, endidx)
+
def completedefault(self, text, line, begidx, endidx):
if text.startswith("$"):
# Complete convenience variables
self.assertIn(b'42', output)
+ def test_multiline_indent_completion(self):
+ script = textwrap.dedent("""
+ import pdb; pdb.Pdb().set_trace()
+ """)
+
+ # \t should always complete a 4-space indent
+ # This piece of code will raise an IndentationError or a SyntaxError
+ # if the completion is not working as expected
+ input = textwrap.dedent("""\
+ def func():
+ \ta = 1
+ \ta += 1
+ \ta += 1
+ \tif a > 0:
+ a += 1
+ \t\treturn a
+
+ func()
+ c
+ """).encode()
+
+ output = run_pty(script, input)
+
+ self.assertIn(b'4', output)
+ self.assertNotIn(b'Error', output)
+
def load_tests(loader, tests, pattern):
from test import test_pdb