您可以使用诸如multi-command之类的宏扩展来完成此操作。您确实必须硬编码一个粗略的猜测,以确定您想要将注释对齐到右侧多远。你不能简单地测量最远的距离并使用它——你需要一个更复杂的扩展来做到这一点。但是,最后您将有多个光标,如果您不喜欢它们在演示中的结束位置,那么一次调整所有注释很容易,因为在演示中退格和制表符移动所有注释对齐。
演示:

使用一些键绑定来触发宏:
{
"key": "alt+w", // whatever keybinding you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.alignComments" },
"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.js/"
},
我是为.js文件制作的,但您可以为其他扩展名修改它,例如
resourceExtname =~ /\\.(js$|php)/ 对于 .js 和 .php 文件。
在您的 settings.json 中,实际的宏:
"multiCommand.commands": [
{
"command": "multiCommand.alignComments",
// "interval": 3000,
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHomeSelect",
{
"command": "editor.action.insertSnippet", // pad with lots of spaces's'
"args": {
// so capture group 1 is before the comment, add spaces after it
"snippet": "${TM_SELECTED_TEXT/^([^;]+;)(\\s*)(?=\\/\\/.*)/$1 /g}",
}
},
"cursorHomeSelect",
{
"command": "editor.action.insertSnippet",
"args": {
// keep first 25 characters or wherever you want to align
// and then add the comments
"snippet": "${TM_SELECTED_TEXT/(.{25})(\\s*)(.*)/$1$3/g}",
}
},
// "removeSecondaryCursors" // to exit multi-cursor mode
]
}
]
只需点击Escape退出多光标模式(或将该命令添加到宏的末尾)。