[英]What is a VS Code Command to run a python file?
I have made an extension that opens a file dialog.我做了一个打开文件对话框的扩展。 What I would like to do is after the file is selected, I want a python file to run.
我想做的是在选择文件后,我想要一个 python 文件运行。 What I need is the VS Code command to run a file (or perhaps a python file specifically?).
我需要的是运行文件的 VS Code 命令(或者可能是 python 文件?)。
here is a working example where the command I use is a command that comments the selected line in the currently active editor.这是一个工作示例,其中我使用的命令是在当前活动编辑器中注释所选行的命令。 It works perfectly so I know this structure is generally correct.
它工作得很好,所以我知道这个结构通常是正确的。 I just need the right command to replace the comment line command.
我只需要正确的命令来替换注释行命令。
below is the code in questions with the working command I mentioned above.下面是我上面提到的工作命令有问题的代码。 I found it using this resource: where I found the comment line command
我使用这个资源找到了它: where I found the comment line command
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const { ChildProcess } = require('child_process');
const vscode = require('vscode');
const { execFile } = require('node:child_process');
const { stdout, stderr } = require('process');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('fileDialog.openFile', function () {
const options = {
canSelectMany: false,
openLabel: 'Open'
};
vscode.window.showOpenDialog(options).then(fileUri => {
if (fileUri && fileUri[0]) {
console.log('Selected file: ' + fileUri[0].fsPath);
vscode.commands.executeCommand('python.execInInterminal');
}
});
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}
You can go for child_process' exec or spawn if you only need to run the Python script(s).如果您只需要运行 Python 脚本,则可以 go 执行child_process或spawn 。 If you really prefer to rely on the Python extension, then you'll need to at least feed the script's Uri into the executeCommand's argument - its the 2nd part of what you found .
如果您真的更喜欢依赖 Python 扩展,那么您至少需要将脚本的 Uri 输入到 executeCommand 的参数中——它是您找到的第二部分。
function activate(context) {
let disposable = vscode.commands.registerCommand('fileDialog.openFile', function () {
const options = {
canSelectMany: false,
openLabel: 'Open',
filters: {
'Python script': ['py']
}
};
vscode.window.showOpenDialog(options).then((fileUris) => {
// This will always get an array of Uris regardless of canSelectMany being false
fileUris?.forEach(async (fileUri) => {
console.log(`Selected file: ${fileUri.fsPath}`);
await vscode.commands.executeCommand('python.execInInterminal', fileUri.fsPath);
});
});
});
context.subscriptions.push(disposable);
}
If you want to handle more things, you can refer to the thenable unittest code they included in the repo: https://github.com/microsoft/vscode-python/blob/main/src/test/smoke/runInTerminal.smoke.test.ts#L46-L48如果你想处理更多的事情,你可以参考他们包含在 repo 中的 thenable unittest 代码: https://github.com/microsoft/vscode-python/blob/main/src/test/smoke/runInTerminal.smoke。 test.ts#L46-L48
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.