forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathreplCommandHandler.ts
More file actions
96 lines (90 loc) · 3.06 KB
/
replCommandHandler.ts
File metadata and controls
96 lines (90 loc) · 3.06 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
import {
commands,
window,
NotebookController,
NotebookEditor,
ViewColumn,
NotebookDocument,
NotebookCellData,
NotebookCellKind,
NotebookEdit,
WorkspaceEdit,
workspace,
} from 'vscode';
import { getExistingReplViewColumn } from './replUtils';
import { PVSC_EXTENSION_ID } from '../common/constants';
/**
* Function that opens/show REPL using IW UI.
* @param notebookController
* @param notebookEditor
* @returns notebookEditor
*/
export async function openInteractiveREPL(
notebookController: NotebookController,
notebookDocument: NotebookDocument | undefined,
): Promise<NotebookEditor> {
let viewColumn = ViewColumn.Beside;
// Case where NotebookDocument (REPL document already exists in the tab)
if (notebookDocument) {
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
viewColumn = existingReplViewColumn ?? viewColumn;
} else if (!notebookDocument) {
// Case where NotebookDocument doesnt exist, create a blank one.
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
}
const editor = window.showNotebookDocument(notebookDocument!, { viewColumn, asRepl: 'Python REPL' });
await commands.executeCommand('notebook.selectKernel', {
editor,
id: notebookController.id,
extension: PVSC_EXTENSION_ID,
});
return editor;
}
/**
* Function that selects notebook Kernel.
* @param notebookEditor
* @param notebookControllerId
* @param extensionId
* @return Promise<void>
*/
export async function selectNotebookKernel(
notebookEditor: NotebookEditor,
notebookControllerId: string,
extensionId: string,
): Promise<void> {
await commands.executeCommand('notebook.selectKernel', {
notebookEditor,
id: notebookControllerId,
extension: extensionId,
});
}
/**
* Function that executes notebook cell given code.
* @param notebookDocument
* @param code
* @return Promise<void>
*/
export async function executeNotebookCell(notebookEditor: NotebookEditor, code: string): Promise<void> {
const { notebook, replOptions } = notebookEditor;
const cellIndex = replOptions?.appendIndex ?? notebook.cellCount;
await addCellToNotebook(notebook, cellIndex, code);
// Execute the cell
commands.executeCommand('notebook.cell.execute', {
ranges: [{ start: cellIndex, end: cellIndex + 1 }],
document: notebook.uri,
});
}
/**
* Function that adds cell to notebook.
* This function will only get called when notebook document is defined.
* @param code
*
*/
async function addCellToNotebook(notebookDocument: NotebookDocument, index: number, code: string): Promise<void> {
const notebookCellData = new NotebookCellData(NotebookCellKind.Code, code as string, 'python');
// Add new cell to interactive window document
const notebookEdit = NotebookEdit.insertCells(index, [notebookCellData]);
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.set(notebookDocument!.uri, [notebookEdit]);
await workspace.applyEdit(workspaceEdit);
}