Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@
],
"debuggers": [
{
"type": "python",
"type": "pacifica",
"label": "Pacifica Simulator Debugger",
"program": "./out/debugAdapter.js",
"runtime": "node",
"languages": [
"python"
],
"configurationAttributes": {
"launch": {
"properties": {
Expand All @@ -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.",
Expand All @@ -224,25 +230,21 @@
},
"initialConfigurations": [
{
"type": "python",
"type": "pacifica",
"request": "launch",
"name": "Pacifica Simulator Debugger",
"program": "${file}",
"stopOnEntry": false,
"justMyCode": true
"console": "integratedTerminal"
}
],
"configurationSnippets": [
{
"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"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/debuggerCommunicationServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
84 changes: 46 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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" });
}
}
});

Expand All @@ -675,7 +683,7 @@ export async function activate(context: vscode.ExtensionContext) {
runDevice,
selectSerialPort,
vscode.debug.registerDebugConfigurationProvider(
"python",
CONSTANTS.DEBUG_CONFIGURATION_TYPE,
simulatorDebugConfiguration
),
debugSessionsStarted,
Expand Down
30 changes: 25 additions & 5 deletions src/simulatorDebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,9 +26,23 @@ export class SimulatorDebugConfigurationProvider
config: vscode.DebugConfiguration,
token?: vscode.CancellationToken
): vscode.ProviderResult<vscode.DebugConfiguration> {
// 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;

Expand All @@ -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
Expand All @@ -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
});
Expand Down