Skip to content

Commit 4589cfe

Browse files
authored
Merge pull request #24 from krassowski/notebook-completer
Allow to add code to run before invoking completer
2 parents 67e53d0 + 7b51b26 commit 4589cfe

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

src/dramaturg.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ async function waitElementHidden(selector: string, within?: Element) {
4848

4949
function waitForElement(selector: string, within?: Element): Promise<Element> {
5050
return new Promise(resolve => {
51+
const root = within || document.documentElement;
52+
const attributes = selector.includes('[') || selector.includes(':');
5153
const observer = new MutationObserver(mutations => {
5254
for (const mutation of mutations) {
55+
if (attributes && mutation.attributeName) {
56+
const match = root.querySelector(selector);
57+
if (match) {
58+
resolve(match);
59+
observer.disconnect();
60+
}
61+
}
5362
for (const node of mutation.addedNodes) {
5463
if (!(node instanceof HTMLElement)) {
5564
continue;
@@ -67,10 +76,10 @@ function waitForElement(selector: string, within?: Element): Promise<Element> {
6776
}
6877
});
6978

70-
observer.observe(within || document.documentElement, {
79+
observer.observe(root, {
7180
childList: true,
7281
subtree: true,
73-
attributes: selector.includes('[') || selector.includes(':')
82+
attributes: attributes
7483
});
7584

7685
const node = document.querySelector(selector);

src/scenarios.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,20 @@ export class CompleterScenario
240240
tokens.push('t');
241241
text = tokens.join('\n');
242242
}
243-
await insertText(this.jupyterApp, text);
243+
if (this.useNotebook && this.options.setup.setupCell) {
244+
await insertText(this.jupyterApp, this.options.setup.setupCell);
245+
await page.waitForSelector(
246+
'.jp-Notebook-ExecutionIndicator[data-status="idle"]',
247+
{ state: 'attached' }
248+
);
249+
await this.jupyterApp.commands.execute(
250+
'notebook:run-cell-and-insert-below'
251+
);
252+
await insertText(this.jupyterApp, text);
253+
await this.jupyterApp.commands.execute('notebook:enter-edit-mode');
254+
} else {
255+
await insertText(this.jupyterApp, text);
256+
}
244257

245258
if (!this.useNotebook) {
246259
// Scroll down a little bit to avoid out of view bug
@@ -268,8 +281,16 @@ export class CompleterScenario
268281
if (this.useNotebook) {
269282
// TODO enter a specific cell, not the first cell?
270283
const handle = new ElementHandle(this.widget!.node);
271-
const editor = await handle.$('.jp-Editor textarea');
272-
await editor!.focus();
284+
const editorSelector = document.querySelector('.cm-content')
285+
? '.cm-content'
286+
: 'textarea';
287+
const editor = this.options!.setup.setupCell
288+
? await handle.$(`.jp-Cell:nth-child(2) .jp-Editor ${editorSelector}`)
289+
: await handle.$(`.jp-Editor ${editorSelector}`);
290+
if (!editor) {
291+
throw Error('Setup failed: cell editor could not be located');
292+
}
293+
await editor.focus();
273294
}
274295
await layoutReady();
275296
await page.press('Tab');

src/schema/scenario-completer.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@
4949
"title": "I will provide a custom text",
5050
"properties": {
5151
"setupText": {
52-
"title": "Path",
52+
"title": "Trigger code",
5353
"description": "Text to enter into the editor. Last line should include a partial token on which the completion will be riggered.",
5454
"type": "string",
55-
"default": ""
55+
"default": "np."
56+
},
57+
"setupCell": {
58+
"title": "Code to run (notebook only)",
59+
"description": "Code to run prior to invoking completer, e.g. `import numpy as np`. Only has an effect in notebook.",
60+
"type": "string",
61+
"default": "import numpy as np"
5662
}
5763
},
58-
"required": ["setupText"]
64+
"required": ["setupText", "triggerCell"]
5965
}
6066
]
6167
}

src/types/_scenario-completer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export type TokenSize = number;
3030
/**
3131
* Text to enter into the editor. Last line should include a partial token on which the completion will be riggered.
3232
*/
33-
export type Path = string;
33+
export type TriggerCode = string;
34+
/**
35+
* Code to run prior to invoking completer, e.g. `import numpy as np`. Only has an effect in notebook.
36+
*/
37+
export type CodeToRunNotebookOnly = string;
3438

3539
export interface CompleterScenarioOptions {
3640
editor: EditorType;
@@ -43,6 +47,7 @@ export interface AutoGenerateTokensToComplete {
4347
[k: string]: any;
4448
}
4549
export interface IWillProvideACustomText {
46-
setupText: Path;
50+
setupText: TriggerCode;
51+
setupCell?: CodeToRunNotebookOnly;
4752
[k: string]: any;
4853
}

src/ui.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ export class BenchmarkHistory extends React.Component<
725725
? 'up-BenchmarkHistory-file up-BenchmarkHistory-file-active'
726726
: 'up-BenchmarkHistory-file'
727727
}
728+
key={file.name}
728729
onClick={() => {
729730
this.props.onSelect(file);
730731
this.setState({

0 commit comments

Comments
 (0)