Skip to content

Commit

Permalink
Fix completion of '%tim' in the Qt console.
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Jun 15, 2012
1 parent 637c65e commit e362fa9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions IPython/core/inputsplitter.py
Expand Up @@ -96,6 +96,9 @@
ESC_QUOTE2 = ';' # Quote all args as a single string, call
ESC_PAREN = '/' # Call first argument with rest of line as arguments

ESCAPE_RE = re.compile("^[" + ESC_SHELL + ESC_HELP + ESC_MAGIC + ESC_QUOTE + \
ESC_QUOTE2 + ESC_PAREN + "]+")

#-----------------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions IPython/frontend/qt/console/console_widget.py
Expand Up @@ -16,6 +16,7 @@

# Local imports
from IPython.config.configurable import LoggingConfigurable
from IPython.core.inputsplitter import ESCAPE_RE
from IPython.frontend.qt.rich_text import HtmlExporter
from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
from IPython.utils.text import columnize
Expand Down Expand Up @@ -875,7 +876,7 @@ def _clear_temporary_buffer(self):
self._control.setUndoRedoEnabled(False)
self._control.setUndoRedoEnabled(True)

def _complete_with_items(self, cursor, items):
def _complete_with_items(self, cursor, items, strip_escape_prefix=False):
""" Performs completion with 'items' at the specified cursor location.
"""
self._cancel_completion()
Expand All @@ -887,7 +888,12 @@ def _complete_with_items(self, cursor, items):

elif len(items) > 1:
current_pos = self._control.textCursor().position()
prefix = commonprefix(items)
# If required, clean up escape characters like %
if strip_escape_prefix:
prefix_items = [ESCAPE_RE.sub("", match) for match in items]
else:
prefix_items = items
prefix = commonprefix(prefix_items)
if prefix:
cursor.setPosition(current_pos, QtGui.QTextCursor.KeepAnchor)
cursor.insertText(prefix)
Expand Down
14 changes: 12 additions & 2 deletions IPython/frontend/qt/console/ipython_widget.py
Expand Up @@ -20,7 +20,7 @@

# Local imports
from IPython.core.inputsplitter import IPythonInputSplitter, \
transform_ipy_prompt
transform_ipy_prompt, ESCAPE_RE
from IPython.utils.traitlets import Bool, Unicode
from frontend_widget import FrontendWidget
import styles
Expand Down Expand Up @@ -158,10 +158,20 @@ def _handle_complete_reply(self, rep):
chop_length = sum(map(len, parts[:sep_count])) + sep_count
matches = [ match[chop_length:] for match in matches ]
offset -= chop_length

# If the completed text starts with an escape sequence (e.g. %),
# we'll ignore that for generating the common prefix to fill in. So
# we move the offset so it's filled from after the escape sequence.
strip_escape_prefix = False
if len(matches) > 1:
m = ESCAPE_RE.match(text)
if m:
strip_escape_prefix = True
offset -= len(m.group(0))

# Move the cursor to the start of the match and complete.
cursor.movePosition(QtGui.QTextCursor.Left, n=offset)
self._complete_with_items(cursor, matches)
self._complete_with_items(cursor, matches, strip_escape_prefix)

def _handle_execute_reply(self, msg):
""" Reimplemented to support prompt requests.
Expand Down

0 comments on commit e362fa9

Please sign in to comment.