-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathextension.js
More file actions
80 lines (64 loc) · 1.95 KB
/
extension.js
File metadata and controls
80 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const vscode = require('vscode');
const path = require('path');
let sharedTerminal = null;
/**
* Core function to detect the current Python function name and run a script.
*/
async function runPythonCommand(scriptName) {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const doc = editor.document;
await doc.save();
const cursorLine = editor.selection.active.line;
// Find the nearest "def"/"async def" above cursor
let funcName = null;
for (let i = cursorLine; i >= 0; i--) {
const lineText = doc.lineAt(i).text.trim();
const match = lineText.match(/^(?:async\s+def|def)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/);
if (match) {
funcName = match[1];
break;
}
}
if (!funcName) {
vscode.window.showErrorMessage("No enclosing Python function found.");
return;
}
const filePath = doc.fileName;
const fileDir = path.dirname(filePath);
let command = '';
if (scriptName == 'spin_func')
command = `python ${filePath} spin ${funcName}`;
else
command = `python ${filePath} run`;
// Reuse or create a single shared terminal
if (!sharedTerminal || sharedTerminal.exitStatus !== undefined) {
sharedTerminal = vscode.window.createTerminal('Metaflow Runner');
}
sharedTerminal.show();
sharedTerminal.sendText(`cd "${fileDir}"`);
sharedTerminal.sendText(command);
/*
vscode.window.showInformationMessage(
`${scriptName.toUpperCase()}: ${funcName} from ${path.basename(filePath)}`
);
*/
}
function activate(context) {
const runCmd = vscode.commands.registerCommand(
'extension.runPythonFunction',
() => runPythonCommand('run_func')
);
const spinCmd = vscode.commands.registerCommand(
'extension.spinPythonFunction',
() => runPythonCommand('spin_func')
);
context.subscriptions.push(runCmd, spinCmd);
}
function deactivate() {
if (sharedTerminal) {
sharedTerminal.dispose();
sharedTerminal = null;
}
}
module.exports = { activate, deactivate };