-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.js
362 lines (300 loc) · 10.8 KB
/
main.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use babel';
import { CompositeDisposable, Disposable, Emitter } from 'atom';
import React from 'react';
import ReactDOM from 'react-dom';
import v4 from 'uuid/v4';
import stripIndent from 'strip-indent';
import fs from 'fs';
import path from 'path';
import VariableExplorer from './variable-explorer';
// Should be unique among all plugins. At some point, hydrogen should provide
// an API that doesn't require adding fields to the kernel wrappers.
const PLUGIN_KEY = '_nikitakit_python';
const VARIABLE_EXPLORER_URI = 'atom://hydrogen-python/variable-explorer';
function _getUsername() {
return (
process.env.LOGNAME ||
process.env.USER ||
process.env.LNAME ||
process.env.USERNAME
);
}
function createMessageFactory(parent_header) {
return type => ({
header: {
username: _getUsername(),
session: parent_header.session, // check if this is correct
msg_type: type,
msg_id: v4(),
date: new Date(),
version: '5.0',
},
metadata: {},
parent_header,
content: {},
});
}
function codeFromFile(filename) {
// Takes a file and runs it in a (possibly remote) kernel, while trying not to
// pollute the kernel's global namespace. We supply all the python files this
// gets called on, so to avoid escaping issues we mandate that the files not
// contain ''' anywhere.
const fullPath = path.join(__dirname, '..', 'py', filename);
const contents = fs.readFileSync(fullPath, 'utf8');
if (contents.includes("'''")) {
throw new Error(`File ${filename} contains triple single-quotes`);
}
return `exec('''${contents}''', globals().copy())`;
}
function expandCode(editor, code) {
const selection = editor.getSelectedBufferRange();
const isSelection = !selection.start.isEqual(selection.end);
// if there is a selection, we don't need to expand the code
if (isSelection) {
return code;
}
const originalCodeLength = code.split('\n').length;
const bufferRowLength = editor.getLastBufferRow();
// trim right to avoid empty lines at the end of the selection
code = code.trimRight();
const cursorLineText = editor.lineTextForBufferRow(selection.start.row);
const lastCodeLineText = code.split('\n')[code.split('\n').length - 1];
const isRunWithoutMove = (code.includes(cursorLineText) &&
editor.lineTextForBufferRow(
Math.min(selection.start.row + code.split('\n').length - 1,
bufferRowLength)).trimRight() === lastCodeLineText);
// find start of executed code
let startBufferRow = selection.start.row;
if (!isRunWithoutMove) {
while (editor.lineTextForBufferRow(startBufferRow).trim() != lastCodeLineText.trim()) {
startBufferRow -= 1;
if (startBufferRow < 0) break;
}
startBufferRow = Math.max(0, startBufferRow - code.split('\n').length + 1);
}
// find original indent
const indent = (code.match(/^\s+/) || '');
// prepare expansion regex to the top
const prependCodeList = atom.config.get('hydrogen-python.prependCodeList');
const prependCode = prependCodeList.join('|');
const prependRegex = new RegExp(`^(${indent}(${prependCode}))`);
// if start line matches the prepend code regex
// expand to first non-prepend line
let prependOffset = 0;
while (editor.lineTextForBufferRow(startBufferRow + prependOffset).match(prependRegex)) {
prependOffset += 1;
code += `\n${editor.lineTextForBufferRow(startBufferRow + prependOffset)}`;
}
// function to identify breakpoints via tokens
const isBreakpoint = rownum =>
editor.tokenizedBuffer.tokenizedLines[rownum].tokens.reduce(
(accumulated, token) => token.scopes.concat(accumulated),
[],
).filter(
token => token.endsWith('.breakpoint'),
).length > 0;
// prepare expansion regex to the bottom
const expandCodeList = atom.config.get('hydrogen-python.expandCodeList');
// add whitespace to the list
const expandCode = ['\\s'].concat(expandCodeList).join('|');
const expandRegex = new RegExp(`^(${indent}(${expandCode})|\s*#|\s*$)`);
let lastRow = startBufferRow + (code.split('\n').length - 1);
const origRow = lastRow;
let nextRowText = (editor.lineTextForBufferRow(lastRow + 1) || '');
// expand code to the bottom
while (lastRow <= bufferRowLength && !isBreakpoint(lastRow) && nextRowText.match(expandRegex)) {
code += `\n${nextRowText}`;
lastRow += 1;
if (lastRow + 1 > editor.getLastBufferRow()) break;
nextRowText = editor.lineTextForBufferRow(lastRow + 1);
}
// expand code to the top
let prevRowText = (editor.lineTextForBufferRow(startBufferRow - 1) || '');
while (startBufferRow - 1 >= 0 && !isBreakpoint(startBufferRow - 1) && prevRowText.match(prependRegex)) {
code = `${prevRowText}\n${code}`;
startBufferRow -= 1;
if (startBufferRow <= 0) break;
prevRowText = editor.lineTextForBufferRow(startBufferRow - 1);
}
if (code.trimRight().split('\n').length > originalCodeLength) { console.log(`[hydrogen-python] executed code from line ${startBufferRow + 1} to line ${lastRow + 1}`); }
// if run-and-move set cursor to the beginning of the next content line
if (!isRunWithoutMove) {
let bufferRow = startBufferRow + code.split('\n').length;
while (bufferRow <= bufferRowLength && (editor.lineTextForBufferRow(bufferRow) || '').match(/^\s*($|#)/)) { bufferRow += 1; }
editor.setCursorBufferPosition([(bufferRow == startBufferRow) ? (bufferRow + 1) : (bufferRow), 0]);
}
return code;
}
class PythonKernelMod {
constructor(kernel, emitter) {
this.kernel = kernel;
this.emitter = emitter;
this.subscriptions = new CompositeDisposable();
this.kernel.addMiddleware(this);
this.kernelPluginInstalled = false;
this.kernelPluginFailed = false;
this.enableVariableExplorer = false;
this.subscriptions.add(
this.emitter.on('did-show-explorer', () => {
this.enableVariableExplorer = true;
}),
);
this.emitter.emit('did-install-middleware');
}
execute(next, code, onResults) {
let makeReply = null;
if (atom.config.get('hydrogen-python.expandCode')) {
const editor = atom.workspace.getActiveTextEditor();
code = expandCode(editor, code);
}
// IPython can string leading indentation if every line of code being run
// is indented the same amount. However, this only works if the first line
// is indented. We therefore trim empty lines at the start of the executed
// region.
code = code.replace(/^\n|\n$/g, '');
next.execute(code, (msg, channel) => {
if (!makeReply && msg.parent_header) {
makeReply = createMessageFactory(msg.parent_header);
}
onResults(msg, channel);
if (msg.header.msg_type == 'execute_reply') {
if (this.enableVariableExplorer) {
this.variableExplorerHook(next);
}
}
});
}
shutdown(next) {
next.shutdown();
this.kernelPluginInstalled = false;
this.kernelPluginFailed = false;
this.emitter.emit('did-update-vars', []);
}
restart(next, onRestarted) {
next.restart(onRestarted);
this.kernelPluginInstalled = false;
this.kernelPluginFailed = false;
this.emitter.emit('did-update-vars', []);
}
wrapDataHandler(dataHandler) {
return (msg, channel) => {
if (channel === 'iopub'
&& msg.header.msg_type === 'display_data'
&& msg.content.data
&& msg.content.data['application/json']
&& msg.content.data['application/json'].hydrogen_python) {
dataHandler(msg.content.data['application/json'].hydrogen_python);
}
};
}
startKernelPluginInstall(next, onInstalled) {
this.kernelPluginFailed = true;
const sanityCode = codeFromFile('sanity_check.py');
const installCode = codeFromFile('install_kernel_plugin.py');
next.execute(sanityCode, this.wrapDataHandler((data) => {
if (data !== 'pass') {
return;
}
next.execute(installCode, this.wrapDataHandler((data) => {
console.log('hydrogen-python: kernel plugin installed');
this.kernelPluginInstalled = true;
this.kernelPluginFailed = false;
if (onInstalled) {
onInstalled(next);
}
}));
}));
}
variableExplorerHook(next) {
if (this.kernelPluginFailed) {
return;
}
if (!this.kernelPluginInstalled) {
this.startKernelPluginInstall(next, this.variableExplorerHook.bind(this));
return;
}
next.execute("get_ipython()._hydrogen_python.run('variable_explorer_hook')",
this.wrapDataHandler((data) => {
if (data.error) {
console.error(data.error);
}
if (data.variables) {
this.emitter.emit('did-update-vars', data.variables);
}
}));
}
}
const HydrogenPythonPlugin = {
subscriptions: null,
hydrogen: null,
emitter: null,
activate() {
this.subscriptions = new CompositeDisposable();
this.emitter = new Emitter();
this.subscriptions.add(this.emitter);
this.subscriptions.add(
atom.workspace.addOpener((uri) => {
switch (uri) {
case VARIABLE_EXPLORER_URI:
return new VariableExplorerPane(this.emitter);
}
}),
// Destroy any Panes when the package is deactivated.
new Disposable(() => {
atom.workspace.getPaneItems().forEach((item) => {
if (item instanceof VariableExplorer) {
item.destroy();
}
});
}),
);
this.subscriptions.add(
atom.commands.add('atom-text-editor:not([mini])', {
'hydrogen-python:toggle-variable-explorer': () =>
atom.workspace.toggle(VARIABLE_EXPLORER_URI),
}),
);
},
deactivate() {
this.subscriptions.dispose();
},
consumeHydrogen(hydrogen) {
this.hydrogen = hydrogen;
this.hydrogen.onDidChangeKernel((kernel) => {
if (kernel && kernel.language === 'python' && !kernel[PLUGIN_KEY]) {
const kernelMod = new PythonKernelMod(kernel, this.emitter);
kernel[PLUGIN_KEY] = { mod: kernelMod };
}
});
return new Disposable(() => {
this.hydrogen = null;
});
},
};
class VariableExplorerPane {
reactElement = null;
element = document.createElement('div');
subscriptions = new CompositeDisposable();
constructor(emitter) {
const reactElement = <VariableExplorer emitter={emitter} />;
ReactDOM.render(reactElement, this.element);
this.subscriptions.add(new Disposable(() => {
ReactDOM.unmountComponentAtNode(this.element);
}));
emitter.emit('did-show-explorer');
this.subscriptions.add(
emitter.on('did-install-middleware', () => {
emitter.emit('did-show-explorer');
}),
);
}
getTitle = () => 'Variable Explorer';
getURI = () => VARIABLE_EXPLORER_URI;
getDefaultLocation = () => 'right';
getAllowedLocations = () => ['left', 'right', 'bottom'];
destroy() {
this.subscriptions.dispose();
this.element.remove();
}
}
export default HydrogenPythonPlugin;