added line link

This commit is contained in:
aamunger 2023-10-18 10:26:25 -07:00 committed by Aaron Munger
parent 4448839e4d
commit 4c2c49d5b0
2 changed files with 14 additions and 4 deletions

View file

@ -34,7 +34,7 @@ const fileRegex = /File\s+(?:\u001b\[.+?m)?(.+):(\d+)/;
const lineNumberRegex = /((?:\u001b\[.+?m)?[ ->]*?)(\d+)(.*)/;
const cellRegex = /(Cell\s+(?:\u001b\[.+?m)?In\s*\[(\d+)\])(,\s*line \d+)/;
// older versions of IPython ~8.3.0
const inputRegex = /(Input\s+?(?:\u001b\[.+?m)In\s*\[(\d+)\])(.*?)/;
const inputRegex = /(Input\s+?(?:\u001b\[.+?m)In\s*\[(\d+)\])(.*)/;
function isIpythonStackTrace(stack: string) {
return cellRegex.test(stack) || inputRegex.test(stack) || fileRegex.test(stack);
@ -65,6 +65,10 @@ function linkifyStack(stack: string) {
} else if (cellRegex.test(original)) {
lines[i] = original.replace(cellRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = { kind: 'cell', path: `vscode-notebook-cell:?execution_count=${stripFormatting(executionCount)}` };
const lineNumberMatch = /line (\d+)/i.exec(suffix);
if (lineNumberMatch) {
suffix = `, <a href='${fileOrCell.path}&line=${lineNumberMatch[1]}'>line ${lineNumberMatch[1]}</a>`;
}
return `<a href='${fileOrCell.path}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});
@ -72,6 +76,10 @@ function linkifyStack(stack: string) {
} else if (inputRegex.test(original)) {
lines[i] = original.replace(inputRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = { kind: 'cell', path: `vscode-notebook-cell:?execution_count=${stripFormatting(executionCount)}` };
const lineNumberMatch = /<cell line: (\d+)>/i.exec(suffix);
if (lineNumberMatch) {
suffix = `, <a href='${fileOrCell.path}&line=${lineNumberMatch[1]}'>line ${lineNumberMatch[1]}</a>`;
}
return `<a href='${fileOrCell.path}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});

View file

@ -33,6 +33,7 @@ suite('StackTraceHelper', () => {
const formatted = formatStackTrace(stack);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3\'>Cell In[3]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, formatted);
});
@ -42,12 +43,13 @@ suite('StackTraceHelper', () => {
'\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n' +
'\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n' +
'Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m<cell line: 5>\u001b[1;34m()\u001b[0m\n' +
'\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmyLib\u001b[39;00m\n' +
'\u001b[1;32m----> 2\u001b[0m \u001b[43mmyLib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrowEx\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n';
'\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmyLib\u001b[39;00m\n' +
'\u001b[1;32m----> 5\u001b[0m \u001b[43mmyLib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrowEx\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n';
const formatted = formatStackTrace(stack);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2\'>Input In [2]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=2\'>2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=5\'>line 5</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=5\'>5</a>') > 0, formatted);
});
test('IPython stack trace lines without associated location are not linkified', () => {