Skip to content

Commit ff7b74d

Browse files
committed
Extend the line magics fix to rpy2
1 parent 73210dd commit ff7b74d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ describe('rpy2 IPython overrides', () => {
7676
expect(reverse).to.equal(line);
7777
});
7878

79+
it('does not substitute magic-like constructs', () => {
80+
let line = 'print("%R -i x")';
81+
let override = line_magics.override_for(line);
82+
expect(override).to.equal(null);
83+
});
84+
7985
it('works with the long form arguments', () => {
8086
let line = '%R --input x';
8187
let override = line_magics.override_for(line);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import {
66
rpy2_reverse_pattern,
77
rpy2_reverse_replacement
88
} from './rpy2';
9+
import { LINE_MAGIC_PREFIX } from '../ipython/overrides';
910

1011
export let overrides: IScopedCodeOverride[] = [
1112
{
1213
// support up to 10 arguments
13-
pattern: '%R' + rpy2_args_pattern(RPY2_MAX_ARGS) + '(.*)(\n)?',
14-
replacement: (match, ...args) => {
14+
pattern:
15+
LINE_MAGIC_PREFIX + '%R' + rpy2_args_pattern(RPY2_MAX_ARGS) + '(.*)(\n)?',
16+
replacement: (match, prefix, ...args) => {
1517
let r = parse_r_args(args, -4);
16-
return `${r.outputs}rpy2.ipython.rmagic.RMagics.R("${r.content}", "${r.others}"${r.inputs})`;
18+
// note: only supports assignment or -o/--output, not both
19+
// TODO assignment like in x = %R 1 should be distinguished from -o
20+
return `${prefix}${r.outputs}rpy2.ipython.rmagic.RMagics.R("${r.content}", "${r.others}"${r.inputs})`;
1721
},
1822
scope: 'line',
1923
reverse: {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ function empty_or_escaped(x: string) {
3636
* This will not always work: e.g.:
3737
* x['a = !ls'] = !ls
3838
* is perfectly valid IPython, but regular expressions cannot help here.
39+
*
40+
* Look behind could be used to avoid capturing the group,
41+
* but at the time of writing support is only at 77%.
3942
*/
4043
export const LINE_MAGIC_PREFIX = '^(\\s*|\\s*\\S+\\s*=\\s*)';
4144

0 commit comments

Comments
 (0)