Skip to content

Commit 25d85a6

Browse files
committed
Implement IPython pinfo (?) and pinfo2 (??), closes #342
1 parent 9d1b9bd commit 25d85a6

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

packages/jupyterlab-lsp/src/transclusions/ipython/overrides.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,46 @@ describe('Default IPython overrides', () => {
165165
it('does not override != comparisons', () => {
166166
let override = line_magics_map.override_for('1 != None');
167167
expect(override).to.equal(null);
168-
})
168+
});
169+
});
170+
171+
describe('IPython help line magics', () => {
172+
let line_magics_map = new ReversibleOverridesMap(
173+
overrides.filter(override => override.scope == 'line')
174+
);
175+
176+
it('overrides pinfo', () => {
177+
let override = line_magics_map.override_for('?int');
178+
expect(override).to.equal("get_ipython().run_line_magic('pinfo', 'int')");
179+
180+
let reverse = line_magics_map.reverse.override_for(override);
181+
expect(reverse).to.equal('?int');
182+
183+
override = line_magics_map.override_for('int?');
184+
expect(override).to.equal(
185+
"get_ipython().run_line_magic('pinfo', 'int')"
186+
);
187+
188+
reverse = line_magics_map.reverse.override_for(override);
189+
expect(reverse).to.equal('int?');
190+
});
191+
192+
it('overrides pinfo2', () => {
193+
let override = line_magics_map.override_for('??int');
194+
expect(override).to.equal(
195+
"get_ipython().run_line_magic('pinfo2', 'int')"
196+
);
197+
198+
let reverse = line_magics_map.reverse.override_for(override);
199+
expect(reverse).to.equal('??int');
200+
201+
override = line_magics_map.override_for('int??');
202+
expect(override).to.equal(
203+
"get_ipython().run_line_magic('pinfo2', 'int')"
204+
);
205+
206+
reverse = line_magics_map.reverse.override_for(override);
207+
expect(reverse).to.equal('int??');
208+
});
169209
});
170210
});

packages/jupyterlab-lsp/src/transclusions/ipython/overrides.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function empty_or_escaped(x: string) {
3737
* x['a = !ls'] = !ls
3838
* is perfectly valid IPython, but regular expressions cannot help here.
3939
*/
40-
export const LINE_MAGIC_PREFIX = '^(\\s*|\\s*\\S+\\s*=\\s*)'
40+
export const LINE_MAGIC_PREFIX = '^(\\s*|\\s*\\S+\\s*=\\s*)';
4141

4242
export let overrides: IScopedCodeOverride[] = [
4343
/**
@@ -55,6 +55,45 @@ export let overrides: IScopedCodeOverride[] = [
5555
scope: 'line'
5656
}
5757
},
58+
{
59+
// note: assignments of pinfo/pinfo2 are not supported by IPython
60+
pattern: '^(\\s*)' + '(\\?{1,2})(\\S+)(\n)?',
61+
replacement: (match, prefix, marks, name, line_break) => {
62+
const cmd = marks == '?' ? 'pinfo' : 'pinfo2';
63+
line_break = line_break || '';
64+
// trick: use single quotes to distinguish
65+
return `${prefix}get_ipython().run_line_magic(\'${cmd}\', \'${name}\')${line_break}`;
66+
},
67+
scope: 'line',
68+
reverse: {
69+
pattern:
70+
"get_ipython\\(\\).run_line_magic\\('(pinfo2?)', '(.*?)'\\)(\n)?",
71+
replacement: (match, cmd, name) => {
72+
const marks = cmd == 'pinfo' ? '?' : '??';
73+
return `${marks}${name}`;
74+
},
75+
scope: 'line'
76+
}
77+
},
78+
{
79+
pattern: '^(\\s*)' + '([^\\?\\s]+)(\\?{1,2})(\n)?',
80+
replacement: (match, prefix, name, marks, line_break) => {
81+
const cmd = marks == '?' ? 'pinfo' : 'pinfo2';
82+
line_break = line_break || '';
83+
// trick: use two spaces to distinguish pinfo using suffix (int?) from the one using prefix (?int)
84+
return `${prefix}get_ipython().run_line_magic(\'${cmd}\', \'${name}\')${line_break}`;
85+
},
86+
scope: 'line',
87+
reverse: {
88+
pattern:
89+
"get_ipython\\(\\).run_line_magic\\('(pinfo2?)', '(.*?)'\\)(\n)?",
90+
replacement: (match, cmd, name) => {
91+
const marks = cmd == 'pinfo' ? '?' : '??';
92+
return `${name}${marks}`;
93+
},
94+
scope: 'line'
95+
}
96+
},
5897
{
5998
pattern: LINE_MAGIC_PREFIX + '%(\\S+)(.*)(\n)?',
6099
replacement: (match, prefix, name, args, line_break) => {

0 commit comments

Comments
 (0)