/
extension.js
94 lines (80 loc) · 2.81 KB
/
extension.js
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
let vscode = require('vscode');
const helpers = require('./helpers');
const pythonTerminalName = 'IPython';
let pythonTerminal = null;
let textQueue = [];
let waitsQueue = [];
function createPythonTerminal() {
textQueue = [];
waitsQueue = [];
pythonTerminal = vscode.window.createTerminal(pythonTerminalName);
sendQueuedText('ipython', 1500);
}
function removePythonTerminal() {
pythonTerminal = null;
textQueue = [];
waitsQueue = [];
}
function sendQueuedText(text, waitTime=50) {
textQueue.push(text);
waitsQueue.push(waitTime);
}
function queueLoop() {
if (textQueue.length > 0 && pythonTerminal !== null && pythonTerminal._queuedRequests.length === 0) {
const text = textQueue.shift();
const waitTime = waitsQueue.shift();
pythonTerminal.sendText(text);
setTimeout(queueLoop, waitTime);
} else {
setTimeout(queueLoop, 50);
}
}
function activate(context) {
vscode.window.onDidCloseTerminal(function(event) {
if (event._name === pythonTerminalName) {
removePythonTerminal();
}
});
queueLoop();
let sendSelectedToIPython = vscode.commands.registerCommand('ipython.sendSelectedToIPython', function () {
if (pythonTerminal === null) {
createPythonTerminal();
}
const editor = vscode.window.activeTextEditor;
const filename = editor.document.fileName;
let startLine, endLine;
if (editor.selection.isEmpty) {
startLine = editor.selection.active.line + 1
endLine = startLine;
} else {
startLine = editor.selection.start.line + 1;
endLine = editor.selection.end.line + 1;
}
sendQueuedText(`__file__ = r'${filename}'`);
sendQueuedText('import sys')
sendQueuedText('sys.path.append(os.path.dirname(__file__))')
const command = `%load -r ${startLine}-${endLine} ${filename}`;
sendQueuedText(command);
sendQueuedText('\n\n');
pythonTerminal.show();
});
let sendFileContentsToIPython = vscode.commands.registerCommand('ipython.sendFileContentsToIPython', function () {
if (pythonTerminal === null) {
createPythonTerminal();
}
const editor = vscode.window.activeTextEditor;
const filename = editor.document.fileName;
sendQueuedText(`__file__ = r'${filename}'`);
sendQueuedText('import sys')
sendQueuedText('sys.path.append(os.path.dirname(__file__))')
sendQueuedText(`%load ${filename}`, 100);
sendQueuedText('\n\n');
pythonTerminal.show();
});
context.subscriptions.push(sendSelectedToIPython);
context.subscriptions.push(sendFileContentsToIPython);
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;