diff --git a/package.json b/package.json index 753239860..7f9165427 100644 --- a/package.json +++ b/package.json @@ -180,10 +180,11 @@ ], "debuggers": [ { - "type": "python", + "type": "pacifica", "label": "Pacifica Simulator Debugger", - "program": "./out/debugAdapter.js", - "runtime": "node", + "languages": [ + "python" + ], "configurationAttributes": { "launch": { "properties": { @@ -195,12 +196,17 @@ "stopOnEntry": { "type": "boolean", "description": "Automatically stop after launch.", - "default": false - }, - "justMyCode": { - "type": "boolean", "default": true }, + "console": { + "enum": [ + "internalConsole", + "integratedTerminal", + "externalTerminal" + ], + "description": "Where to launch the debug target: internal console, integrated terminal, or external terminal.", + "default": "integratedTerminal" + }, "args": { "type": "array", "description": "Command line arguments passed to the program.", @@ -224,12 +230,10 @@ }, "initialConfigurations": [ { - "type": "python", + "type": "pacifica", "request": "launch", "name": "Pacifica Simulator Debugger", - "program": "${file}", - "stopOnEntry": false, - "justMyCode": true + "console": "integratedTerminal" } ], "configurationSnippets": [ @@ -237,12 +241,10 @@ "label": "Pacifica Simulator Debugger : Launch", "description": "Pacifica Simulator Debugger - A configuration for debugging a python code file for the Pacifica simulator.", "body": { - "type": "python", + "type": "pacifica", "request": "launch", "name": "Pacifica Simulator Debugger", - "program": "${file}", - "stopOnEntry": false, - "justMyCode": true + "console": "integratedTerminal" } } ] diff --git a/src/constants.ts b/src/constants.ts index d74e64ba4..8aac4b7e9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -17,7 +17,7 @@ const localize: nls.LocalizeFunc = nls.config({ })(); export const CONSTANTS = { - DEBUG_CONFIGURATION_NAME: "Pacifica Simulator Debugger", + DEBUG_CONFIGURATION_TYPE: "pacifica", DEPENDENCY_CHECKER: { PYTHON: "python", PYTHON3: "python3", diff --git a/src/debuggerCommunicationServer.ts b/src/debuggerCommunicationServer.ts index 7cd2d45bc..23d429475 100644 --- a/src/debuggerCommunicationServer.ts +++ b/src/debuggerCommunicationServer.ts @@ -27,8 +27,9 @@ export class DebuggerCommunicationServer { } public closeConnection(): void { - this.serverHttp.close(); this.serverIo.close(); + this.serverHttp.close(); + console.info("Closing the server"); } public setWebview(webviewPanel: WebviewPanel | undefined) { diff --git a/src/extension.ts b/src/extension.ts index 1425348ab..221874268 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -63,11 +63,7 @@ export async function activate(context: vscode.ExtensionContext) { if (outChannel === undefined) { outChannel = vscode.window.createOutputChannel(CONSTANTS.NAME); - utils.logToOutputChannel( - outChannel, - CONSTANTS.INFO.WELCOME_OUTPUT_TAB, - true - ); + utils.logToOutputChannel(outChannel, CONSTANTS.INFO.WELCOME_OUTPUT_TAB); } vscode.workspace.onDidSaveTextDocument( @@ -297,7 +293,7 @@ export async function activate(context: vscode.ExtensionContext) { console.info(CONSTANTS.INFO.RUNNING_CODE); - utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_SIMULATOR); + utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_SIMULATOR, true); killProcessIfRunning(); @@ -450,7 +446,7 @@ export async function activate(context: vscode.ExtensionContext) { const deployCodeToDevice = async () => { console.info("Sending code to device"); - utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_DEVICE); + utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_DEVICE, true); await updateCurrentFileIfPython(vscode.window.activeTextEditor!.document); @@ -623,44 +619,56 @@ export async function activate(context: vscode.ExtensionContext) { // On Debug Session Start: Init comunication const debugSessionsStarted = vscode.debug.onDidStartDebugSession(() => { - // Reinitialize process - killProcessIfRunning(); - console.log("Debug Started"); - inDebugMode = true; + if (simulatorDebugConfiguration.pacificaDebug) { + // Reinitialize process + killProcessIfRunning(); + console.log("Debug Started"); + inDebugMode = true; + + try { + // Shut down existing server on debug restart + if (debuggerCommunicationHandler) { + debuggerCommunicationHandler.closeConnection(); + debuggerCommunicationHandler = undefined; + } - try { - debuggerCommunicationHandler = new DebuggerCommunicationServer( - currentPanel, - utils.getServerPortConfig() - ); - openWebview(); - if (currentPanel) { - debuggerCommunicationHandler.setWebview(currentPanel); - currentPanel.webview.postMessage({ command: "activate-play" }); - } - } catch (err) { - if (err.message === SERVER_INFO.ERROR_CODE_INIT_SERVER) { - console.error( - `Error trying to init the server on port ${utils.getServerPortConfig()}` - ); - vscode.window.showErrorMessage( - CONSTANTS.ERROR.DEBUGGER_SERVER_INIT_FAILED( - utils.getServerPortConfig() - ) + debuggerCommunicationHandler = new DebuggerCommunicationServer( + currentPanel, + utils.getServerPortConfig() ); + openWebview(); + if (currentPanel) { + debuggerCommunicationHandler.setWebview(currentPanel); + currentPanel.webview.postMessage({ command: "activate-play" }); + } + } catch (err) { + if (err.message === SERVER_INFO.ERROR_CODE_INIT_SERVER) { + console.error( + `Error trying to init the server on port ${utils.getServerPortConfig()}` + ); + vscode.window.showErrorMessage( + CONSTANTS.ERROR.DEBUGGER_SERVER_INIT_FAILED( + utils.getServerPortConfig() + ) + ); + } } } }); // On Debug Session Stop: Stop communiation const debugSessionStopped = vscode.debug.onDidTerminateDebugSession(() => { - console.log("Debug Stopped"); - inDebugMode = false; - if (debuggerCommunicationHandler) { - debuggerCommunicationHandler.closeConnection(); - } - if (currentPanel) { - currentPanel.webview.postMessage({ command: "reset-state" }); + if (simulatorDebugConfiguration.pacificaDebug) { + console.log("Debug Stopped"); + inDebugMode = false; + simulatorDebugConfiguration.pacificaDebug = false; + if (debuggerCommunicationHandler) { + debuggerCommunicationHandler.closeConnection(); + debuggerCommunicationHandler = undefined; + } + if (currentPanel) { + currentPanel.webview.postMessage({ command: "reset-state" }); + } } }); @@ -675,7 +683,7 @@ export async function activate(context: vscode.ExtensionContext) { runDevice, selectSerialPort, vscode.debug.registerDebugConfigurationProvider( - "python", + CONSTANTS.DEBUG_CONFIGURATION_TYPE, simulatorDebugConfiguration ), debugSessionsStarted, diff --git a/src/simulatorDebugConfigurationProvider.ts b/src/simulatorDebugConfigurationProvider.ts index 15d87c117..8e167539a 100644 --- a/src/simulatorDebugConfigurationProvider.ts +++ b/src/simulatorDebugConfigurationProvider.ts @@ -12,7 +12,11 @@ let shouldShowInvalidFileNamePopup: boolean = true; export class SimulatorDebugConfigurationProvider implements vscode.DebugConfigurationProvider { - constructor(private pathToScript: string) {} + public pacificaDebug: boolean; + + constructor(private pathToScript: string) { + this.pacificaDebug = false; + } /** * Modify the debug configuration just before a debug session is being launched. @@ -22,9 +26,23 @@ export class SimulatorDebugConfigurationProvider config: vscode.DebugConfiguration, token?: vscode.CancellationToken ): vscode.ProviderResult { - // Check config name - if (config.name === CONSTANTS.DEBUG_CONFIGURATION_NAME) { - const activeTextEditor = vscode.window.activeTextEditor; + const activeTextEditor = vscode.window.activeTextEditor; + + // Create a configuration if no launch.json exists or if it's empty + if (!config.type && !config.request && !config.name) { + if ( + activeTextEditor && + activeTextEditor.document.languageId === "python" + ) { + config.type = "pacifica"; + config.request = "launch"; + config.name = "Pacifica Simulator Debugger"; + config.console = "integratedTerminal"; + } + } + // Check config type + if (config.type === CONSTANTS.DEBUG_CONFIGURATION_TYPE) { + this.pacificaDebug = true; if (activeTextEditor) { const currentFilePath = activeTextEditor.document.fileName; @@ -50,6 +68,8 @@ export class SimulatorDebugConfigurationProvider } }); } + // Set the new configuration type so the python debugger can take over + config.type = "python"; // Set process_user_code path as program config.program = this.pathToScript; // Set user's code path and server's port as args @@ -68,7 +88,7 @@ export class SimulatorDebugConfigurationProvider // Abort / show error message if can't find process_user_code.py if (!config.program) { return vscode.window - .showInformationMessage(CONSTANTS.ERROR.NO_PROGRAM_FOUND_DEBUG) + .showErrorMessage(CONSTANTS.ERROR.NO_PROGRAM_FOUND_DEBUG) .then(() => { return undefined; // Abort launch });