|
| 1 | +const vscode = require('vscode'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +function getWorkspaceRoot() { |
| 5 | + const ws = vscode.workspace.workspaceFolders; |
| 6 | + if (!ws || ws.length === 0) return undefined; |
| 7 | + return ws[0].uri.fsPath; |
| 8 | +} |
| 9 | + |
| 10 | +function resolveInterpreterPath() { |
| 11 | + const config = vscode.workspace.getConfiguration(); |
| 12 | + const py = config.get('asmln.pythonPath'); |
| 13 | + return py || 'python'; |
| 14 | +} |
| 15 | + |
| 16 | +function ensureSaved(editor) { |
| 17 | + if (!editor) return Promise.resolve(true); |
| 18 | + if (!editor.document.isDirty) return Promise.resolve(true); |
| 19 | + return editor.document.save(); |
| 20 | +} |
| 21 | + |
| 22 | +function runCommandInTerminal(command, cwd) { |
| 23 | + const term = vscode.window.createTerminal({ cwd: cwd }); |
| 24 | + term.show(true); |
| 25 | + term.sendText(command); |
| 26 | +} |
| 27 | + |
| 28 | +function runFile() { |
| 29 | + const editor = vscode.window.activeTextEditor; |
| 30 | + if (!editor) { |
| 31 | + vscode.window.showInformationMessage('No active editor to run'); |
| 32 | + return; |
| 33 | + } |
| 34 | + ensureSaved(editor).then((ok) => { |
| 35 | + if (!ok) return; |
| 36 | + const filePath = editor.document.uri.fsPath; |
| 37 | + const root = getWorkspaceRoot() || path.dirname(filePath); |
| 38 | + const interp = resolveInterpreterPath(); |
| 39 | + const asmlnPath = path.join(root, 'asmln.py'); |
| 40 | + const cmd = `${interp} "${asmlnPath}" "${filePath}"`; |
| 41 | + runCommandInTerminal(cmd, root); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function runRepl() { |
| 46 | + const root = getWorkspaceRoot() || process.cwd(); |
| 47 | + const interp = resolveInterpreterPath(); |
| 48 | + const asmlnPath = path.join(root, 'asmln.py'); |
| 49 | + const cmd = `${interp} "${asmlnPath}"`; |
| 50 | + runCommandInTerminal(cmd, root); |
| 51 | +} |
| 52 | + |
| 53 | +/** @param {vscode.ExtensionContext} context */ |
| 54 | +function activate(context) { |
| 55 | + context.subscriptions.push( |
| 56 | + vscode.commands.registerCommand('asmln.runFile', runFile), |
| 57 | + vscode.commands.registerCommand('asmln.runRepl', runRepl) |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +function deactivate() {} |
| 62 | + |
| 63 | +module.exports = { activate, deactivate }; |
0 commit comments