Skip to content

Commit

Permalink
Improve run-until-cursor extension, and add default configuration to …
Browse files Browse the repository at this point in the history
…vscode settings
  • Loading branch information
pabloarosado committed Mar 7, 2025
1 parent ce3816c commit 7813a63
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"**/*.meta.yml",
"**/*.meta.override.yml"
],
"schemas/multidim-schema.json": "export/multidim/**/*.yml",
"schemas/multidim-schema.json": "export/multidim/**/*.yml"
},
"[python]": {
"editor.formatOnSave": true,
Expand Down Expand Up @@ -50,5 +50,9 @@
{
"file": ".copilot-commit-message-instructions.md"
}
]
],

// Settings for the "Run Until Cursor" extension
"runUntilCursor.execDelay": 100, // Delay before executing code (default: 100ms)
"runUntilCursor.cursorDelay": 500 // Delay before moving the cursor into run() (default: 500ms)
}
Binary file not shown.
75 changes: 52 additions & 23 deletions vscode_extensions/run-until-cursor/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as vscode from "vscode";

export function activate(context: vscode.ExtensionContext) {
console.log("🟢 Run Until Cursor Extension is activating...");

let disposable = vscode.commands.registerCommand("extension.runUntilCursor", async () => {
console.log("✅ Command extension.runUntilCursor is running!");

const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage("No active editor found.");
Expand All @@ -15,41 +11,74 @@ export function activate(context: vscode.ExtensionContext) {
const document = editor.document;
let cursorLine = editor.selection.active.line;
let startLine: number | null = null;
let endLine: number | null = null;

// Find 'def run()'
// Read timeout values from VS Code settings (default: 100ms and 500ms)
const execDelay = vscode.workspace.getConfiguration("runUntilCursor").get<number>("execDelay", 100);
const cursorDelay = vscode.workspace.getConfiguration("runUntilCursor").get<number>("cursorDelay", 500);

// Step 1: Find where `run()` starts
for (let i = 0; i < document.lineCount; i++) {
if (/^\s*def run\(/.test(document.lineAt(i).text)) {
startLine = i + 1;
startLine = i + 1; // Start selection after `def run():`
break;
}
}

if (startLine === null) {
vscode.window.showErrorMessage("Could not find `run()`.");
return;
}
if (startLine !== null) {
// Step 2: Find where `run()` ends (detecting **non-indented** lines)
for (let i = startLine; i < document.lineCount; i++) {
const lineText = document.lineAt(i).text;

// If we find a **non-indented line** (not empty & not a comment), assume `run()` ends here
if (lineText.trim().length > 0 && !lineText.startsWith(" ") && !lineText.startsWith("\t")) {
endLine = i;
break;
}
}

// If cursor is outside 'run()', execute the full function
if (cursorLine < startLine) {
cursorLine = document.lineCount - 1;
// If no non-indented line was found, assume `run()` ends at the last line
if (endLine === null) {
endLine = document.lineCount;
}
}

// Select the range
const range = new vscode.Range(startLine, 0, cursorLine + 1, 0);
editor.selection = new vscode.Selection(range.start, range.end);
let executeRange: vscode.Range;
let shouldMoveCursor = false;

console.log("📤 Selection updated. Executing in Interactive Window...");
if (startLine !== null && endLine !== null) {
// If inside `run()`, execute up to the cursor
if (cursorLine >= startLine && cursorLine < endLine) {
executeRange = new vscode.Range(startLine, 0, cursorLine + 1, 0);
}
// If outside `run()` (either above or below), execute **the entire script**
else {
executeRange = new vscode.Range(0, 0, document.lineCount, 0);
shouldMoveCursor = true;
}
} else {
// If `run()` is not found, execute everything as a fallback
executeRange = new vscode.Range(0, 0, document.lineCount, 0);
}

// Step 3: Apply selection and execute immediately (with an adjustable delay)
editor.selection = new vscode.Selection(executeRange.start, executeRange.end);

// Wait a short moment to ensure selection is applied before execution
setTimeout(async () => {
await vscode.commands.executeCommand("jupyter.execSelectionInteractive");
}, 100);
}, execDelay); // User-configurable execution delay

// Step 4: Move cursor inside `run()` **only if the cursor was outside**
if (shouldMoveCursor && startLine !== null) {
setTimeout(() => {
const newPosition = new vscode.Position(startLine, 0);
editor.selection = new vscode.Selection(newPosition, newPosition);
editor.revealRange(new vscode.Range(newPosition, newPosition));
}, cursorDelay); // User-configurable cursor movement delay
}
});

context.subscriptions.push(disposable);
console.log("🟢 Command extension.runUntilCursor registered!");
}

export function deactivate() {
console.log("🔴 Run Until Cursor Extension deactivated.");
}
export function deactivate() {}

0 comments on commit 7813a63

Please sign in to comment.