|
5 | 5 | #----------------------------------------------------------------------------- |
6 | 6 |
|
7 | 7 | # Standard library imports |
8 | | -from os.path import commonprefix |
| 8 | +import os.path |
9 | 9 | import re |
10 | 10 | import sys |
11 | 11 | from textwrap import dedent |
|
16 | 16 |
|
17 | 17 | # Local imports |
18 | 18 | from IPython.config.configurable import LoggingConfigurable |
| 19 | +from IPython.core.inputsplitter import ESC_SEQUENCES |
19 | 20 | from IPython.frontend.qt.rich_text import HtmlExporter |
20 | 21 | from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font |
21 | 22 | from IPython.utils.text import columnize |
|
26 | 27 | from completion_plain import CompletionPlain |
27 | 28 | from kill_ring import QtKillRing |
28 | 29 |
|
| 30 | + |
29 | 31 | #----------------------------------------------------------------------------- |
30 | 32 | # Functions |
31 | 33 | #----------------------------------------------------------------------------- |
32 | 34 |
|
| 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 | + |
33 | 56 | def is_letter_or_number(char): |
34 | 57 | """ Returns whether the specified unicode character is a letter or a number. |
35 | 58 | """ |
|
0 commit comments