Skip to content

Commit 3ee91c3

Browse files
committed
take #!%... prefix into account for completion
fixes ipython#1767
1 parent 83b6d25 commit 3ee91c3

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

IPython/core/inputsplitter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
ESC_QUOTE2 = ';' # Quote all args as a single string, call
9797
ESC_PAREN = '/' # Call first argument with rest of line as arguments
9898

99+
ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\
100+
ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\
101+
ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ]
102+
99103
#-----------------------------------------------------------------------------
100104
# Utilities
101105
#-----------------------------------------------------------------------------

IPython/frontend/qt/console/console_widget.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#-----------------------------------------------------------------------------
66

77
# Standard library imports
8-
from os.path import commonprefix
8+
import os.path
99
import re
1010
import sys
1111
from textwrap import dedent
@@ -16,6 +16,7 @@
1616

1717
# Local imports
1818
from IPython.config.configurable import LoggingConfigurable
19+
from IPython.core.inputsplitter import ESC_SEQUENCES
1920
from IPython.frontend.qt.rich_text import HtmlExporter
2021
from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
2122
from IPython.utils.text import columnize
@@ -26,10 +27,32 @@
2627
from completion_plain import CompletionPlain
2728
from kill_ring import QtKillRing
2829

30+
2931
#-----------------------------------------------------------------------------
3032
# Functions
3133
#-----------------------------------------------------------------------------
3234

35+
def commonprefix(items):
36+
"""Given a list of pathnames, returns the longest common leading component
37+
38+
Same function as os.path.commonprefix, but don't considere prefix made of
39+
special caracters like #!$%... see
40+
41+
IPython.core.inputsplitter import ESC_SEQUENCES
42+
"""
43+
# the last item will always have the least leading % symbol
44+
prefixes = ''.join(ESC_SEQUENCES)
45+
get_prefix = lambda x : x[0:-len(x.lstrip(prefixes))]
46+
# min / max are first/last in alphabetical order
47+
first_prefix = get_prefix(min(items))
48+
last_prefix = get_prefix(max(items))
49+
50+
# common suffix is (common prefix of reversed items) reversed
51+
prefix = os.path.commonprefix((first_prefix[::-1], last_prefix[::-1]))[::-1]
52+
53+
items = [ s.lstrip(prefixes) for s in items ]
54+
return prefix+os.path.commonprefix(items)
55+
3356
def is_letter_or_number(char):
3457
""" Returns whether the specified unicode character is a letter or a number.
3558
"""

0 commit comments

Comments
 (0)