Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false “undefined name” message caused by cell magic #1007

Merged
merged 7 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1101,8 +1101,14 @@ Requires JupyterLab `>=3.6.0,<4.0.0a0` and Python 3.8 or newer.
### `jupyter-lsp 0.6.0b0`

- features

- starts language servers on demand
- accepts configuration via Jupyter config system (traitlets) and python
`entry_point`s
- autodetects language servers for bash, CSS, LESS, SASS, Dockerfile, YAML, JS,
TypeScript, JSX, TSX, JSON, YAML

- bugfixes
- fix issue that variables declared in cell magics(%%time, %%capture) are masked(
[#635](https://github.com/jupyter-lsp/jupyterlab-lsp/issues/635)
)
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ describe('Default IPython overrides', () => {
expect(reverse).toBe(cellMagicWithArgs);
});

it('some cell magic commands are unwrapped', () => {
const cellMagicToUnwrap = '%%capture --no-display\ntext';
let override = cellMagicsMap.overrideFor(cellMagicToUnwrap)!;
expect(override).toBe(
'# START_CELL_MAGIC("capture", " --no-display")\ntext\n# END_CELL_MAGIC'
);

let reverse = cellMagicsMap.reverse.overrideFor(override);
expect(reverse).toBe(cellMagicToUnwrap);
});

it('escapes docstrings properly', () => {
let override = cellMagicsMap.overrideFor(CELL_MAGIC_WITH_DOCSTRINGS)!;
expect(override).toBe(
Expand Down
30 changes: 26 additions & 4 deletions packages/jupyterlab-lsp/src/transclusions/ipython/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function emptyOrEscaped(x: string) {
}
}

const MAGICS_TO_UNWRAP = ['time', 'capture'];

function unwrapCellMagic(name: string, firstLine: string, content: string) {
return `# START_CELL_MAGIC("${name}", "${firstLine}")
${content}
# END_CELL_MAGIC`;
}

/**
* Line magics do not have to start with the new line, for example:
* x = !ls
Expand Down Expand Up @@ -131,13 +139,27 @@ export let overrides: IScopedCodeOverride[] = [
firstLine = firstLine.slice(0, -1);
}
content = content.replace(/"""/g, '\\"\\"\\"');
return `get_ipython().run_cell_magic("${name}", "${firstLine}", """${content}""")`;

let replaced: string;
if (MAGICS_TO_UNWRAP.includes(name)) {
replaced = unwrapCellMagic(name, firstLine, content);
} else {
replaced = `get_ipython().run_cell_magic("${name}", "${firstLine}", """${content}""")`;
}
return replaced;
},
scope: 'cell',
reverse: {
pattern:
'^get_ipython\\(\\).run_cell_magic\\("(.*?)", "(.*?)", """([\\s\\S]*)"""\\)',
replacement: (match, name, line, content) => {
pattern: '^get_ipython[\\s\\S]*|^# START_CELL_MAGIC[\\s\\S]*',
replacement: code => {
const regCellMagic = RegExp(
'^get_ipython\\(\\).run_cell_magic\\("(.*?)", "(.*?)", """([\\s\\S]*)"""\\)'
);
const regUnwrapped = RegExp(
'^# START_CELL_MAGIC\\("(.*?)", "(.*?)"\\)\\n([\\s\\S]*)\\n# END_CELL_MAGIC$'
);
let m = code.match(regCellMagic) || code.match(regUnwrapped);
let [name, line, content] = m?.slice(1, 4) || ['', '', ''];
content = content.replace(/\\"\\"\\"/g, '"""');
line = unescape(line);
return `%%${name}${line}\n${content}`;
Expand Down