linkify stack lines for cell references

This commit is contained in:
Aaron Munger 2023-10-16 11:29:07 -07:00 committed by Aaron Munger
parent 2123a011bf
commit fe8ac3f89e
2 changed files with 12 additions and 4 deletions

View file

@ -29,14 +29,16 @@ export function formatStackTrace(stack: string) {
return cleaned;
}
const fileRegex = /^File\s+(.+):\d+/;
const lineNumberRegex = /([ ->]*?)(\d+)(.*)/;
const cellRegex = /^(Cell\s+In\[(\d+)\])(,\s+line \d+)$/;
function isIpythonStackTrace(stack: string) {
// at least one group will point to the Cell within the notebook
const cellIdentifier = /^Cell In\[\d+\], line \d+$/gm;
return cellIdentifier.test(stack);
}
const fileRegex = /^File\s+(.+):\d+/;
const lineNumberRegex = /([ ->]*?)(\d+)(.*)/;
function linkifyStack(stack: string) {
const lines = stack.split('\n');
@ -49,8 +51,12 @@ function linkifyStack(stack: string) {
if (fileRegex.test(original)) {
const fileMatch = lines[i].match(fileRegex);
fileOrCell = fileMatch![1];
console.log(`matched file ${fileOrCell}`); // REMOVE
continue;
} else if (cellRegex.test(original)) {
lines[i] = original.replace(cellRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = `vscode-notebook-cell:?execution=${executionCount}`;
return `<a href='${fileOrCell}'>${cellLabel}</a>${suffix}`;
});
} else if (!fileOrCell || original.trim() === '') {
// we don't have a location, so don't linkify anything
fileOrCell = undefined;

View file

@ -31,6 +31,8 @@ suite('StackTraceHelper', () => {
'----> 2 raise Exception\n';
const formatted = formatStackTrace(stack);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution=3\'>Cell In[3]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution=3:2\'>2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, formatted);
});