-
Notifications
You must be signed in to change notification settings - Fork 0
Add Debug Adapter Protocol (DAP) support #5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import * as vscode from "vscode"; | ||
| import type * as lsp from "vscode-languageclient"; | ||
| import type { BaseLanguageClient } from "vscode-languageclient"; | ||
| import { executeCommand } from "./commands.ts"; | ||
| import { Logger } from "./logging.ts"; | ||
| import { notebookType } from "./types.ts"; | ||
|
|
||
| export function debugAdapter( | ||
| client: BaseLanguageClient, | ||
| options: { signal: AbortSignal }, | ||
| ) { | ||
| Logger.info("Debug.Init", "Registering debug adapter"); | ||
|
|
||
| const disposeFactory = vscode.debug.registerDebugAdapterDescriptorFactory( | ||
| "marimo", | ||
| { | ||
| createDebugAdapterDescriptor: createDebugAdapterDescriptor.bind( | ||
| null, | ||
| client, | ||
| ), | ||
| }, | ||
| ); | ||
|
|
||
| const disposeProvider = vscode.debug.registerDebugConfigurationProvider( | ||
| "marimo", | ||
| { | ||
| resolveDebugConfiguration(_folder, config) { | ||
| Logger.info("Debug.Config", "Resolving debug configuration", { | ||
| config, | ||
| }); | ||
|
|
||
| const notebook = vscode.window.activeNotebookEditor?.notebook; | ||
| if (!notebook || notebook.notebookType !== notebookType) { | ||
| Logger.warn("Debug.Config", "No active marimo notebook found"); | ||
| return undefined; | ||
| } | ||
| config.type = "marimo"; | ||
| config.name = config.name ?? "Debug Marimo"; | ||
| config.request = config.request ?? "launch"; | ||
| config.notebookUri = notebook.uri.toString(); | ||
|
|
||
| Logger.info("Debug.Config", "Configuration resolved", { | ||
| notebookUri: config.notebookUri, | ||
| type: config.type, | ||
| request: config.request, | ||
| }); | ||
| return config; | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| options.signal.addEventListener("abort", () => { | ||
| Logger.info("Debug.Cleanup", "Disposing debug adapter"); | ||
| disposeFactory.dispose(); | ||
| disposeProvider.dispose(); | ||
| }); | ||
| } | ||
|
|
||
| function createDebugAdapterDescriptor( | ||
| client: lsp.BaseLanguageClient, | ||
| session: vscode.DebugSession, | ||
| ): vscode.DebugAdapterDescriptor { | ||
| Logger.info("Debug.Factory", "Creating debug adapter", { | ||
| sessionId: session.id, | ||
| name: session.name, | ||
| type: session.type, | ||
| configuration: session.configuration, | ||
| }); | ||
|
|
||
| const sendMessage = new vscode.EventEmitter<vscode.DebugProtocolMessage>(); | ||
| const disposer = client.onNotification( | ||
| "marimo/dap", | ||
| ({ sessionId, message }) => { | ||
| Logger.debug("Debug.Receive", "Received DAP response from LSP", { | ||
| sessionId, | ||
| message, | ||
| }); | ||
| if (sessionId === session.id) { | ||
| sendMessage.fire(message); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| return new vscode.DebugAdapterInlineImplementation({ | ||
| onDidSendMessage: sendMessage.event, | ||
| handleMessage(message) { | ||
| Logger.debug("Debug.Send", "Sending DAP message to LSP", { | ||
| sessionId: session.id, | ||
| message, | ||
| }); | ||
| executeCommand(client, { | ||
| command: "marimo.dap", | ||
| params: { | ||
| sessionId: session.id, | ||
| notebookUri: session.configuration.notebookUri, | ||
| message, | ||
| }, | ||
|
Comment on lines
+91
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Custom LSP "command" that wraps a DAP request with additional information (for a request). I assume that the handler will manage its own debug sessions. |
||
| }); | ||
| }, | ||
| dispose() { | ||
| disposer.dispose(); | ||
| }, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """Handler for DAP messages.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import typing | ||
|
|
||
| import attrs | ||
| import cattrs | ||
|
|
||
| from marimo_lsp.loggers import get_logger | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from pygls.lsp.server import LanguageServer | ||
|
|
||
| from marimo_lsp.session_manager import LspSessionManager | ||
|
|
||
| logger = get_logger() | ||
| converter = cattrs.Converter() | ||
|
|
||
|
|
||
| @attrs.define | ||
| class DapRequestMessage: | ||
| """ | ||
| A generic DAP (Debug Adapter Protocol) request message. | ||
|
|
||
| DAP requests follow a standard structure where the command field | ||
| determines the action, and arguments contain command-specific parameters | ||
| that require further parsing based on the command type. | ||
| """ | ||
|
|
||
| seq: int | ||
| """Sequence number of the message.""" | ||
|
|
||
| type: typing.Literal["request"] | ||
| """Message type - always 'request' for DAP requests.""" | ||
|
|
||
| command: str | ||
| """The command to execute (e.g., 'initialize', 'launch', 'setBreakpoints').""" | ||
|
|
||
| arguments: dict | None | ||
| """Command-specific arguments. Should be parsed further in ./debug_adapter.py""" | ||
|
|
||
|
|
||
| def handle_debug_adapter_request( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. barebones request/response. I assume this is where your large conditional thing would go @dmadisetti |
||
| ls: LanguageServer, | ||
| manager: LspSessionManager, | ||
| *, | ||
| notebook_uri: str, | ||
| session_id: str, | ||
| message: dict, | ||
| ) -> None: | ||
| """Handle DAP requests.""" | ||
| request = converter.structure(message, DapRequestMessage) | ||
| logger.debug(f"Debug.Send {session_id=}, {request=}") | ||
|
|
||
| session = manager.get_session(notebook_uri) | ||
| assert session, f"No session in workspace for {notebook_uri}" | ||
|
|
||
| ls.protocol.notify( | ||
| "marimo/dap", | ||
| { | ||
| "sessionId": session_id, | ||
| "message": { | ||
| "type": "response", | ||
| "request_seq": request.seq, | ||
| "success": True, | ||
| "command": request.command, | ||
| "request": {}, | ||
| }, | ||
| }, | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Responses from the request handler.