Skip to content

Commit

Permalink
Add cell debug controller
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Sep 3, 2021
1 parent 959a412 commit 82ea82d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ import { sendTelemetryEvent } from '../../telemetry';
import { DebuggingTelemetry } from '../constants';
import { DebuggingDelegate, IKernelDebugAdapter } from '../types';

export class DebugCellController implements DebuggingDelegate {
constructor(
private readonly debugAdapter: IKernelDebugAdapter,
public readonly debugCell: NotebookCell,
private readonly kernel: IKernel,
private readonly commandManager: ICommandManager,
) {
sendTelemetryEvent(DebuggingTelemetry.successfullyStartedRunAndDebugCell);
}

public async willSendEvent(_msg: DebugProtocolMessage): Promise<boolean> {
return false;
}

public async willSendRequest(request: DebugProtocol.Request): Promise<void> {
if (request.command === 'configurationDone') {
await cellDebugSetup(this.kernel, this.debugAdapter, this.debugCell);

void this.commandManager.executeCommand('notebook.cell.execute', {
ranges: [{ start: this.debugCell.index, end: this.debugCell.index + 1 }],
document: this.debugCell.document.uri
});
}
}
}

export class RunByLineController implements DebuggingDelegate {
private lastPausedThreadId: number | undefined;

Expand Down Expand Up @@ -85,15 +111,7 @@ export class RunByLineController implements DebuggingDelegate {
}

private async initializeExecute() {
// remove this if when https://github.com/microsoft/debugpy/issues/706 is fixed and ipykernel ships it
// executing this code restarts debugpy and fixes https://github.com/microsoft/vscode-jupyter/issues/7251
if (this.kernel) {
const code = 'import debugpy\ndebugpy.debug_this_thread()';
await this.kernel.executeHidden(code, this.debugCell.notebook);
}

// put breakpoint at the beginning of the cell
await this.debugAdapter.dumpCell(this.debugCell.index);
await cellDebugSetup(this.kernel, this.debugAdapter, this.debugCell);

// This will save the code lines of the cell in lineList (so ignore comments and emtpy lines)
// Its done to set the Run by Line breakpoint on the first code line
Expand Down Expand Up @@ -138,3 +156,14 @@ export class RunByLineController implements DebuggingDelegate {
});
}
}

async function cellDebugSetup(kernel: IKernel, debugAdapter: IKernelDebugAdapter, debugCell: NotebookCell): Promise<void> {
// remove this if when https://github.com/microsoft/debugpy/issues/706 is fixed and ipykernel ships it
// executing this code restarts debugpy and fixes https://github.com/microsoft/vscode-jupyter/issues/7251
if (kernel) {
const code = 'import debugpy\ndebugpy.debug_this_thread()';
await kernel.executeHidden(code, debugCell.notebook);
}

await debugAdapter.dumpCell(debugCell.index);
}
17 changes: 13 additions & 4 deletions src/client/debugger/jupyter/debuggingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { DebuggingTelemetry, pythonKernelDebugAdapter } from '../constants';
import { IPythonInstaller } from '../../api/types';
import { sendTelemetryEvent } from '../../telemetry';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { RunByLineController } from './runByLineController';
import { DebugCellController, RunByLineController } from './debugControllers';

class Debugger {
private resolveFunc?: (value: DebugSession) => void;
Expand Down Expand Up @@ -348,6 +348,15 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
);
adapter.setDebuggingDelegate(controller);
this.notebookToRunByLineController.set(debug.document, controller);
} else if (config.__mode === KernelDebugMode.Cell && typeof config.__cellIndex === 'number') {
const cell = activeDoc.cellAt(config.__cellIndex);
const controller = new DebugCellController(
adapter,
cell,
kernel!,
this.commandManager
);
adapter.setDebuggingDelegate(controller);
}

this.disposables.push(
Expand Down Expand Up @@ -409,9 +418,9 @@ export class DebuggingManager implements IExtensionSingleActivationService, IDeb
this.pythonInstaller.isProductVersionCompatible(Product.ipykernel, '>=6.0.0', interpreter);
const status = waitingMessage
? await this.appShell.withProgress(
{ location: ProgressLocation.Notification, title: waitingMessage },
checkCompatible
)
{ location: ProgressLocation.Notification, title: waitingMessage },
checkCompatible
)
: await checkCompatible();
const result = status === ProductInstallStatus.Installed;

Expand Down
4 changes: 0 additions & 4 deletions src/client/debugger/jupyter/kernelDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID
this.debugCellUri = notebookDocument.cellAt(configuration.__cellIndex!)?.document.uri;
}

if (configuration.__mode === KernelDebugMode.Cell) {
sendTelemetryEvent(DebuggingTelemetry.successfullyStartedRunAndDebugCell);
}

this.disposables.push(
this.jupyterSession.onIOPubMessage(async (msg: KernelMessage.IIOPubMessage) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 82ea82d

Please sign in to comment.