@jupyterlab/tooltip-extension 4.0.0-alpha.19 → 4.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +18 -17
- package/src/index.ts +340 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlab/tooltip-extension",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.20",
|
|
4
4
|
"description": "JupyterLab - Tooltip Extension",
|
|
5
5
|
"homepage": "https://github.com/jupyterlab/jupyterlab",
|
|
6
6
|
"bugs": {
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"lib/*.js",
|
|
29
29
|
"schema/*.json",
|
|
30
30
|
"style/**/*.css",
|
|
31
|
-
"style/index.js"
|
|
31
|
+
"style/index.js",
|
|
32
|
+
"src/**/*.{ts,tsx}"
|
|
32
33
|
],
|
|
33
34
|
"scripts": {
|
|
34
35
|
"build": "tsc -b",
|
|
@@ -37,24 +38,24 @@
|
|
|
37
38
|
"watch": "tsc -b --watch"
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"@jupyterlab/application": "^4.0.0-alpha.
|
|
41
|
-
"@jupyterlab/codeeditor": "^4.0.0-alpha.
|
|
42
|
-
"@jupyterlab/console": "^4.0.0-alpha.
|
|
43
|
-
"@jupyterlab/coreutils": "^6.0.0-alpha.
|
|
44
|
-
"@jupyterlab/fileeditor": "^4.0.0-alpha.
|
|
45
|
-
"@jupyterlab/notebook": "^4.0.0-alpha.
|
|
46
|
-
"@jupyterlab/rendermime": "^4.0.0-alpha.
|
|
47
|
-
"@jupyterlab/services": "^7.0.0-alpha.
|
|
48
|
-
"@jupyterlab/tooltip": "^4.0.0-alpha.
|
|
49
|
-
"@jupyterlab/translation": "^4.0.0-alpha.
|
|
50
|
-
"@lumino/algorithm": "^2.0.0-
|
|
51
|
-
"@lumino/coreutils": "^2.0.0-
|
|
52
|
-
"@lumino/widgets": "^2.0.0-
|
|
41
|
+
"@jupyterlab/application": "^4.0.0-alpha.20",
|
|
42
|
+
"@jupyterlab/codeeditor": "^4.0.0-alpha.20",
|
|
43
|
+
"@jupyterlab/console": "^4.0.0-alpha.20",
|
|
44
|
+
"@jupyterlab/coreutils": "^6.0.0-alpha.20",
|
|
45
|
+
"@jupyterlab/fileeditor": "^4.0.0-alpha.20",
|
|
46
|
+
"@jupyterlab/notebook": "^4.0.0-alpha.20",
|
|
47
|
+
"@jupyterlab/rendermime": "^4.0.0-alpha.20",
|
|
48
|
+
"@jupyterlab/services": "^7.0.0-alpha.20",
|
|
49
|
+
"@jupyterlab/tooltip": "^4.0.0-alpha.20",
|
|
50
|
+
"@jupyterlab/translation": "^4.0.0-alpha.20",
|
|
51
|
+
"@lumino/algorithm": "^2.0.0-rc.0",
|
|
52
|
+
"@lumino/coreutils": "^2.0.0-rc.0",
|
|
53
|
+
"@lumino/widgets": "^2.0.0-rc.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
56
|
"rimraf": "~3.0.0",
|
|
56
|
-
"typedoc": "~0.
|
|
57
|
-
"typescript": "~
|
|
57
|
+
"typedoc": "~0.23.25",
|
|
58
|
+
"typescript": "~5.0.0-beta"
|
|
58
59
|
},
|
|
59
60
|
"publishConfig": {
|
|
60
61
|
"access": "public"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
/**
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
* @module tooltip-extension
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
JupyterFrontEnd,
|
|
10
|
+
JupyterFrontEndPlugin
|
|
11
|
+
} from '@jupyterlab/application';
|
|
12
|
+
import { CodeEditor } from '@jupyterlab/codeeditor';
|
|
13
|
+
import { IConsoleTracker } from '@jupyterlab/console';
|
|
14
|
+
import { Text } from '@jupyterlab/coreutils';
|
|
15
|
+
import { IEditorTracker } from '@jupyterlab/fileeditor';
|
|
16
|
+
import { INotebookTracker } from '@jupyterlab/notebook';
|
|
17
|
+
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
|
|
18
|
+
import { Kernel, KernelMessage, Session } from '@jupyterlab/services';
|
|
19
|
+
import { ITooltipManager, Tooltip } from '@jupyterlab/tooltip';
|
|
20
|
+
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
|
|
21
|
+
import { find } from '@lumino/algorithm';
|
|
22
|
+
import { JSONObject } from '@lumino/coreutils';
|
|
23
|
+
import { Widget } from '@lumino/widgets';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The command IDs used by the tooltip plugin.
|
|
27
|
+
*/
|
|
28
|
+
namespace CommandIDs {
|
|
29
|
+
export const dismiss = 'tooltip:dismiss';
|
|
30
|
+
|
|
31
|
+
export const launchConsole = 'tooltip:launch-console';
|
|
32
|
+
|
|
33
|
+
export const launchNotebook = 'tooltip:launch-notebook';
|
|
34
|
+
|
|
35
|
+
export const launchFile = 'tooltip:launch-file';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The main tooltip manager plugin.
|
|
40
|
+
*/
|
|
41
|
+
const manager: JupyterFrontEndPlugin<ITooltipManager> = {
|
|
42
|
+
id: '@jupyterlab/tooltip-extension:manager',
|
|
43
|
+
autoStart: true,
|
|
44
|
+
optional: [ITranslator],
|
|
45
|
+
provides: ITooltipManager,
|
|
46
|
+
activate: (
|
|
47
|
+
app: JupyterFrontEnd,
|
|
48
|
+
translator: ITranslator | null
|
|
49
|
+
): ITooltipManager => {
|
|
50
|
+
const trans = (translator ?? nullTranslator).load('jupyterlab');
|
|
51
|
+
let tooltip: Tooltip | null = null;
|
|
52
|
+
|
|
53
|
+
// Add tooltip dismiss command.
|
|
54
|
+
app.commands.addCommand(CommandIDs.dismiss, {
|
|
55
|
+
label: trans.__('Dismiss the tooltip'),
|
|
56
|
+
execute: () => {
|
|
57
|
+
if (tooltip) {
|
|
58
|
+
tooltip.dispose();
|
|
59
|
+
tooltip = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
invoke(options: ITooltipManager.IOptions): Promise<void> {
|
|
66
|
+
const detail: 0 | 1 = 0;
|
|
67
|
+
const { anchor, editor, kernel, rendermime } = options;
|
|
68
|
+
|
|
69
|
+
if (tooltip) {
|
|
70
|
+
tooltip.dispose();
|
|
71
|
+
tooltip = null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Private.fetch({ detail, editor, kernel })
|
|
75
|
+
.then(bundle => {
|
|
76
|
+
tooltip = new Tooltip({ anchor, bundle, editor, rendermime });
|
|
77
|
+
Widget.attach(tooltip, document.body);
|
|
78
|
+
})
|
|
79
|
+
.catch(() => {
|
|
80
|
+
/* Fails silently. */
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The console tooltip plugin.
|
|
89
|
+
*/
|
|
90
|
+
const consoles: JupyterFrontEndPlugin<void> = {
|
|
91
|
+
id: '@jupyterlab/tooltip-extension:consoles',
|
|
92
|
+
autoStart: true,
|
|
93
|
+
optional: [ITranslator],
|
|
94
|
+
requires: [ITooltipManager, IConsoleTracker],
|
|
95
|
+
activate: (
|
|
96
|
+
app: JupyterFrontEnd,
|
|
97
|
+
manager: ITooltipManager,
|
|
98
|
+
consoles: IConsoleTracker,
|
|
99
|
+
translator: ITranslator | null
|
|
100
|
+
): void => {
|
|
101
|
+
const trans = (translator ?? nullTranslator).load('jupyterlab');
|
|
102
|
+
|
|
103
|
+
// Add tooltip launch command.
|
|
104
|
+
app.commands.addCommand(CommandIDs.launchConsole, {
|
|
105
|
+
label: trans.__('Open the tooltip'),
|
|
106
|
+
execute: () => {
|
|
107
|
+
const parent = consoles.currentWidget;
|
|
108
|
+
|
|
109
|
+
if (!parent) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const anchor = parent.console;
|
|
114
|
+
const editor = anchor.promptCell?.editor;
|
|
115
|
+
const kernel = anchor.sessionContext.session?.kernel;
|
|
116
|
+
const rendermime = anchor.rendermime;
|
|
117
|
+
|
|
118
|
+
// If all components necessary for rendering exist, create a tooltip.
|
|
119
|
+
if (!!editor && !!kernel && !!rendermime) {
|
|
120
|
+
return manager.invoke({ anchor, editor, kernel, rendermime });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The notebook tooltip plugin.
|
|
129
|
+
*/
|
|
130
|
+
const notebooks: JupyterFrontEndPlugin<void> = {
|
|
131
|
+
id: '@jupyterlab/tooltip-extension:notebooks',
|
|
132
|
+
autoStart: true,
|
|
133
|
+
optional: [ITranslator],
|
|
134
|
+
requires: [ITooltipManager, INotebookTracker],
|
|
135
|
+
activate: (
|
|
136
|
+
app: JupyterFrontEnd,
|
|
137
|
+
manager: ITooltipManager,
|
|
138
|
+
notebooks: INotebookTracker,
|
|
139
|
+
translator: ITranslator | null
|
|
140
|
+
): void => {
|
|
141
|
+
const trans = (translator ?? nullTranslator).load('jupyterlab');
|
|
142
|
+
|
|
143
|
+
// Add tooltip launch command.
|
|
144
|
+
app.commands.addCommand(CommandIDs.launchNotebook, {
|
|
145
|
+
label: trans.__('Open the tooltip'),
|
|
146
|
+
execute: () => {
|
|
147
|
+
const parent = notebooks.currentWidget;
|
|
148
|
+
|
|
149
|
+
if (!parent) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const anchor = parent.content;
|
|
154
|
+
const editor = anchor.activeCell?.editor;
|
|
155
|
+
const kernel = parent.sessionContext.session?.kernel;
|
|
156
|
+
const rendermime = anchor.rendermime;
|
|
157
|
+
|
|
158
|
+
// If all components necessary for rendering exist, create a tooltip.
|
|
159
|
+
if (!!editor && !!kernel && !!rendermime) {
|
|
160
|
+
return manager.invoke({ anchor, editor, kernel, rendermime });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The file editor tooltip plugin.
|
|
169
|
+
*/
|
|
170
|
+
const files: JupyterFrontEndPlugin<void> = {
|
|
171
|
+
id: '@jupyterlab/tooltip-extension:files',
|
|
172
|
+
autoStart: true,
|
|
173
|
+
optional: [ITranslator],
|
|
174
|
+
requires: [ITooltipManager, IEditorTracker, IRenderMimeRegistry],
|
|
175
|
+
activate: (
|
|
176
|
+
app: JupyterFrontEnd,
|
|
177
|
+
manager: ITooltipManager,
|
|
178
|
+
editorTracker: IEditorTracker,
|
|
179
|
+
rendermime: IRenderMimeRegistry,
|
|
180
|
+
translator: ITranslator | null
|
|
181
|
+
): void => {
|
|
182
|
+
const trans = (translator ?? nullTranslator).load('jupyterlab');
|
|
183
|
+
|
|
184
|
+
// Keep a list of active ISessions so that we can
|
|
185
|
+
// clean them up when they are no longer needed.
|
|
186
|
+
const activeSessions: {
|
|
187
|
+
[id: string]: Session.ISessionConnection;
|
|
188
|
+
} = {};
|
|
189
|
+
|
|
190
|
+
const sessions = app.serviceManager.sessions;
|
|
191
|
+
// When the list of running sessions changes,
|
|
192
|
+
// check to see if there are any kernels with a
|
|
193
|
+
// matching path for the file editors.
|
|
194
|
+
const onRunningChanged = (
|
|
195
|
+
sender: Session.IManager,
|
|
196
|
+
models: Iterable<Session.IModel>
|
|
197
|
+
) => {
|
|
198
|
+
editorTracker.forEach(file => {
|
|
199
|
+
const model = find(models, m => file.context.path === m.path);
|
|
200
|
+
if (model) {
|
|
201
|
+
const oldSession = activeSessions[file.id];
|
|
202
|
+
// If there is a matching path, but it is the same
|
|
203
|
+
// session as we previously had, do nothing.
|
|
204
|
+
if (oldSession && oldSession.id === model.id) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// Otherwise, dispose of the old session and reset to
|
|
208
|
+
// a new CompletionConnector.
|
|
209
|
+
if (oldSession) {
|
|
210
|
+
delete activeSessions[file.id];
|
|
211
|
+
oldSession.dispose();
|
|
212
|
+
}
|
|
213
|
+
const session = sessions.connectTo({ model });
|
|
214
|
+
activeSessions[file.id] = session;
|
|
215
|
+
} else {
|
|
216
|
+
const session = activeSessions[file.id];
|
|
217
|
+
if (session) {
|
|
218
|
+
session.dispose();
|
|
219
|
+
delete activeSessions[file.id];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
onRunningChanged(sessions, sessions.running());
|
|
225
|
+
sessions.runningChanged.connect(onRunningChanged);
|
|
226
|
+
|
|
227
|
+
// Clean up after a widget when it is disposed
|
|
228
|
+
editorTracker.widgetAdded.connect((sender, widget) => {
|
|
229
|
+
widget.disposed.connect(w => {
|
|
230
|
+
const session = activeSessions[w.id];
|
|
231
|
+
if (session) {
|
|
232
|
+
session.dispose();
|
|
233
|
+
delete activeSessions[w.id];
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// Add tooltip launch command.
|
|
239
|
+
app.commands.addCommand(CommandIDs.launchFile, {
|
|
240
|
+
label: trans.__('Open the tooltip'),
|
|
241
|
+
execute: async () => {
|
|
242
|
+
const parent = editorTracker.currentWidget;
|
|
243
|
+
const kernel =
|
|
244
|
+
parent &&
|
|
245
|
+
activeSessions[parent.id] &&
|
|
246
|
+
activeSessions[parent.id].kernel;
|
|
247
|
+
if (!kernel) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const anchor = parent!.content;
|
|
251
|
+
const editor = anchor?.editor;
|
|
252
|
+
|
|
253
|
+
// If all components necessary for rendering exist, create a tooltip.
|
|
254
|
+
if (!!editor && !!kernel && !!rendermime) {
|
|
255
|
+
return manager.invoke({ anchor, editor, kernel, rendermime });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Export the plugins as default.
|
|
264
|
+
*/
|
|
265
|
+
const plugins: JupyterFrontEndPlugin<any>[] = [
|
|
266
|
+
manager,
|
|
267
|
+
consoles,
|
|
268
|
+
notebooks,
|
|
269
|
+
files
|
|
270
|
+
];
|
|
271
|
+
export default plugins;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A namespace for private data.
|
|
275
|
+
*/
|
|
276
|
+
namespace Private {
|
|
277
|
+
/**
|
|
278
|
+
* A counter for outstanding requests.
|
|
279
|
+
*/
|
|
280
|
+
let pending = 0;
|
|
281
|
+
|
|
282
|
+
export interface IFetchOptions {
|
|
283
|
+
/**
|
|
284
|
+
* The detail level requested from the API.
|
|
285
|
+
*
|
|
286
|
+
* #### Notes
|
|
287
|
+
* The only acceptable values are 0 and 1. The default value is 0.
|
|
288
|
+
* @see http://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection
|
|
289
|
+
*/
|
|
290
|
+
detail?: 0 | 1;
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The referent editor for the tooltip.
|
|
294
|
+
*/
|
|
295
|
+
editor: CodeEditor.IEditor;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The kernel against which the API request will be made.
|
|
299
|
+
*/
|
|
300
|
+
kernel: Kernel.IKernelConnection;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Fetch a tooltip's content from the API server.
|
|
305
|
+
*/
|
|
306
|
+
export function fetch(options: IFetchOptions): Promise<JSONObject> {
|
|
307
|
+
const { detail, editor, kernel } = options;
|
|
308
|
+
const code = editor.model.sharedModel.getSource();
|
|
309
|
+
const position = editor.getCursorPosition();
|
|
310
|
+
const offset = Text.jsIndexToCharIndex(editor.getOffsetAt(position), code);
|
|
311
|
+
|
|
312
|
+
// Clear hints if the new text value is empty or kernel is unavailable.
|
|
313
|
+
if (!code || !kernel) {
|
|
314
|
+
return Promise.reject(void 0);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const contents: KernelMessage.IInspectRequestMsg['content'] = {
|
|
318
|
+
code,
|
|
319
|
+
cursor_pos: offset,
|
|
320
|
+
detail_level: detail || 0
|
|
321
|
+
};
|
|
322
|
+
const current = ++pending;
|
|
323
|
+
|
|
324
|
+
return kernel.requestInspect(contents).then(msg => {
|
|
325
|
+
const value = msg.content;
|
|
326
|
+
|
|
327
|
+
// If a newer request is pending, bail.
|
|
328
|
+
if (current !== pending) {
|
|
329
|
+
return Promise.reject(void 0) as Promise<JSONObject>;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// If request fails or returns negative results, bail.
|
|
333
|
+
if (value.status !== 'ok' || !value.found) {
|
|
334
|
+
return Promise.reject(void 0) as Promise<JSONObject>;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return Promise.resolve(value.data);
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|