Show More
@@ -0,0 +1,29 b'' | |||
|
1 | Automatic Vi prompt stripping | |
|
2 | ============================= | |
|
3 | ||
|
4 | When pasting code into IPython, it will strip the leading prompt characters if | |
|
5 | there are any. For example, you can paste the following code into the console - | |
|
6 | it will still work, even though each line is prefixed with prompts (`In`, | |
|
7 | `Out`):: | |
|
8 | ||
|
9 | In [1]: 2 * 2 == 4 | |
|
10 | Out[1]: True | |
|
11 | ||
|
12 | In [2]: print("This still works as pasted") | |
|
13 | ||
|
14 | ||
|
15 | Previously, this was not the case for the Vi-mode prompts:: | |
|
16 | ||
|
17 | In [1]: [ins] In [13]: 2 * 2 == 4 | |
|
18 | ...: Out[13]: True | |
|
19 | ...: | |
|
20 | File "<ipython-input-1-727bb88eaf33>", line 1 | |
|
21 | [ins] In [13]: 2 * 2 == 4 | |
|
22 | ^ | |
|
23 | SyntaxError: invalid syntax | |
|
24 | ||
|
25 | This is now fixed, and Vi prompt prefixes - ``[ins]`` and ``[nav]`` - are | |
|
26 | skipped just as the normal ``In`` would be. | |
|
27 | ||
|
28 | IPython shell can be started in the Vi mode using ``ipython | |
|
29 | --TerminalInteractiveShell.editing_mode=vi`` |
@@ -89,7 +89,30 b' classic_prompt = PromptStripper(' | |||
|
89 | 89 | initial_re=re.compile(r'^>>>( |$)') |
|
90 | 90 | ) |
|
91 | 91 | |
|
92 | ipython_prompt = PromptStripper(re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')) | |
|
92 | ipython_prompt = PromptStripper( | |
|
93 | re.compile( | |
|
94 | r""" | |
|
95 | ^( # Match from the beginning of a line, either: | |
|
96 | ||
|
97 | # 1. First-line prompt: | |
|
98 | ((\[nav\]|\[ins\])?\ )? # Vi editing mode prompt, if it's there | |
|
99 | In\ # The 'In' of the prompt, with a space | |
|
100 | \[\d+\]: # Command index, as displayed in the prompt | |
|
101 | \ # With a mandatory trailing space | |
|
102 | ||
|
103 | | # ... or ... | |
|
104 | ||
|
105 | # 2. The three dots of the multiline prompt | |
|
106 | \s* # All leading whitespace characters | |
|
107 | \.{3,}: # The three (or more) dots | |
|
108 | \ ? # With an optional trailing space | |
|
109 | ||
|
110 | ) | |
|
111 | """, | |
|
112 | re.VERBOSE, | |
|
113 | ) | |
|
114 | ) | |
|
115 | ||
|
93 | 116 | |
|
94 | 117 | def cell_magic(lines): |
|
95 | 118 | if not lines or not lines[0].startswith('%%'): |
@@ -61,10 +61,50 b' for a in range(5):' | |||
|
61 | 61 | print(a ** 2) |
|
62 | 62 | """) |
|
63 | 63 | |
|
64 | ||
|
65 | IPYTHON_PROMPT_VI_INS = ( | |
|
66 | """\ | |
|
67 | [ins] In [11]: def a(): | |
|
68 | ...: 123 | |
|
69 | ...: | |
|
70 | ...: 123 | |
|
71 | """, | |
|
72 | """\ | |
|
73 | def a(): | |
|
74 | 123 | |
|
75 | ||
|
76 | 123 | |
|
77 | """, | |
|
78 | ) | |
|
79 | ||
|
80 | IPYTHON_PROMPT_VI_NAV = ( | |
|
81 | """\ | |
|
82 | [nav] In [11]: def a(): | |
|
83 | ...: 123 | |
|
84 | ...: | |
|
85 | ...: 123 | |
|
86 | """, | |
|
87 | """\ | |
|
88 | def a(): | |
|
89 | 123 | |
|
90 | ||
|
91 | 123 | |
|
92 | """, | |
|
93 | ) | |
|
94 | ||
|
95 | ||
|
64 | 96 | def test_ipython_prompt(): |
|
65 |
for sample, expected in [ |
|
|
66 | nt.assert_equal(ipt2.ipython_prompt(sample.splitlines(keepends=True)), | |
|
67 | expected.splitlines(keepends=True)) | |
|
97 | for sample, expected in [ | |
|
98 | IPYTHON_PROMPT, | |
|
99 | IPYTHON_PROMPT_L2, | |
|
100 | IPYTHON_PROMPT_VI_INS, | |
|
101 | IPYTHON_PROMPT_VI_NAV, | |
|
102 | ]: | |
|
103 | nt.assert_equal( | |
|
104 | ipt2.ipython_prompt(sample.splitlines(keepends=True)), | |
|
105 | expected.splitlines(keepends=True), | |
|
106 | ) | |
|
107 | ||
|
68 | 108 | |
|
69 | 109 | INDENT_SPACES = ("""\ |
|
70 | 110 | if True: |
General Comments 0
You need to be logged in to leave comments.
Login now