如何评论VSCode中所有标记的可变长度块?
How to comment all marked variable length chunks in VSCode?
在 VSCode 1.60.2 中,我在分布式系统上工作并使用日志来帮助调试。调试时,多个进程各自创建自己的日志。我的代码中有很好的格式化输出,指定如下:
// non-debugging code
foo();
for (...)
write(...);
////// DEBUGGING CHUNK
write(...);
for (...) write(...);
write(...);
////// END DEBUGGING CHUNK
// non-debugging code
bar();
for (...)
if (...)
write(...);
////// DEBUGGING CHUNK
...
////// END DEBUGGING CHUNK
// non-debugging code
baz();
进程交互,执行速度影响它们的一些输出。因此,在某些情况下,我想尽量减少 运行 没有写入的代码的窃听。上面的伪代码可能看起来像:
// non-debugging code
foo();
for (...)
write(...);
// ////// DEBUGGING CHUNK
// write(...);
// for (...) write(...);
// write(...);
// ////// END DEBUGGING CHUNK
// non-debugging code
bar();
for (...)
if (...)
write(...);
// ////// DEBUGGING CHUNK
// ...
// ////// END DEBUGGING CHUNK
// non-debugging code
baz();
我一直在手动浏览我的文件,一次一个 comment/uncomment 个块(突出显示块,Ctrl+/
),但这需要一段时间。由于我的块具有标准格式(开始和结束格式;不一定是大小),我希望可能有一种方法可以同时对所有块进行注释和取消注释。
为了找到一个简单的解决方案,我尝试查看扩展,但发现主要是颜色突出显示和评论模板插入。
我也考虑过使用内置的替换功能,但不确定这会有什么帮助。我看到两个问题:(1) 调试块内部和外部的某些语句匹配,因此无法进行简单的替换,以及 (2) 我需要能够撤消可能来自正则表达式匹配替换的间距更改(仅“要替换的内容”字段采用正则表达式;我似乎无法用匹配的组替换)。
假设 //
是一个行注释,你可以用几个扩展来做到这一点。您将需要一个像 multi-command to run a series of commands. And an extension like Find and Transform 这样的宏扩展,它允许您保存查找查询。
在你的keybindings.json
中:
{
"key": "alt+z", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"interval": 100,
// a slight delay seems to be necessary, you could play with this if there are a lot of blocks in a file
"sequence": [
{
"command": "findInCurrentFile", // find and select the matches
"args": {
"find": "(////// DEBUGGING CHUNK\r\n)(^.*?\r\n)+?(////// END DEBUGGING CHUNK)",
// "find": "(////// DEBUGGING CHUNK\s)(^.*?\n)+?",
"isRegex": true
}
},
// create a cursor on each line of the selection
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHome",
// you can't simply toggle a line comment here because that would remove
// a '//' from the beginning of each of the block delimiters
// instead of adding two '//'
{
"command": "type", // add a line comment to the beginning of each line
"args": {
"text": "// "
}
}
]
},
"when": "editorTextFocus"
},
// to undo the comments
{
"key": "shift+alt+z",
"command": "extension.multiCommand.execute",
"args": {
"interval": 100,
"sequence": [
{
"command": "findInCurrentFile",
"args": {
"find": "(// ////// DEBUGGING CHUNK\r\n)(^.*?\r\n)+?(// ////// END DEBUGGING CHUNK)",
"isRegex": true
}
},
"editor.action.commentLine", // now the toggle works as expected
]
},
"when": "editorTextFocus"
},
正则表达式可能有所改进。也许在不久的将来,将不再需要 Find and Transform 扩展,因为合并了将其某些功能包含到 vscode.
中的工作
使用扩展程序 Select By and multi-command,您可以在 settings.json
中创建以下内容 multiCommand
- 转到文件顶部
- select 第一次出现
////// DEBUGGING CHUNK
- select 所有其他事件
- 标记select离子
的开始
- 将光标移动到匹配的末尾
////// END DEBUGGING CHUNK
- 从标记位置到当前位置创建select离子
- 切换行注释
- 删除多光标(转至顶部)
{
"command": "multiCommand.toggleDebugComment",
"sequence": [
"cursorTop",
{ "command": "selectby.regex", "args": { "forward": "(?=(////// DEBUGGING CHUNK))", "forwardInclude": false, "forwardNext": "{{1}}", "forwardNextInclude": true } },
"editor.action.selectHighlights",
{ "command": "selectby.mark", "args": { "first": true } },
{ "command": "moveby.regex", "args": { "regex": "////// END DEBUGGING CHUNK", "properties": ["next", "end"] } },
"selectby.mark",
"editor.action.commentLine",
"cursorTop"
]
}
编辑
在 Select By
的 v1.9.0 中,我已将 first
参数添加到 selectby.mark
在 VSCode 1.60.2 中,我在分布式系统上工作并使用日志来帮助调试。调试时,多个进程各自创建自己的日志。我的代码中有很好的格式化输出,指定如下:
// non-debugging code
foo();
for (...)
write(...);
////// DEBUGGING CHUNK
write(...);
for (...) write(...);
write(...);
////// END DEBUGGING CHUNK
// non-debugging code
bar();
for (...)
if (...)
write(...);
////// DEBUGGING CHUNK
...
////// END DEBUGGING CHUNK
// non-debugging code
baz();
进程交互,执行速度影响它们的一些输出。因此,在某些情况下,我想尽量减少 运行 没有写入的代码的窃听。上面的伪代码可能看起来像:
// non-debugging code
foo();
for (...)
write(...);
// ////// DEBUGGING CHUNK
// write(...);
// for (...) write(...);
// write(...);
// ////// END DEBUGGING CHUNK
// non-debugging code
bar();
for (...)
if (...)
write(...);
// ////// DEBUGGING CHUNK
// ...
// ////// END DEBUGGING CHUNK
// non-debugging code
baz();
我一直在手动浏览我的文件,一次一个 comment/uncomment 个块(突出显示块,Ctrl+/
),但这需要一段时间。由于我的块具有标准格式(开始和结束格式;不一定是大小),我希望可能有一种方法可以同时对所有块进行注释和取消注释。
为了找到一个简单的解决方案,我尝试查看扩展,但发现主要是颜色突出显示和评论模板插入。
我也考虑过使用内置的替换功能,但不确定这会有什么帮助。我看到两个问题:(1) 调试块内部和外部的某些语句匹配,因此无法进行简单的替换,以及 (2) 我需要能够撤消可能来自正则表达式匹配替换的间距更改(仅“要替换的内容”字段采用正则表达式;我似乎无法用匹配的组替换)。
假设 //
是一个行注释,你可以用几个扩展来做到这一点。您将需要一个像 multi-command to run a series of commands. And an extension like Find and Transform 这样的宏扩展,它允许您保存查找查询。
在你的keybindings.json
中:
{
"key": "alt+z", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"interval": 100,
// a slight delay seems to be necessary, you could play with this if there are a lot of blocks in a file
"sequence": [
{
"command": "findInCurrentFile", // find and select the matches
"args": {
"find": "(////// DEBUGGING CHUNK\r\n)(^.*?\r\n)+?(////// END DEBUGGING CHUNK)",
// "find": "(////// DEBUGGING CHUNK\s)(^.*?\n)+?",
"isRegex": true
}
},
// create a cursor on each line of the selection
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHome",
// you can't simply toggle a line comment here because that would remove
// a '//' from the beginning of each of the block delimiters
// instead of adding two '//'
{
"command": "type", // add a line comment to the beginning of each line
"args": {
"text": "// "
}
}
]
},
"when": "editorTextFocus"
},
// to undo the comments
{
"key": "shift+alt+z",
"command": "extension.multiCommand.execute",
"args": {
"interval": 100,
"sequence": [
{
"command": "findInCurrentFile",
"args": {
"find": "(// ////// DEBUGGING CHUNK\r\n)(^.*?\r\n)+?(// ////// END DEBUGGING CHUNK)",
"isRegex": true
}
},
"editor.action.commentLine", // now the toggle works as expected
]
},
"when": "editorTextFocus"
},
正则表达式可能有所改进。也许在不久的将来,将不再需要 Find and Transform 扩展,因为合并了将其某些功能包含到 vscode.
中的工作使用扩展程序 Select By and multi-command,您可以在 settings.json
multiCommand
- 转到文件顶部
- select 第一次出现
////// DEBUGGING CHUNK
- select 所有其他事件
- 标记select离子 的开始
- 将光标移动到匹配的末尾
////// END DEBUGGING CHUNK
- 从标记位置到当前位置创建select离子
- 切换行注释
- 删除多光标(转至顶部)
{
"command": "multiCommand.toggleDebugComment",
"sequence": [
"cursorTop",
{ "command": "selectby.regex", "args": { "forward": "(?=(////// DEBUGGING CHUNK))", "forwardInclude": false, "forwardNext": "{{1}}", "forwardNextInclude": true } },
"editor.action.selectHighlights",
{ "command": "selectby.mark", "args": { "first": true } },
{ "command": "moveby.regex", "args": { "regex": "////// END DEBUGGING CHUNK", "properties": ["next", "end"] } },
"selectby.mark",
"editor.action.commentLine",
"cursorTop"
]
}
编辑
在 Select By
的 v1.9.0 中,我已将 first
参数添加到 selectby.mark