1
1
import * as vscode from "vscode" ;
2
- import { TextDocument , Range , Position , TextEditor , Selection } from "vscode" ;
2
+ import { Range , Position , TextEditor , Selection } from "vscode" ;
3
3
import { getExpandCodeList } from "./settings" ;
4
4
5
5
export function runEntireFile ( ) {
@@ -18,7 +18,9 @@ export function runInferredCodeBlock(): void {
18
18
return ;
19
19
}
20
20
21
- _runInferredCodeBlock ( textEditor ) ;
21
+ const expandedCodeRange = inferCodeBlock ( textEditor ) ;
22
+ const inferredBlockText = textEditor . document . getText ( expandedCodeRange ) ;
23
+ executeText ( inferredBlockText ) ;
22
24
}
23
25
24
26
export function runInferredCodeBlockAndMoveDown ( ) : void {
@@ -27,43 +29,44 @@ export function runInferredCodeBlockAndMoveDown(): void {
27
29
return ;
28
30
}
29
31
30
- const expandedCodeRange = _runInferredCodeBlock ( textEditor ) ;
32
+ const expandedCodeRange = inferCodeBlock ( textEditor ) ;
33
+ const inferredBlockText = textEditor . document . getText ( expandedCodeRange ) ;
34
+ executeText ( inferredBlockText ) ;
31
35
32
36
const endPosition = new Position ( expandedCodeRange . end . line + 1 , 0 ) ;
33
37
const newSelection = new Selection ( endPosition , endPosition ) ;
34
- setSelection ( textEditor , newSelection ) ;
38
+ setSelectionAndMoveDown ( textEditor , newSelection ) ;
39
+ }
40
+
41
+ function inferCodeBlock ( textEditor : TextEditor ) : Range {
42
+ const initialSelection = textEditor . selection ;
43
+
44
+ return initialSelection . isEmpty
45
+ ? getExpandedCodeRegion ( textEditor )
46
+ : new Range ( initialSelection . start , initialSelection . end ) ;
35
47
}
36
48
37
49
/** Set selection in given text editor and scroll down */
38
- function setSelection ( textEditor : TextEditor , selection : Selection ) : void {
50
+ function setSelectionAndMoveDown (
51
+ textEditor : TextEditor ,
52
+ selection : Selection
53
+ ) : void {
39
54
textEditor . selections = [ selection ] ;
40
55
textEditor . revealRange ( selection ) ;
41
56
}
42
57
43
- function _runInferredCodeBlock ( textEditor : TextEditor ) : Range {
44
- const initialCursorPosition = movePositionToStartOfLine (
45
- textEditor . selection . anchor
46
- ) ;
47
-
48
- const expandedCodeRange = getExpandedCodeRegion (
49
- textEditor ,
50
- initialCursorPosition
51
- ) ;
52
-
53
- const text = textEditor . document . getText ( expandedCodeRange ) ;
58
+ function executeText ( text : string ) : void {
54
59
vscode . commands . executeCommand ( "jupyter.execSelectionInteractive" , text ) ;
55
-
56
- return expandedCodeRange ;
57
60
}
58
61
59
62
function movePositionToStartOfLine ( position : Position ) : Position {
60
63
return position . with ( undefined , 0 ) ;
61
64
}
62
65
63
- function getExpandedCodeRegion (
64
- editor : TextEditor ,
65
- initialPosition : Position
66
- ) : Range {
66
+ function getExpandedCodeRegion ( editor : TextEditor ) : Range {
67
+ // When inferring an expanded code range, always start at the beginning of a line
68
+ const initialPosition = movePositionToStartOfLine ( editor . selection . anchor ) ;
69
+
67
70
// Assuming that no text is selected
68
71
// In practice, initialPosition here is the beginning of a line
69
72
const beginRange = new Range ( initialPosition , initialPosition ) ;
0 commit comments