-
Notifications
You must be signed in to change notification settings - Fork 3
/
run-code.ts
125 lines (103 loc) · 3.58 KB
/
run-code.ts
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
import * as vscode from "vscode";
import { TextDocument, Range, Position, TextEditor, Selection } from "vscode";
import { getExpandCodeList } from "./settings";
export function runEntireFile() {
const textEditor = vscode.window.activeTextEditor;
const document = textEditor?.document;
const text = document?.getText();
vscode.commands.executeCommand("jupyter.execSelectionInteractive", text);
}
/**
* Run an inferred code block in the IPython kernel
*/
export function runInferredCodeBlock(): void {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return;
}
_runInferredCodeBlock(textEditor);
}
export function runInferredCodeBlockAndMoveDown(): void {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return;
}
const expandedCodeRange = _runInferredCodeBlock(textEditor);
const endPosition = new Position(expandedCodeRange.end.line + 1, 0);
const newSelection = new Selection(endPosition, endPosition);
textEditor.selections = [newSelection];
}
function _runInferredCodeBlock(textEditor: TextEditor): Range {
const initialCursorPosition = movePositionToStartOfLine(
textEditor.selection.anchor
);
const expandedCodeRange = getExpandedCodeRegion(
textEditor,
initialCursorPosition
);
const text = textEditor.document.getText(expandedCodeRange);
vscode.commands.executeCommand("jupyter.execSelectionInteractive", text);
return expandedCodeRange;
}
function movePositionToStartOfLine(position: Position): Position {
return position.with(undefined, 0);
}
function getExpandedCodeRegion(
editor: TextEditor,
initialPosition: Position
): Range {
// Assuming that no text is selected
// In practice, initialPosition here is the beginning of a line
const beginRange = new Range(initialPosition, initialPosition);
console.log("beginRange", beginRange);
const initialIndentText = getInitialIndentTextAtLine(editor, initialPosition);
console.log("initialIndentText", initialIndentText);
const finalRange = expandRangeDownward(editor, beginRange, initialIndentText);
console.log("finalRange", finalRange);
return finalRange;
}
function getInitialIndentTextAtLine(
editor: TextEditor,
initialPosition: Position
): string {
const lineText = editor.document.lineAt(initialPosition.line).text;
const indent = lineText.match(/^\s+/);
return indent ? indent[0] : "";
}
/**
* Expand range downwards
*
* @param editor [editor description]
* @param range Starting Range
* @param indent Indentation of original line
*
* @return {Range} [return description]
*/
function expandRangeDownward(
editor: TextEditor,
currentRange: Range,
indent: string
): Range {
const document = editor.document;
const expandCodeList = getExpandCodeList();
// add whitespace to the list
const expandCode = ["\\s"].concat(expandCodeList).join("|");
const expandRegex = new RegExp(`^(${indent}(${expandCode})|\s*#|\s*$)`);
const whitespaceOnlyRegex = new RegExp("^\\s*$");
let nextLineNum = currentRange.end.line + 1;
// expand code to the bottom
while (
nextLineNum < editor.document.lineCount &&
(document.lineAt(nextLineNum).text.match(whitespaceOnlyRegex) ||
document.lineAt(nextLineNum).text.match(expandRegex))
) {
nextLineNum += 1;
console.log("adding a line number:", nextLineNum);
}
console.log("nextLineNum", nextLineNum);
const endPosition = document.lineAt(nextLineNum - 1).range.end;
console.log("endPosition", endPosition);
const endRange = new Range(currentRange.start, endPosition);
console.log("endRange", endRange);
return endRange;
}