Skip to content

Commit d16691a

Browse files
committed
somewhat working code block
1 parent 919590a commit d16691a

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

src/extension.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The module 'vscode' contains the VS Code extensibility API
33
// Import the module and reference it with the alias vscode in your code below
44
import * as vscode from "vscode";
5+
import { runEntireFile, runInferredCodeBlock } from "./run-code";
56

67
// this method is called when your extension is activated
78
// your extension is activated the very first time the command is executed
@@ -12,14 +13,20 @@ export function activate(context: vscode.ExtensionContext) {
1213
'Congratulations, your extension "vscode-jupyter-python" is now active!'
1314
);
1415

16+
const tmp = vscode.workspace.getConfiguration("vscode-jupyter-python");
17+
const expandCode = tmp.get("expandCode");
18+
19+
1520
// The command has been defined in the package.json file
1621
// Now provide the implementation of the command with registerCommand
1722
// The commandId parameter must match the command field in package.json
18-
let disposable = vscode.commands.registerCommand("vscode-jupyter-python.sayHello", () => {
23+
let disposable = vscode.commands.registerCommand("vscode-jupyter-python.sayHello", async () => {
1924
// The code you place here will be executed every time your command is executed
2025

2126
// Display a message box to the user
22-
vscode.window.showInformationMessage("Hello World from vscode-jupyter-python!");
27+
// vscode.window.showInformationMessage(`Hello World ${expandCode} from vscode-jupyter-python!`);
28+
// await vscode.commands.executeCommand('jupyter.execSelectionInteractive', "print('sent to Interactive Window by package')\n");
29+
runInferredCodeBlock();
2330
});
2431

2532
context.subscriptions.push(disposable);

src/run-code.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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";
34

45
export function runEntireFile() {
56
const textEditor = vscode.window.activeTextEditor;
@@ -10,28 +11,88 @@ export function runEntireFile() {
1011

1112
export function runInferredCodeBlock() {
1213
const textEditor = vscode.window.activeTextEditor;
14+
console.log('textEditor', textEditor);
1315
if (!textEditor) {
1416
return;
1517
}
1618

1719
const initialCursorPosition = textEditor?.selection.anchor;
20+
console.log('initialCursorPosition', initialCursorPosition);
1821

1922
const expandedCodeRange = getExpandedCodeRegion(
2023
textEditor,
2124
initialCursorPosition
2225
);
26+
console.log('expandedCodeRange', expandedCodeRange);
27+
2328
const text = textEditor.document.getText(expandedCodeRange);
29+
console.log('text', text);
2430
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+
2537
}
2638

27-
function getExpandedCodeRegion(editor: TextEditor, position: Position): Range {
39+
function getExpandedCodeRegion(
40+
editor: TextEditor,
41+
initialPosition: Position
42+
): Range {
2843
// 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 {
3178
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*$)`);
3283

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+
}
3494

35-
const cursorLineText = document.lineAt(beginPosition.line);
95+
const endPosition = document.lineAt(nextLineNum + 1).range.end;
96+
return new Range(currentRange.start, endPosition);
3697
}
3798

0 commit comments

Comments
 (0)