visual-studio-code - 如何使用脚本折叠 Visual Studio 代码中所有打开的编辑器
问题描述
我希望能够创建/编写一个命令来折叠 Visual Studio 代码中所有打开的编辑器中的所有代码。
我相信我非常接近。
我正在使用Marcel J. Kloubert编写的“脚本命令”扩展
当我在一个组中使用以下脚本和 7 个左右的打开编辑器时。我实现了以下目标:
- 打开的编辑器(在执行时)将其代码折叠起来
- VSC 将遍历打开的编辑器
- 没有其他编辑器的代码被折叠
我正在使用的脚本:
// Fold all code in all open editors.
function execute(args) {
// Obtain access to vscode
var vscode = args.require('vscode');
// Set number of open editors... (future: query vscode for number of open editors)
var numOpenEditor = 20;
// Loop for numOpenEditor times
for (var i = 0; i <= numOpenEditor; i++){
// Fold the current open editor
vscode.commands.executeCommand('editor.foldAll');
// Move to the next editor to the right
vscode.commands.executeCommand('workbench.action.nextEditor');
// Loop message
var statusString = 'Loop ->' + i
// print message
vscode.window.showErrorMessage(statusString);
}
}
// Script Commands must have a public execute() function to work.
exports.execute = execute;
我做了一个有趣的观察,当我使用上面的脚本和 7 个左右的开放编辑器和两个或更多组时。关于切换到新组的某些内容将允许该命令editor.foldAll
起作用。请注意,如果一个组有多个编辑器,则折叠其代码的唯一编辑器是该组中打开的编辑器。因此,所有其他编辑器都不会折叠。
我还想也许……脚本需要放慢速度,所以我添加了一个函数来暂停每次迭代。结果也没有用。
任何帮助都会很棒!
解决方案
您只需将此函数设为异步并等待 executeCommand 调用完成,然后再继续:
// Fold all code in all open editors.
async function execute(args) {
// Obtain access to vscode
var vscode = args.require('vscode');
// Set number of open editors... (future: query vscode for number of open editors)
var numOpenEditor = 5;
// Loop for numOpenEditor times
for (var i = 0; i <= numOpenEditor; i++) {
// Fold the current open editor
await vscode.commands.executeCommand('editor.foldAll');
// Move to the next editor to the right
await vscode.commands.executeCommand('workbench.action.nextEditor');
// Loop message
var statusString = 'Loop ->' + i
// print message
vscode.window.showErrorMessage(statusString);
}
}
// Script Commands must have a public execute() function to work.
exports.execute = execute;
推荐阅读
- python - 熊猫df。将一个数据框的列值与另一个数据框的列值匹配
- java - 设置打包在 WAR 中的 JAR 名称
- javascript - 将数据添加到数据库时未触发 Firebase 功能
- neural-network - 索引数组无法与形状 (103,) (103,10) 一起广播
- javascript - Burgermenu:如果单击图标,则为 slideToggle,如果单击其他地方,则为 slideUp(使用 jQuery)
- python - 优雅退出 Python 并输出
- python - 在 python 中编辑文本文件并创建一个新的文本文件
- javascript - RegEx JavaScript - 字符串不能全是点
- elasticsearch - 在 ElasticSearch 服务器的 aggs 查询中放置数据范围过滤器的位置?
- angular - 与 angulajs webpack 不同,浏览器中的 Angular 路由脚本获取和路由可以使用 publicpath