Skip to content

Commit 0058ff9

Browse files
authored
prefer selection if non empty (#5)
1 parent 45ac966 commit 0058ff9

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/run-code.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import { TextDocument, Range, Position, TextEditor, Selection } from "vscode";
2+
import { Range, Position, TextEditor, Selection } from "vscode";
33
import { getExpandCodeList } from "./settings";
44

55
export function runEntireFile() {
@@ -18,7 +18,9 @@ export function runInferredCodeBlock(): void {
1818
return;
1919
}
2020

21-
_runInferredCodeBlock(textEditor);
21+
const expandedCodeRange = inferCodeBlock(textEditor);
22+
const inferredBlockText = textEditor.document.getText(expandedCodeRange);
23+
executeText(inferredBlockText);
2224
}
2325

2426
export function runInferredCodeBlockAndMoveDown(): void {
@@ -27,43 +29,44 @@ export function runInferredCodeBlockAndMoveDown(): void {
2729
return;
2830
}
2931

30-
const expandedCodeRange = _runInferredCodeBlock(textEditor);
32+
const expandedCodeRange = inferCodeBlock(textEditor);
33+
const inferredBlockText = textEditor.document.getText(expandedCodeRange);
34+
executeText(inferredBlockText);
3135

3236
const endPosition = new Position(expandedCodeRange.end.line + 1, 0);
3337
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);
3547
}
3648

3749
/** 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 {
3954
textEditor.selections = [selection];
4055
textEditor.revealRange(selection);
4156
}
4257

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 {
5459
vscode.commands.executeCommand("jupyter.execSelectionInteractive", text);
55-
56-
return expandedCodeRange;
5760
}
5861

5962
function movePositionToStartOfLine(position: Position): Position {
6063
return position.with(undefined, 0);
6164
}
6265

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+
6770
// Assuming that no text is selected
6871
// In practice, initialPosition here is the beginning of a line
6972
const beginRange = new Range(initialPosition, initialPosition);

0 commit comments

Comments
 (0)