Skip to content

Commit 51b8bd4

Browse files
committed
Alleviate too aggressive line magics substitution #281
1 parent 30e38bd commit 51b8bd4

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(and vice versa) ([#195][])
99
- restores sorting order-indicating caret icons in diagnostics panel table ([#261][])
1010
- handles document open and change operation ordering more predictably ([#284][])
11+
- fixes some pyflakes issues caused by line magics substitution
1112

1213
[#195]: https://github.com/krassowski/jupyterlab-lsp/issues/195
1314
[#261]: https://github.com/krassowski/jupyterlab-lsp/issues/261

packages/jupyterlab-lsp/src/magics/defaults.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ describe('Default IPython overrides', () => {
104104
expect(reverse).to.equal(LINE_MAGIC_WITH_SPACE);
105105
});
106106

107+
it('overrides x =%ls', () => {
108+
// this is a corner-case as described in
109+
// https://github.com/krassowski/jupyterlab-lsp/issues/281#issuecomment-645286076
110+
let override = line_magics_map.override_for('x =%ls');
111+
expect(override).to.equal(
112+
'x =get_ipython().run_line_magic("ls", "")'
113+
);
114+
});
115+
116+
it('does not override line-magic-like constructs', () => {
117+
let override = line_magics_map.override_for('list("%")');
118+
expect(override).to.equal(null);
119+
});
120+
107121
it('escapes arguments', () => {
108122
const line_magic_with_args = '%MAGIC "arg"';
109123
let override = line_magics_map.override_for(line_magic_with_args);
@@ -122,5 +136,10 @@ describe('Default IPython overrides', () => {
122136
let reverse = line_magics_map.reverse.override_for(override);
123137
expect(reverse).to.equal('!ls -o');
124138
});
139+
140+
it('does not override shell-like constructs', () => {
141+
let override = line_magics_map.override_for('"!ls"');
142+
expect(override).to.equal(null);
143+
});
125144
});
126145
});

packages/jupyterlab-lsp/src/magics/defaults.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ export const language_specific_overrides: IOverridesRegistry = {
3838
// keep the content, keep magic/command name and new line at the end
3939

4040
// note magics do not have to start with the new line, for example x = !ls or x = %ls are both valid.
41+
// x =%ls is also valid. However, percent may also appear in strings, e.g. ls('%').
42+
// Hence: (^|\s|=) for shell commands (!) and line magics (%); see issue #281.
43+
// This does not solve all issues, for example `list(" %ls")` still leads to:
44+
// `list(" get_ipython().run_line_magic("ls")", "")`.
4145
{
42-
pattern: '!(\\S+)(.*)(\n)?',
43-
replacement: 'get_ipython().getoutput("$1$2")$3',
46+
pattern: '(^|\s|=)!(\\S+)(.*)(\n)?',
47+
replacement: '$1get_ipython().getoutput("$2$3")$4',
4448
reverse: {
4549
pattern: 'get_ipython\\(\\).getoutput\\("(.*?)"\\)(\n)?',
4650
replacement: '!$1$2'
@@ -62,11 +66,11 @@ export const language_specific_overrides: IOverridesRegistry = {
6266
}
6367
},
6468
{
65-
pattern: '%(\\S+)(.*)(\n)?',
66-
replacement: (match, name, args, line_break) => {
69+
pattern: '(^|\\s|=)%(\\S+)(.*)(\n)?',
70+
replacement: (match, prefix, name, args, line_break) => {
6771
args = empty_or_escaped(args);
6872
line_break = line_break || '';
69-
return `get_ipython().run_line_magic("${name}", "${args}")${line_break}`;
73+
return `${prefix}get_ipython().run_line_magic("${name}", "${args}")${line_break}`;
7074
},
7175
reverse: {
7276
pattern:

0 commit comments

Comments
 (0)