Skip to content

Commit 68b7875

Browse files
tdusnokirobertsipka
authored andcommitted
Add multiple source handling to extension (#34)
IoT.js-VSCode-DCO-1.0-Signed-off-by: Tibor Dusnoki [email protected]
1 parent ed1f1a7 commit 68b7875

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ absolute path to executable (e.g.:/path/to/iotjs/build/x86_64-linux/debug/bin/io
180180
After the engine is running you can start the debug session inside the extension host by pressing the `F5` key or click on the green triangle in the debug panel.
181181
If the client (VSCode extension) is connected then you have to see that file which is running inside the engine or if you started the engine in waiting mode you will get a prompt window where you can select that file what you want to running and then you can see where the execution is stopped. Now you can use the VSCode debug action bar to control the debug session.
182182

183+
***Important note:*** When sending multiple sources to debug server, be aware that IoT.js compiles them in the order they are received, so the files containing `require` should be sent first and the files used by them after.
184+
183185
***Note:*** If you using the development version of this extension, you have to run the following commands for the first time in the extension directory:
184186
```bash
185187
# Install the node modules

src/IotjsDebugger.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,10 @@ class IotjsDebugSession extends DebugSession {
532532
this._protocolhandler.sendClientSource(args.program.name, args.program.source)
533533
.then(() => {
534534
this.log('Source has been sent to the engine.', LOG_LEVEL.SESSION);
535-
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.LAST_SENT;
535+
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
536+
if (args.program.isLast) {
537+
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.LAST_SENT;
538+
}
536539
this.sendResponse(response);
537540
})
538541
.catch(error => {
@@ -630,8 +633,10 @@ class IotjsDebugSession extends DebugSession {
630633
this.log('onWaitForSource', LOG_LEVEL.SESSION);
631634

632635
if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.NOP) {
636+
await this.sendEvent(new Event('readSources'));
633637
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
634-
this.sendEvent(new Event('waitForSource'));
638+
} else if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.WAITING) {
639+
this.sendEvent(new Event('sendNextSource'));
635640
} else if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.LAST_SENT) {
636641
if (!this._sourceSendingOptions.contextReset) {
637642
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.NOP;

src/extension.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ let lastModified = {
3131
mtime: undefined
3232
};
3333

34+
let sources: string[];
35+
let pathArray = [];
3436
const defaultModules = [{
3537
link: 'process',
3638
mod: 'process',
@@ -129,36 +131,52 @@ const getListOfFiles = (): string[] => {
129131
return wsFiles;
130132
};
131133

132-
const getProgramName = (): Thenable<string> => {
134+
const getProgramName = (): Thenable<string[]> => {
133135
return vscode.window.showQuickPick(getListOfFiles(), {
134136
placeHolder: 'Select a file you want to debug',
135-
ignoreFocusOut: true
137+
canPickMany: true,
138+
ignoreFocusOut: true,
139+
onDidSelectItem: item => {
140+
if (pathArray.indexOf(item.toString()) === -1) {
141+
pathArray.push(item.toString());
142+
} else {
143+
pathArray.splice(pathArray.indexOf(item.toString()), 1);
144+
}
145+
}
136146
});
137147
};
138148

139-
const getProgramSource = (path: string): string => {
140-
return fs.readFileSync(path, {
141-
encoding: 'utf8',
142-
flag: 'r'
149+
const getProgramSource = (path: string[]): string[] => {
150+
return path.map((p) => {
151+
return fs.readFileSync(p, {
152+
encoding: 'utf8',
153+
flag: 'r'
154+
});
143155
});
156+
144157
};
145158

146159
const processCustomEvent = async (e: vscode.DebugSessionCustomEvent): Promise<any> => {
147160
switch (e.event) {
148-
case 'waitForSource': {
161+
case 'readSources': {
149162
if (vscode.debug.activeDebugSession) {
150-
const pathName = await getProgramName().then(path => path);
151-
const source = getProgramSource(pathName);
152-
153-
vscode.debug.activeDebugSession.customRequest('sendSource', {
154-
program: {
155-
name: pathName.split(path.delimiter).pop(),
156-
source
157-
}
158-
});
163+
await getProgramName().then(path => path);
164+
sources = getProgramSource(pathArray);
165+
e.event = 'sendNextSource';
166+
processCustomEvent(e);
159167
}
160168
return true;
161169
}
170+
case 'sendNextSource': {
171+
vscode.debug.activeDebugSession.customRequest('sendSource', {
172+
program: {
173+
name: pathArray.pop(),
174+
source: sources.pop(),
175+
isLast: sources.length === 0
176+
}
177+
});
178+
return true;
179+
}
162180
default:
163181
return undefined;
164182
}

0 commit comments

Comments
 (0)