|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as lsp from "vscode-languageclient"; |
| 3 | +import type { BaseLanguageClient } from "vscode-languageclient"; |
| 4 | + |
| 5 | +import { notebookType } from "./types.ts"; |
| 6 | +import { Logger } from "./logging.ts"; |
| 7 | +import { executeCommand } from "./commands.ts"; |
| 8 | + |
| 9 | +export function debugAdapter( |
| 10 | + client: BaseLanguageClient, |
| 11 | + options: { signal: AbortSignal }, |
| 12 | +) { |
| 13 | + Logger.info("Debug.Init", "Registering debug adapter"); |
| 14 | + |
| 15 | + const disposeFactory = vscode.debug.registerDebugAdapterDescriptorFactory( |
| 16 | + "marimo", |
| 17 | + { |
| 18 | + createDebugAdapterDescriptor: createDebugAdapterDescriptor.bind( |
| 19 | + null, |
| 20 | + client, |
| 21 | + ), |
| 22 | + }, |
| 23 | + ); |
| 24 | + |
| 25 | + const disposeProvider = vscode.debug.registerDebugConfigurationProvider( |
| 26 | + "marimo", |
| 27 | + { |
| 28 | + resolveDebugConfiguration(_folder, config) { |
| 29 | + Logger.info("Debug.Config", "Resolving debug configuration", { |
| 30 | + config, |
| 31 | + }); |
| 32 | + |
| 33 | + const notebook = vscode.window.activeNotebookEditor?.notebook; |
| 34 | + if (!notebook || notebook.notebookType !== notebookType) { |
| 35 | + Logger.warn("Debug.Config", "No active marimo notebook found"); |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + config.type = "marimo"; |
| 39 | + config.name = config.name ?? "Debug Marimo"; |
| 40 | + config.request = config.request ?? "launch"; |
| 41 | + config.notebookUri = notebook.uri.toString(); |
| 42 | + |
| 43 | + Logger.info("Debug.Config", "Configuration resolved", { |
| 44 | + notebookUri: config.notebookUri, |
| 45 | + type: config.type, |
| 46 | + request: config.request, |
| 47 | + }); |
| 48 | + return config; |
| 49 | + }, |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + options.signal.addEventListener("abort", () => { |
| 54 | + Logger.info("Debug.Cleanup", "Disposing debug adapter"); |
| 55 | + disposeFactory.dispose(); |
| 56 | + disposeProvider.dispose(); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function createDebugAdapterDescriptor( |
| 61 | + client: lsp.BaseLanguageClient, |
| 62 | + session: vscode.DebugSession, |
| 63 | +): vscode.DebugAdapterDescriptor { |
| 64 | + Logger.info("Debug.Factory", "Creating debug adapter", { |
| 65 | + sessionId: session.id, |
| 66 | + name: session.name, |
| 67 | + type: session.type, |
| 68 | + configuration: session.configuration, |
| 69 | + }); |
| 70 | + |
| 71 | + const sendMessage = new vscode.EventEmitter<vscode.DebugProtocolMessage>(); |
| 72 | + const disposer = client.onNotification( |
| 73 | + "marimo/dap", |
| 74 | + ({ sessionId, message }) => { |
| 75 | + Logger.debug("Debug.Receive", "Received DAP response from LSP", { |
| 76 | + sessionId, |
| 77 | + message, |
| 78 | + }); |
| 79 | + if (sessionId === session.id) { |
| 80 | + sendMessage.fire(message); |
| 81 | + } |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + return new vscode.DebugAdapterInlineImplementation({ |
| 86 | + onDidSendMessage: sendMessage.event, |
| 87 | + handleMessage(message) { |
| 88 | + Logger.debug("Debug.Send", "Sending DAP message to LSP", { |
| 89 | + sessionId: session.id, |
| 90 | + message, |
| 91 | + }); |
| 92 | + executeCommand(client, { |
| 93 | + command: "marimo.dap", |
| 94 | + params: { |
| 95 | + sessionId: session.id, |
| 96 | + notebookUri: session.configuration.notebookUri, |
| 97 | + message, |
| 98 | + }, |
| 99 | + }); |
| 100 | + }, |
| 101 | + dispose() { |
| 102 | + disposer.dispose(); |
| 103 | + }, |
| 104 | + }); |
| 105 | +} |
0 commit comments