Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
]
}
]
}
131 changes: 124 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,90 @@
const vscode = require('vscode');
const path = require('path');
const { spawn } = require('child_process');

let sharedTerminal = null;

/**
* Tree Data Provider for Metaflow Steps
*/
class MetaflowTreeProvider {
constructor() {
this._onDidChangeTreeData = new vscode.EventEmitter();
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
this.steps = [];
}

refresh() {
const editor = vscode.window.activeTextEditor;
if (!editor || !editor.document.fileName.endsWith('.py')) {
this.steps = [];
this._onDidChangeTreeData.fire();
return;
}

const pythonPath = vscode.workspace.getConfiguration('python').get('defaultInterpreterPath') || 'python';
const parserPath = path.join(__dirname, 'parser.py');
const filePath = editor.document.fileName;

const pyParser = spawn(pythonPath, [parserPath, filePath]);
let output = '';

pyParser.stdout.on('data', (data) => {
output += data.toString();
});

pyParser.stderr.on('data', (data) => {
console.error(`Parser error: ${data}`);
});

pyParser.on('close', (code) => {
if (code === 0) {
try {
this.steps = JSON.parse(output);
this._onDidChangeTreeData.fire();
} catch (e) {
console.error('Failed to parse JSON output from Python script', e);
}
}
});
}

getTreeItem(element) {
const treeItem = new vscode.TreeItem(element.name);
treeItem.description = element.type;
treeItem.tooltip = `${element.name} (${element.type})`;
treeItem.command = {
command: 'metaflow.goToStep',
title: 'Go to Step',
arguments: [element.line],
};

// Set icons based on step type
switch (element.type) {
case 'linear':
treeItem.iconPath = new vscode.ThemeIcon('arrow-right');
break;
case 'split':
treeItem.iconPath = new vscode.ThemeIcon('git-branch');
break;
case 'foreach':
treeItem.iconPath = new vscode.ThemeIcon('repo-forked');
break;
case 'join':
treeItem.iconPath = new vscode.ThemeIcon('git-merge');
break;
default:
treeItem.iconPath = new vscode.ThemeIcon('circle-outline');
}

return treeItem;
}

getChildren() {
return Promise.resolve(this.steps);
}
}

/**
* Core function to detect the current Python function name and run a script.
*/
Expand Down Expand Up @@ -48,15 +130,12 @@ async function runPythonCommand(scriptName) {
sharedTerminal.show();
sharedTerminal.sendText(`cd "${fileDir}"`);
sharedTerminal.sendText(command);

/*
vscode.window.showInformationMessage(
`${scriptName.toUpperCase()}: ${funcName} from ${path.basename(filePath)}`
);
*/
}

function activate(context) {
const treeProvider = new MetaflowTreeProvider();
vscode.window.registerTreeDataProvider('metaflow.stepOutline', treeProvider);

const runCmd = vscode.commands.registerCommand(
'extension.runPythonFunction',
() => runPythonCommand('run_func')
Expand All @@ -67,7 +146,45 @@ function activate(context) {
() => runPythonCommand('spin_func')
);

context.subscriptions.push(runCmd, spinCmd);
const goToStepCmd = vscode.commands.registerCommand(
'metaflow.goToStep',
(line) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const position = new vscode.Position(line - 1, 0);
editor.selection = new vscode.Selection(position, position);
editor.revealRange(new vscode.Range(position, position), vscode.TextEditorRevealType.InCenter);
}
}
);

const refreshCmd = vscode.commands.registerCommand('metaflow.refreshEntry', () => treeProvider.refresh());

// Auto-refresh logic
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(() => treeProvider.refresh()),
vscode.workspace.onDidSaveTextDocument((doc) => {
if (doc.fileName.endsWith('.py')) {
treeProvider.refresh();
}
})
);

// Debounced refresh on text change (optional but good)
let debounceTimer;
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument((e) => {
if (e.document === vscode.window.activeTextEditor?.document && e.document.fileName.endsWith('.py')) {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => treeProvider.refresh(), 1000);
}
})
);

context.subscriptions.push(runCmd, spinCmd, goToStepCmd, refreshCmd);

// Initial refresh
treeProvider.refresh();
}

function deactivate() {
Expand Down
74 changes: 59 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,84 @@
{
"name": "metaflow-dev",
"displayName": "Run and Spin Metaflow",
"version": "0.0.7",
"publisher": "local",
"engines": { "vscode": "^1.80.0" },
"activationEvents": [
"onCommand:extension.runPythonFunction",
"onCommand:extension.spinPythonFunction"
],
"repository": {
"type": "git",
"url": "https://github.com/outerbounds/metaflow-dev-vscode"
"displayName": "Metaflow Dev Tools",
"description": "Shortcuts and tools for Metaflow development in VS Code",
"version": "0.0.8",
"publisher": "nandinisaagar",
"engines": {
"vscode": "^1.75.0"
},
"categories": [
"Other"
],
"activationEvents": [],
"main": "./extension.js",
"contributes": {
"viewsContainers": {
"activitybar": [
{
"id": "metaflow-explorer",
"title": "Metaflow",
"icon": "$(rocket)"
}
]
},
"views": {
"metaflow-explorer": [
{
"id": "metaflow.stepOutline",
"name": "Step Outline"
}
]
},
"commands": [
{
"command": "extension.runPythonFunction",
"title": "Run a flow"
"title": "Metaflow: Run Current Step"
},
{
"command": "extension.spinPythonFunction",
"title": "Spin the current step"
"title": "Metaflow: Spin Current Step"
},
{
"command": "metaflow.goToStep",
"title": "Metaflow: Go to Step"
},
{
"command": "metaflow.refreshEntry",
"title": "Refresh",
"icon": "$(refresh)"
}
],
"menus": {
"view/title": [
{
"command": "metaflow.refreshEntry",
"when": "view == metaflow.stepOutline",
"group": "navigation"
}
]
},
"keybindings": [
{
"command": "extension.runPythonFunction",
"key": "ctrl+alt+r",
"when": "editorLangId == python"
"mac": "cmd+alt+r"
},
{
"command": "extension.spinPythonFunction",
"key": "ctrl+alt+s",
"when": "editorLangId == python"
"mac": "cmd+alt+s"
}
]
},
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "node ./test/runTest.js"
},
"devDependencies": {
"@types/vscode": "^1.75.0",
"@types/node": "16.x",
"eslint": "^8.34.0"
}
}
Loading