forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadedScripts.ts
More file actions
359 lines (287 loc) · 9.13 KB
/
Copy pathloadedScripts.ts
File metadata and controls
359 lines (287 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as nls from 'vscode-nls';
import * as vscode from 'vscode';
import { TreeDataProvider, TreeItem, EventEmitter, Event, ProviderResult } from 'vscode';
import { basename } from 'path';
const localize = nls.loadMessageBundle();
//---- loaded script explorer
const URL_REGEXP = /^(https?:\/\/[^/]+)(\/.*)$/;
class Source {
name: string;
path: string;
sourceReference: number;
constructor(path: string) {
this.name = basename(path);
this.path = path;
}
}
class LoadedScriptItem implements vscode.QuickPickItem {
label: string;
description: string;
source?: Source;
constructor(source: Source) {
this.label = basename(source.path);
this.description = source.path;
this.source = source;
}
}
export class LoadedScriptsProvider implements TreeDataProvider<BaseTreeItem> {
private _root: RootTreeItem;
private _onDidChangeTreeData: EventEmitter<BaseTreeItem> = new EventEmitter<BaseTreeItem>();
readonly onDidChangeTreeData: Event<BaseTreeItem> = this._onDidChangeTreeData.event;
constructor(context: vscode.ExtensionContext) {
this._root = new RootTreeItem();
context.subscriptions.push(vscode.debug.onDidStartDebugSession(async (session: vscode.DebugSession): Promise<void> => {
const t = session ? session.type : undefined;
if (await this.isSupportedDebugType(t, session)) {
vscode.commands.executeCommand('setContext', 'showLoadedScriptsExplorer', true);
this._root.add(session);
this._onDidChangeTreeData.fire(undefined);
}
}));
let timeout: NodeJS.Timer;
context.subscriptions.push(vscode.debug.onDidReceiveDebugSessionCustomEvent(async (event): Promise<void> => {
const t = (event.event === 'loadedSource' && event.session) ? event.session.type : undefined;
if (await this.isSupportedDebugType(t, event.session)) {
const sessionRoot = this._root.add(event.session);
sessionRoot.addPath(<Source> event.body.source);
clearTimeout(timeout);
timeout = setTimeout(() => {
this._onDidChangeTreeData.fire(undefined);
}, 300);
}
}));
context.subscriptions.push(vscode.debug.onDidTerminateDebugSession(session => {
this._root.remove(session.id);
this._onDidChangeTreeData.fire(undefined);
}));
}
getChildren(node?: BaseTreeItem): ProviderResult<BaseTreeItem[]> {
return (node || this._root).getChildren();
}
getTreeItem(node: BaseTreeItem): TreeItem {
return node;
}
private async isSupportedDebugType(debugType: string | undefined, session: vscode.DebugSession): Promise<boolean> {
if (debugType === 'vslsShare') {
try {
debugType = session ? await session.customRequest('debugType', {}) : undefined;
}
catch (e) {
}
}
return debugType === 'node' || debugType === 'node2' || debugType === 'extensionHost' || debugType === 'chrome';
}
}
class BaseTreeItem extends TreeItem {
private _children: { [key: string]: BaseTreeItem; };
constructor(label: string, state = vscode.TreeItemCollapsibleState.Collapsed) {
super(label, state);
this._children = {};
}
setSource(session: vscode.DebugSession, source: Source): void {
this.command = {
command: 'extension.node-debug.openScript',
arguments: [session, source],
title: ''
};
}
getChildren(): ProviderResult<BaseTreeItem[]> {
this.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
const array = Object.keys(this._children).map(key => this._children[key]);
return array.sort((a, b) => this.compare(a, b));
}
createIfNeeded<T extends BaseTreeItem>(key: string, factory: (label: string) => T): T {
let child = <T>this._children[key];
if (!child) {
child = factory(key);
this._children[key] = child;
}
return child;
}
remove(key: string): void {
delete this._children[key];
}
protected compare(a: BaseTreeItem, b: BaseTreeItem): number {
if (a.label && b.label) {
return a.label.localeCompare(b.label);
}
return 0;
}
}
class RootTreeItem extends BaseTreeItem {
private _showedMoreThanOne: boolean;
constructor() {
super('Root', vscode.TreeItemCollapsibleState.Expanded);
this._showedMoreThanOne = false;
}
getChildren(): ProviderResult<BaseTreeItem[]> {
// skip sessions if there is only one
const children = super.getChildren();
if (Array.isArray(children)) {
const size = children.length;
if (!this._showedMoreThanOne && size === 1) {
return children[0].getChildren();
}
this._showedMoreThanOne = size > 1;
}
return children;
}
add(session: vscode.DebugSession): SessionTreeItem {
return this.createIfNeeded(session.id, () => new SessionTreeItem(session));
}
}
class SessionTreeItem extends BaseTreeItem {
private _session: vscode.DebugSession;
private _initialized: boolean;
constructor(session: vscode.DebugSession) {
super(session.name, vscode.TreeItemCollapsibleState.Expanded);
this._initialized = false;
this._session = session;
/*
const dir = dirname(__filename);
this.iconPath = {
light: join(dir, '..', '..', '..', 'images', 'debug-light.svg'),
dark: join(dir, '..', '..', '..', 'images', 'debug-dark.svg')
};
*/
}
getChildren(): ProviderResult<BaseTreeItem[]> {
if (!this._initialized) {
this._initialized = true;
return listLoadedScripts(this._session).then(paths => {
if (paths) {
paths.forEach(path => this.addPath(path));
}
return super.getChildren();
});
}
return super.getChildren();
}
protected compare(a: BaseTreeItem, b: BaseTreeItem): number {
const acat = this.category(a);
const bcat = this.category(b);
if (acat !== bcat) {
return acat - bcat;
}
return super.compare(a, b);
}
/**
* Return an ordinal number for folders
*/
private category(item: BaseTreeItem): number {
// workspace scripts come at the beginning in "folder" order
if (item instanceof FolderTreeItem) {
return item.folder.index;
}
// <...> come at the very end
if (item.label && /^<.+>$/.test(item.label)) {
return 1000;
}
// everything else in between
return 999;
}
addPath(source: Source): void {
let folder: vscode.WorkspaceFolder | undefined;
let url: string;
let p: string;
let path = source.path;
const match = URL_REGEXP.exec(path);
if (match && match.length === 3) {
url = match[1];
p = decodeURI(match[2]);
} else {
folder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(path));
p = trim(path);
}
let x: BaseTreeItem = this;
p.split(/[\/\\]/).forEach((segment, i) => {
if (segment.length === 0) { // macOS or unix path
segment = '/';
}
if (i === 0 && folder) {
x = x.createIfNeeded(folder.name, () => new FolderTreeItem(<vscode.WorkspaceFolder>folder));
} else if (i === 0 && url) {
x = x.createIfNeeded(url, () => new BaseTreeItem(url));
} else {
x = x.createIfNeeded(segment, () => new BaseTreeItem(segment));
}
});
x.collapsibleState = vscode.TreeItemCollapsibleState.None;
x.setSource(this._session, source);
}
}
class FolderTreeItem extends BaseTreeItem {
folder: vscode.WorkspaceFolder;
constructor(folder: vscode.WorkspaceFolder) {
super(folder.name, vscode.TreeItemCollapsibleState.Collapsed);
this.folder = folder;
}
}
//---- loaded script picker
export function pickLoadedScript() {
const session = vscode.debug.activeDebugSession;
return listLoadedScripts(session).then(sources => {
let options: vscode.QuickPickOptions = {
placeHolder: localize('select.script', "Select a script"),
matchOnDescription: true,
matchOnDetail: true,
ignoreFocusOut: true
};
let items: LoadedScriptItem[];
if (sources === undefined) {
items = [ { label: localize('no.loaded.scripts', "No loaded scripts available"), description: '' }];
} else {
items = sources.map(source => new LoadedScriptItem(source)).sort((a, b) => a.label.localeCompare(b.label));
}
vscode.window.showQuickPick(items, options).then(item => {
if (item && item.source) {
openScript(session, item.source);
}
});
});
}
let USERHOME: string;
function getUserHome(): string {
if (!USERHOME) {
USERHOME = require('os').homedir();
if (USERHOME && USERHOME[USERHOME.length - 1] !== '/') {
USERHOME += '/';
}
}
return USERHOME;
}
function trim(path: string) : string {
path = vscode.workspace.asRelativePath(path, true);
if (path.indexOf('/') === 0) {
path = path.replace(getUserHome(), '~/');
}
return path;
}
function listLoadedScripts(session: vscode.DebugSession | undefined): Thenable<Source[] | undefined> {
if (session) {
return session.customRequest('loadedSources').then(reply => {
return <Source[]>reply.sources;
}, err => {
return undefined;
});
} else {
return Promise.resolve(undefined);
}
}
export function openScript(session: vscode.DebugSession | undefined, source: Source) {
let debug = `debug:${encodeURIComponent(source.path)}`;
let sep = '?';
if (session) {
debug += `${sep}session=${encodeURIComponent(session.id)}`;
sep = '&';
}
if (source.sourceReference) {
debug += `${sep}ref=${source.sourceReference}`;
}
let uri = vscode.Uri.parse(debug);
vscode.workspace.openTextDocument(uri).then(doc => vscode.window.showTextDocument(doc));
}