-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
sources.ts
329 lines (296 loc) · 8.86 KB
/
sources.ts
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { JupyterFrontEnd } from '@jupyterlab/application';
import { DOMUtils, MainAreaWidget, WidgetTracker } from '@jupyterlab/apputils';
import { CodeEditorWrapper, IEditorServices } from '@jupyterlab/codeeditor';
import { IConsoleTracker } from '@jupyterlab/console';
import { IEditorTracker } from '@jupyterlab/fileeditor';
import { INotebookTracker } from '@jupyterlab/notebook';
import { textEditorIcon } from '@jupyterlab/ui-components';
import { IDebugger } from './tokens';
/**
* The source and editor manager for a debugger instance.
*/
export class DebuggerSources implements IDebugger.ISources {
/**
* Create a new DebuggerSources instance.
*
* @param options The instantiation options for a DebuggerSources instance.
*/
constructor(options: DebuggerSources.IOptions) {
this._config = options.config;
this._shell = options.shell;
this._notebookTracker = options.notebookTracker ?? null;
this._consoleTracker = options.consoleTracker ?? null;
this._editorTracker = options.editorTracker ?? null;
this._readOnlyEditorTracker = new WidgetTracker<
MainAreaWidget<CodeEditorWrapper>
>({ namespace: '@jupyterlab/debugger' });
}
/**
* Returns an array of editors for a source matching the current debug
* session by iterating through all the widgets in each of the supported
* debugger types (i.e., consoles, files, notebooks).
*
* @param params - The editor search parameters.
*/
find(params: IDebugger.ISources.FindParams): IDebugger.ISources.IEditor[] {
return [
...this._findInConsoles(params),
...this._findInEditors(params),
...this._findInNotebooks(params),
...this._findInReadOnlyEditors(params)
];
}
/**
* Open a read-only editor in the main area.
*
* @param params The editor open parameters.
*/
open(params: IDebugger.ISources.OpenParams): void {
const { editorWrapper, label, caption } = params;
const widget = new MainAreaWidget<CodeEditorWrapper>({
content: editorWrapper
});
widget.id = DOMUtils.createDomID();
widget.title.label = label;
widget.title.closable = true;
widget.title.caption = caption;
widget.title.icon = textEditorIcon;
this._shell.add(widget, 'main', { type: 'Debugger Sources' });
void this._readOnlyEditorTracker.add(widget);
}
/**
* Find relevant editors matching the search params in the notebook tracker.
*
* @param params - The editor search parameters.
*/
private _findInNotebooks(
params: IDebugger.ISources.FindParams
): IDebugger.ISources.IEditor[] {
if (!this._notebookTracker) {
return [];
}
const { focus, kernel, path, source } = params;
const editors: IDebugger.ISources.IEditor[] = [];
this._notebookTracker.forEach(notebookPanel => {
const sessionContext = notebookPanel.sessionContext;
if (path !== sessionContext.path) {
return;
}
const notebook = notebookPanel.content;
if (focus) {
notebook.mode = 'command';
}
const cells = notebookPanel.content.widgets;
cells.forEach((cell, i) => {
// check the event is for the correct cell
const code = cell.model.sharedModel.getSource();
const codeId = this._getCodeId(code, kernel);
if (!codeId) {
return;
}
if (source !== codeId) {
return;
}
if (focus) {
notebook.activeCellIndex = i;
if (notebook.activeCell) {
notebook
.scrollToItem(notebook.activeCellIndex, 'smart')
.catch(reason => {
// no-op
});
}
this._shell.activateById(notebookPanel.id);
}
editors.push(
Object.freeze({
get: () => cell.editor,
reveal: () => notebook.scrollToItem(i, 'smart'),
src: cell.model.sharedModel
})
);
});
});
return editors;
}
/**
* Find relevant editors matching the search params in the console tracker.
*
* @param params - The editor search parameters.
*/
private _findInConsoles(
params: IDebugger.ISources.FindParams
): IDebugger.ISources.IEditor[] {
if (!this._consoleTracker) {
return [];
}
const { focus, kernel, path, source } = params;
const editors: IDebugger.ISources.IEditor[] = [];
this._consoleTracker.forEach(consoleWidget => {
const sessionContext = consoleWidget.sessionContext;
if (path !== sessionContext.path) {
return;
}
const cells = consoleWidget.console.cells;
for (const cell of cells) {
const code = cell.model.sharedModel.getSource();
const codeId = this._getCodeId(code, kernel);
if (!codeId) {
break;
}
if (source !== codeId) {
break;
}
editors.push(
Object.freeze({
get: () => cell.editor,
reveal: () =>
Promise.resolve(this._shell.activateById(consoleWidget.id)),
src: cell.model.sharedModel
})
);
if (focus) {
this._shell.activateById(consoleWidget.id);
}
}
});
return editors;
}
/**
* Find relevant editors matching the search params in the editor tracker.
*
* @param params - The editor search parameters.
*/
private _findInEditors(
params: IDebugger.ISources.FindParams
): IDebugger.ISources.IEditor[] {
if (!this._editorTracker) {
return [];
}
const { focus, kernel, path, source } = params;
const editors: IDebugger.ISources.IEditor[] = [];
this._editorTracker.forEach(doc => {
const fileEditor = doc.content;
if (path !== fileEditor.context.path) {
return;
}
const editor = fileEditor.editor;
if (!editor) {
return;
}
const code = editor.model.sharedModel.getSource();
const codeId = this._getCodeId(code, kernel);
if (!codeId) {
return;
}
if (source !== codeId) {
return;
}
editors.push(
Object.freeze({
get: () => editor,
reveal: () => Promise.resolve(this._shell.activateById(doc.id)),
src: fileEditor.model.sharedModel
})
);
if (focus) {
this._shell.activateById(doc.id);
}
});
return editors;
}
/**
* Find relevant editors matching the search params in the read-only tracker.
*
* @param params - The editor search parameters.
*/
private _findInReadOnlyEditors(
params: IDebugger.ISources.FindParams
): IDebugger.ISources.IEditor[] {
const { focus, kernel, source } = params;
const editors: IDebugger.ISources.IEditor[] = [];
this._readOnlyEditorTracker.forEach(widget => {
const editor = widget.content?.editor;
if (!editor) {
return;
}
const code = editor.model.sharedModel.getSource();
const codeId = this._getCodeId(code, kernel);
if (!codeId) {
return;
}
if (widget.title.caption !== source && source !== codeId) {
return;
}
editors.push(
Object.freeze({
get: () => editor,
reveal: () => Promise.resolve(this._shell.activateById(widget.id)),
src: editor.model.sharedModel
})
);
if (focus) {
this._shell.activateById(widget.id);
}
});
return editors;
}
/**
* Get the code id for a given source and kernel,
* and handle the case of a kernel without parameters.
*
* @param code The source code.
* @param kernel The name of the kernel.
*/
private _getCodeId(code: string, kernel: string): string {
try {
return this._config.getCodeId(code, kernel);
} catch {
return '';
}
}
private _shell: JupyterFrontEnd.IShell;
private _readOnlyEditorTracker: WidgetTracker<
MainAreaWidget<CodeEditorWrapper>
>;
private _config: IDebugger.IConfig;
private _notebookTracker: INotebookTracker | null;
private _consoleTracker: IConsoleTracker | null;
private _editorTracker: IEditorTracker | null;
}
/**
* A namespace for `DebuggerSources` statics.
*/
export namespace DebuggerSources {
/**
* The options used to initialize a DebuggerSources object.
*/
export interface IOptions {
/**
* The instance of configuration with hash method.
*/
config: IDebugger.IConfig;
/**
* The editor services.
*/
editorServices: IEditorServices;
/**
* The application shell.
*/
shell: JupyterFrontEnd.IShell;
/**
* An optional console tracker.
*/
consoleTracker?: IConsoleTracker | null;
/**
* An optional file editor tracker.
*/
editorTracker?: IEditorTracker | null;
/**
* An optional notebook tracker.
*/
notebookTracker?: INotebookTracker | null;
}
}