[Jython-checkins] jython: Expand tab to 4 spaces if a continuation line in the console

jim.baker jython-checkins at python.org
Sat Jan 17 19:00:19 CET 2015


https://hg.python.org/jython/rev/f367b4f22d61
changeset:   7534:f367b4f22d61
user:        Jim Baker <jim.baker at rackspace.com>
date:        Sat Jan 17 11:00:09 2015 -0700
summary:
  Expand tab to 4 spaces if a continuation line in the console

Follows the approach suggested in http://bugs.python.org/issue5845

Ideally we would not expand the tab, but insert a tab literal. However
this confuses JLine2 with respect to the cursor position upon
subsequent edits of this line. One can still copy&paste code with tab
literals, although only limited editing is available on the last line
in this scenario.

files:
  Lib/readline.py                       |  22 ++++++++++++++-
  src/org/python/util/JLineConsole.java |   4 +--
  2 files changed, 22 insertions(+), 4 deletions(-)


diff --git a/Lib/readline.py b/Lib/readline.py
--- a/Lib/readline.py
+++ b/Lib/readline.py
@@ -134,7 +134,27 @@
     def complete_handler(buffer, cursor, candidates):
         start = _get_delimited(buffer, cursor)[0]
         delimited = buffer[start:cursor]
-        for state in xrange(100): # TODO arbitrary, what's the number used by gnu readline?
+
+        if _reader.prompt == sys.ps2 and (not delimited or delimited.isspace()):
+            # Insert tab (as expanded to 4 spaces), but only if if
+            # preceding is whitespace/empty and in console
+            # continuation; this is a planned featue for Python 3 per
+            # http://bugs.python.org/issue5845
+            #
+            # Ideally this would not expand tabs, in case of mixed
+            # copy&paste of tab-indented code, however JLine2 gets
+            # confused as to the cursor position if certain, but not
+            # all, subsequent editing if the tab is backspaced
+            candidates.add(" " * 4)
+            return start
+
+        # TODO: if there are a reasonably large number of completions
+        # (need to get specific numbers), CPython 3.4 will show a
+        # message like so:
+        # >>>
+        # Display all 186 possibilities? (y or n)
+        # Currently Jython arbitrarily limits this to 100 and displays them
+        for state in xrange(100):
             completion = None
             try:
                 completion = function(delimited, state)
diff --git a/src/org/python/util/JLineConsole.java b/src/org/python/util/JLineConsole.java
--- a/src/org/python/util/JLineConsole.java
+++ b/src/org/python/util/JLineConsole.java
@@ -9,12 +9,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
-import java.net.MalformedURLException;
-import java.net.URL;
 import java.util.Arrays;
 import java.util.List;
 
-import jline.console.ConsoleKeys;
 import jline.console.ConsoleReader;
 import jline.WindowsTerminal;
 import jline.console.history.FileHistory;
@@ -109,6 +106,7 @@
             reader = new ConsoleReader("jython", in, System.out, null, encoding);
             reader.setKeyMap("jython");
             reader.setHandleUserInterrupt(true);
+            reader.setCopyPasteDetection(true);
 
             // We find the bell too noisy
             reader.setBellEnabled(false);

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list