Skip to content

Commit

Permalink
use indent from is_complete_reply for autoindent
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Jul 1, 2015
1 parent 0dad3b1 commit 6851c89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 3 additions & 2 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,10 @@ def execute(self, source=None, hidden=False, interactive=False):

# Execute the source or show a continuation prompt if it is incomplete.
if self.execute_on_complete_input:
complete = self._is_complete(source, interactive)
complete, indent = self._is_complete(source, interactive)
else:
complete = not interactive
indent = ''
if hidden:
if complete or not self.execute_on_complete_input:
self._execute(source, hidden)
Expand Down Expand Up @@ -634,7 +635,7 @@ def execute(self, source=None, hidden=False, interactive=False):
cursor = self._get_end_cursor()
cursor.beginEditBlock()
cursor.insertText('\n')
self._insert_continuation_prompt(cursor)
self._insert_continuation_prompt(cursor, indent)
cursor.endEditBlock()

# Do not do this inside the edit block. It works as expected
Expand Down
24 changes: 18 additions & 6 deletions qtconsole/frontend_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,30 @@ def _is_complete(self, source, interactive):
""" Returns whether 'source' can be completely processed and a new
prompt created. When triggered by an Enter/Return key press,
'interactive' is True; otherwise, it is False.
Returns
-------
(complete, indent): (bool, str)
complete is a bool, indicating whether the input is complete or not.
indent is the current indentation string for autoindent.
If complete is True, indent will be '', and should be ignored.
"""
kc = self.blocking_client
if kc is None:
self.log.warn("No blocking client to make is_complete requests")
return False, u''
msg_id = kc.is_complete(source)
while True:
try:
reply = kc.shell_channel.get_msg(block=True, timeout=self.is_complete_timeout)
except Empty:
# assume incomplete output if we get no reply in time
return False
return False, u''
if reply['parent_header'].get('msg_id', None) == msg_id:
return reply['content']['status'] == 'complete'
else:
continue
status = reply['content'].get('status', u'complete')
indent = reply['content'].get('indent', u'')
return status == u'complete', indent

def _execute(self, source, hidden):
""" Execute 'source'. If 'hidden', do not show any output.
Expand Down Expand Up @@ -336,11 +347,12 @@ def _event_filter_console_keypress(self, event):

return super(FrontendWidget, self)._event_filter_console_keypress(event)

def _insert_continuation_prompt(self, cursor):
def _insert_continuation_prompt(self, cursor, indent=''):
""" Reimplemented for auto-indentation.
"""
super(FrontendWidget, self)._insert_continuation_prompt(cursor)
cursor.insertText(' ' * 4)
if indent:
cursor.insertText(indent)

#---------------------------------------------------------------------------
# 'BaseFrontendMixin' abstract interface
Expand Down

0 comments on commit 6851c89

Please sign in to comment.