1
1
import * as vscode from "vscode" ;
2
- import { TextDocument , Range , Position , TextEditor } from "vscode" ;
2
+ import { TextDocument , Range , Position , TextEditor , Selection } from "vscode" ;
3
+ import { getExpandCodeList } from "./settings" ;
3
4
4
5
export function runEntireFile ( ) {
5
6
const textEditor = vscode . window . activeTextEditor ;
@@ -10,28 +11,88 @@ export function runEntireFile() {
10
11
11
12
export function runInferredCodeBlock ( ) {
12
13
const textEditor = vscode . window . activeTextEditor ;
14
+ console . log ( 'textEditor' , textEditor ) ;
13
15
if ( ! textEditor ) {
14
16
return ;
15
17
}
16
18
17
19
const initialCursorPosition = textEditor ?. selection . anchor ;
20
+ console . log ( 'initialCursorPosition' , initialCursorPosition ) ;
18
21
19
22
const expandedCodeRange = getExpandedCodeRegion (
20
23
textEditor ,
21
24
initialCursorPosition
22
25
) ;
26
+ console . log ( 'expandedCodeRange' , expandedCodeRange ) ;
27
+
23
28
const text = textEditor . document . getText ( expandedCodeRange ) ;
29
+ console . log ( 'text' , text ) ;
24
30
vscode . commands . executeCommand ( "jupyter.execSelectionInteractive" , text ) ;
31
+
32
+ // TODO: move cursor to end of range
33
+ const endPosition = new Position ( expandedCodeRange . end . line + 1 , 0 ) ;
34
+ const newSelection = new Selection ( endPosition , endPosition ) ;
35
+ textEditor . selections = [ newSelection ] ;
36
+
25
37
}
26
38
27
- function getExpandedCodeRegion ( editor : TextEditor , position : Position ) : Range {
39
+ function getExpandedCodeRegion (
40
+ editor : TextEditor ,
41
+ initialPosition : Position
42
+ ) : Range {
28
43
// Assuming that no text is selected
29
- const selection = editor . selection ;
30
- const beginPosition = selection . anchor ;
44
+ const beginRange = new Range ( initialPosition , initialPosition ) ;
45
+ console . log ( 'beginRange' , beginRange ) ;
46
+
47
+ const initialIndentText = getInitialIndentText ( editor , initialPosition ) ;
48
+ console . log ( 'initialIndentText' , initialIndentText ) ;
49
+
50
+ const finalRange = expandRangeDownward ( editor , beginRange , initialIndentText ) ;
51
+ console . log ( 'finalRange' , finalRange ) ;
52
+ return finalRange ;
53
+ }
54
+
55
+ function getInitialIndentText (
56
+ editor : TextEditor ,
57
+ initialPosition : Position
58
+ ) : string {
59
+ const lineText = editor . document . lineAt ( initialPosition . line ) . text ;
60
+ const indent = lineText . match ( / ^ \s + / ) ;
61
+ return indent ? indent [ 0 ] : "" ;
62
+ }
63
+
64
+ /**
65
+ * Expand range downwards
66
+ *
67
+ * @param editor [editor description]
68
+ * @param range Starting Range
69
+ * @param indent Indentation of original line
70
+ *
71
+ * @return {Range } [return description]
72
+ */
73
+ function expandRangeDownward (
74
+ editor : TextEditor ,
75
+ currentRange : Range ,
76
+ indent : string
77
+ ) : Range {
31
78
const document = editor . document ;
79
+ const expandCodeList = getExpandCodeList ( ) ;
80
+ // add whitespace to the list
81
+ const expandCode = [ "\\s" ] . concat ( expandCodeList ) . join ( "|" ) ;
82
+ const expandRegex = new RegExp ( `^(${ indent } (${ expandCode } )|\s*#|\s*$)` ) ;
32
83
33
- const lastLineNumber = document . lineCount ;
84
+ let nextLineNum = currentRange . end . line ;
85
+
86
+ // expand code to the bottom
87
+ while (
88
+ nextLineNum <= editor . document . lineCount &&
89
+ document . lineAt ( nextLineNum ) . text . match ( expandRegex )
90
+ ) {
91
+ nextLineNum += 1 ;
92
+ console . log ( 'adding a line number' )
93
+ }
34
94
35
- const cursorLineText = document . lineAt ( beginPosition . line ) ;
95
+ const endPosition = document . lineAt ( nextLineNum + 1 ) . range . end ;
96
+ return new Range ( currentRange . start , endPosition ) ;
36
97
}
37
98
0 commit comments