From e79333f6874c19ca806836c1423a027ad283e85d 2018-03-11 10:08:33 From: Thomas Kluyver Date: 2018-03-11 10:08:33 Subject: [PATCH] Add shell.check_complete() method This allows for fewer pieces to know about input_transformer_manager. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index f7c0f2e..8a78e94 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2988,6 +2988,26 @@ class InteractiveShell(SingletonConfigurable): # For backwards compatibility runcode = run_code + def check_complete(self, code): + """Return whether a block of code is ready to execute, or should be continued + + Parameters + ---------- + source : string + Python input code, which can be multiline. + + Returns + ------- + status : str + One of 'complete', 'incomplete', or 'invalid' if source is not a + prefix of valid code. + indent : str + When status is 'incomplete', this is some whitespace to insert on + the next line of the prompt. + """ + status, nspaces = self.input_transformer_manager.check_complete(code) + return status, ' ' * (nspaces or 0) + #------------------------------------------------------------------------- # Things related to GUI support and pylab #------------------------------------------------------------------------- diff --git a/IPython/sphinxext/ipython_directive.py b/IPython/sphinxext/ipython_directive.py index c85ed9f..f11f0cc 100644 --- a/IPython/sphinxext/ipython_directive.py +++ b/IPython/sphinxext/ipython_directive.py @@ -291,7 +291,6 @@ class EmbeddedSphinxShell(object): self.IP = IP self.user_ns = self.IP.user_ns self.user_global_ns = self.IP.user_global_ns - self.input_transformer_mgr = self.IP.input_transformer_manager self.lines_waiting = [] self.input = '' @@ -331,7 +330,7 @@ class EmbeddedSphinxShell(object): try: sys.stdout = self.cout self.lines_waiting.append(line) - if self.input_transformer_mgr.check_complete()[0] != 'incomplete': + if self.IP.check_complete()[0] != 'incomplete': source_raw = ''.join(self.lines_waiting) self.lines_waiting = [] self.IP.run_cell(source_raw, store_history=store_history) diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index c6b8562..a3b50b6 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -231,11 +231,10 @@ class TerminalInteractiveShell(InteractiveShell): # Fall back to plain non-interactive output for tests. # This is very limited. def prompt(): - itm = self.input_transformer_manager prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens()) lines = [input(prompt_text)] prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens()) - while itm.check_complete('\n'.join(lines))[0] == 'incomplete': + while self.check_complete('\n'.join(lines))[0] == 'incomplete': lines.append( input(prompt_continuation) ) return '\n'.join(lines) self.prompt_for_code = prompt diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py index b52c34e..0e9b871 100644 --- a/IPython/terminal/shortcuts.py +++ b/IPython/terminal/shortcuts.py @@ -119,18 +119,18 @@ def newline_or_execute_outer(shell): check_text = d.text else: check_text = d.text[:d.cursor_position] - status, indent = shell.input_transformer_manager.check_complete(check_text + '\n') + status, indent = shell.check_complete(check_text) if not (d.on_last_line or d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end() ): - b.insert_text('\n' + (' ' * (indent or 0))) + b.insert_text('\n' + indent) return if (status != 'incomplete') and b.accept_action.is_returnable: b.accept_action.validate_and_handle(event.cli, b) else: - b.insert_text('\n' + (' ' * (indent or 0))) + b.insert_text('\n' + indent) return newline_or_execute diff --git a/docs/source/config/details.rst b/docs/source/config/details.rst index 4785c32..31e2609 100644 --- a/docs/source/config/details.rst +++ b/docs/source/config/details.rst @@ -282,8 +282,8 @@ IPython configuration:: else: # insert a newline with auto-indentation... if document.line_count > 1: text = text[:document.cursor_position] - indent = shell.input_transformer_manager.check_complete(text)[1] or 0 - buffer.insert_text('\n' + ' ' * indent) + indent = shell.check_complete(text)[1] + buffer.insert_text('\n' + indent) # if you just wanted a plain newline without any indentation, you # could use `buffer.insert_text('\n')` instead of the lines above