From 2ae9930c5739fb6f5b3e8a90d26e1d8828d91797 Mon Sep 17 00:00:00 2001 From: Basant-khalil Date: Wed, 26 Jul 2023 11:22:16 -0400 Subject: [PATCH] can print as well as invoke the adpater, need to figure outpassing file as an argument --- cider-dap/calyxDebug/extension.js | 131 ++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 43 deletions(-) diff --git a/cider-dap/calyxDebug/extension.js b/cider-dap/calyxDebug/extension.js index afdb96246b..f1b1aecb1b 100644 --- a/cider-dap/calyxDebug/extension.js +++ b/cider-dap/calyxDebug/extension.js @@ -1,64 +1,109 @@ const vscode = require('vscode'); const cp = require('child_process'); -let outputChannel; +// Create an output channel +let outputChannel = vscode.window.createOutputChannel("cider dap"); -function startDebugging() { - // After attaching and starting debugging, call the panel printing function - logToPanel('Debugger attached and debugging started.'); - logToPanel('Some additional information...'); -} - -function logToPanel(message) { - // Create an output channel - if (!outputChannel) { - outputChannel = vscode.window.createOutputChannel('Cider DAP'); +// CiderDebugAdapter class for managing debug adapter process +class CiderDebugAdapter { + constructor(adapterPath, cwd, outputChannel) { + this.adapterPath = adapterPath; + this.cwd = cwd; + this.outputChannel = outputChannel; + this.adapterProcess = null; } - // Log the message to the output channel - outputChannel.appendLine(message); + // Start the debug adapter process + start() { + logToPanel('Debugger starting...'); - // Show the message in the panel as well - vscode.window.showInformationMessage(message); -} + // Spawn a new child process for the debug adapter + this.adapterProcess = cp.spawn(this.adapterPath, { + cwd: this.cwd + }); -function activate(context) { - // Create the outputChannel only once when the extension activates - outputChannel = vscode.window.createOutputChannel('Cider DAP'); + // Attach event listener to capture standard output of the adapter process and log it to the output channel + this.adapterProcess.stdout.on('data', (data) => { + logToPanel(data.toString()); + }); - // Register a command to start debugging - const disposableStartDebugging = vscode.commands.registerCommand('extension.cider-dap.startDebugging', startDebugging); - context.subscriptions.push(disposableStartDebugging); + // Attach event listener to capture standard error of the adapter process and log it to the output channel + this.adapterProcess.stderr.on('data', (data) => { + logToPanel(data.toString()); + }); + logToPanel('Debugger started!'); + } - // Register a command for testing purposes - const disposableTest = vscode.commands.registerCommand('extension.cider-dap.test', function () { - // The code you want to run when the command is executed + // Stop the debug adapter process + stop() { + if (this.adapterProcess) { + // Terminate the adapter process and set it to null + this.adapterProcess.kill(); + this.adapterProcess = null; + logToPanel('Debugger stopped.'); + } else { + logToPanel('No running debug adapter to stop.'); + } + } +} - // Log to the output channel - logToPanel('Executing extension.cider-dap.test'); +// Start debugging +function startDebugging() { + if (!debugAdapter) { + // Set the path to the debug adapter and current working directory + const adapterPath = '/home/basantkhalil/calyx2/target/debug/cider-dap'; + const cwd = vscode.workspace.rootPath; - // Listen for stdout data - const proc = cp.spawn('your_command_here', ['arg1', 'arg2']); // Replace 'your_command_here' with the actual command to be executed - proc.stdout.on('data', (data) => { - logToPanel(`stdout: ${data}`); - }); + // Create a new instance of the CiderDebugAdapter + debugAdapter = new CiderDebugAdapter(adapterPath, cwd, outputChannel); + } - // Listen for stderr data - proc.stderr.on('data', (data) => { - logToPanel(`stderr: ${data}`); - }); + // Start the debug adapter + debugAdapter.start(); +} - // Listen for close event - proc.on('close', (code) => { - logToPanel(`child process exited with code ${code}`); - }); - }); +// Stop debugging +function stopDebugging() { + if (debugAdapter) { + // Stop the running debug adapter process + debugAdapter.stop(); + } else { + logToPanel('No running debug adapter to stop.'); + } +} - context.subscriptions.push(disposableTest); +// Variable to hold the debug adapter instance +let debugAdapter = null; + +function logToPanel(message) { + console.log("inside logPanel"); + outputChannel.appendLine(message); } -function deactivate() { } +// Activate the extension +function activate(context) { + logToPanel('Hello, your extension is now activated!'); + + // Register the 'extension.startDebugging' command + let disposableStart = vscode.commands.registerCommand('cider.startDebugging', startDebugging); + console.log("after startDebugging"); + context.subscriptions.push(disposableStart); + + // Register the 'extension.stopDebugging' command + let disposableStop = vscode.commands.registerCommand('cider.stopDebugging', stopDebugging); + context.subscriptions.push(disposableStop); + // Register the debug adapter descriptor factory + vscode.debug.registerDebugAdapterDescriptorFactory('cider-dap', { + createDebugAdapterDescriptor: (_session) => { + return new vscode.DebugAdapterExecutable('./cider-dap'); + } + }); +} +function deactivate() { + logToPanel("deactivate"); +} +// Export the activate function to be used as the entry point for the extension module.exports = { activate, deactivate