您可以使用multi-command 之类的宏扩展来完成此操作。您确实必须硬编码一个粗略的猜测,以确定要对齐 cmets 向右多远。你不能简单地测量最远的距离并使用它——你需要一个更复杂的扩展来做到这一点。但是,最后您将拥有多个光标,如果您不喜欢它们最终的位置,那么一次调整所有 cmets 很容易,就像在演示中退格和制表符移动所有 cmets 对齐一样。
演示:
使用一些键绑定来触发宏:
{
"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 即可退出多光标模式(或将该命令添加到宏的末尾)。