From e383e0ca7811783cebc17a7ca6a7cf1bae6c1d25 2010-08-25 20:15:01
From: epatters <epatters@enthought.com>
Date: 2010-08-25 20:15:01
Subject: [PATCH] Fixed regressions in the pure Python kernel.

---

diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py
index 4680616..cc7e0d2 100644
--- a/IPython/frontend/qt/console/console_widget.py
+++ b/IPython/frontend/qt/console/console_widget.py
@@ -792,9 +792,10 @@ class ConsoleWidget(QtGui.QWidget):
         # Note: this code is adapted from columnize 0.3.2.
         # See http://code.google.com/p/pycolumnize/
 
+        # Calculate the number of characters available.
         width = self._control.viewport().width()
         char_width = QtGui.QFontMetrics(self.font).width(' ')
-        displaywidth = max(5, width / char_width)
+        displaywidth = max(10, (width / char_width) - 1)
 
         # Some degenerate cases.
         size = len(items)
diff --git a/IPython/frontend/qt/console/frontend_widget.py b/IPython/frontend/qt/console/frontend_widget.py
index ae1c4b4..4fbd87e 100644
--- a/IPython/frontend/qt/console/frontend_widget.py
+++ b/IPython/frontend/qt/console/frontend_widget.py
@@ -197,10 +197,7 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
         cursor = self._get_cursor()
         if rep['parent_header']['msg_id'] == self._complete_id and \
                 cursor.position() == self._complete_pos:
-            # The completer tells us what text was actually used for the
-            # matching, so we must move that many characters left to apply the
-            # completions.
-            text = rep['content']['matched_text']
+            text = '.'.join(self._get_context())
             cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
             self._complete_with_items(cursor, rep['content']['matches'])
 
@@ -311,18 +308,15 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
     def _complete(self):
         """ Performs completion at the current cursor location.
         """
-        # We let the kernel split the input line, so we *always* send an empty
-        # text field.  Readline-based frontends do get a real text field which
-        # they can use.
-        text = ''
-        
-        # Send the completion request to the kernel
-        self._complete_id = self.kernel_manager.xreq_channel.complete(
-            text,                                    # text
-            self._get_input_buffer_cursor_line(),    # line
-            self._get_input_buffer_cursor_column(),  # cursor_pos
-            self.input_buffer)                       # block 
-        self._complete_pos = self._get_cursor().position()
+        context = self._get_context()
+        if context:
+            # Send the completion request to the kernel
+            self._complete_id = self.kernel_manager.xreq_channel.complete(
+                '.'.join(context),                       # text
+                self._get_input_buffer_cursor_line(),    # line
+                self._get_input_buffer_cursor_column(),  # cursor_pos
+                self.input_buffer)                       # block 
+            self._complete_pos = self._get_cursor().position()
 
     def _get_banner(self):
         """ Gets a banner to display at the beginning of a session.
diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py
index 6ec6b23..c0f52d7 100644
--- a/IPython/frontend/qt/console/ipython_widget.py
+++ b/IPython/frontend/qt/console/ipython_widget.py
@@ -84,6 +84,19 @@ class IPythonWidget(FrontendWidget):
     # 'BaseFrontendMixin' abstract interface
     #---------------------------------------------------------------------------
 
+    def _handle_complete_reply(self, rep):
+        """ Reimplemented to support IPython's improved completion machinery.
+        """
+        cursor = self._get_cursor()
+        if rep['parent_header']['msg_id'] == self._complete_id and \
+                cursor.position() == self._complete_pos:
+            # The completer tells us what text was actually used for the
+            # matching, so we must move that many characters left to apply the
+            # completions.
+            text = rep['content']['matched_text']
+            cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
+            self._complete_with_items(cursor, rep['content']['matches'])
+
     def _handle_history_reply(self, msg):
         """ Implemented to handle history replies, which are only supported by
             the IPython kernel.
@@ -131,6 +144,22 @@ class IPythonWidget(FrontendWidget):
     # 'FrontendWidget' protected interface
     #---------------------------------------------------------------------------
 
+    def _complete(self):
+        """ Reimplemented to support IPython's improved completion machinery.
+        """
+        # We let the kernel split the input line, so we *always* send an empty
+        # text field. Readline-based frontends do get a real text field which
+        # they can use.
+        text = ''
+        
+        # Send the completion request to the kernel
+        self._complete_id = self.kernel_manager.xreq_channel.complete(
+            text,                                    # text
+            self._get_input_buffer_cursor_line(),    # line
+            self._get_input_buffer_cursor_column(),  # cursor_pos
+            self.input_buffer)                       # block 
+        self._complete_pos = self._get_cursor().position()
+
     def _get_banner(self):
         """ Reimplemented to return IPython's default banner.
         """
diff --git a/IPython/zmq/pykernel.py b/IPython/zmq/pykernel.py
index fc48861..f6cb9b0 100755
--- a/IPython/zmq/pykernel.py
+++ b/IPython/zmq/pykernel.py
@@ -131,7 +131,7 @@ class Kernel(HasTraits):
             self._abort_queue()
 
     def complete_request(self, ident, parent):
-        matches = {'matches' : self.complete(parent),
+        matches = {'matches' : self._complete(parent),
                    'status' : 'ok'}
         completion_msg = self.session.send(self.reply_socket, 'complete_reply',
                                            matches, parent, ident)