我想编写一个宏以在VSCode中搜索正则表达式。
检查编辑器中可用的不同功能后,我发现toggleFindRegex(默认绑定到键Alt-R)非常适合交互使用。
但是,对于宏来说,“切换”是无效的。从宏的角度来看,结果可能是“开”或“关”,即五十五个随机值。
理想情况下,我将需要一个似乎不存在的enableFindRegex函数,或者选择一种宏来在决定是否进行切换之前检测当前findRegex状态。
那么有人能为此指出正确的方向吗?
可能在v1.46中(现在在Insiders Build中)已经可以打开新的搜索编辑器(而不是搜索面板),并带有各种参数,包括正则表达式按钮应该打开还是关闭。
请参阅SearchEditor: Open new editor with args 。所以这有效:
{
"key": "ctrl+shift+g",
"command": "search.action.openNewEditor",
"args": {
"query": "\\w*",
"regexp": true,
"includes": "${fileDirname}", // restrict to current editor's directory
// "includes": "${file}", // restrict search to current file
"showIncludesExcludes": true,
"contextLines": 3,
},
"when": "editorTextFocus"
},
之前是否选择了正则表达式选项。其他可用参数:
{
query: string,
includes: string,
excludes: string,
contextLines: number,
wholeWord: boolean,
caseSensitive: boolean,
regexp: boolean,
useIgnores: boolean,
showIncludesExcludes: boolean,
}
并参见Perform search automatically when running search.action.openNewEditor 。
仅此命令及其参数可能意味着您不需要宏解决方案。但是这里有一个可以立即运行搜索并聚焦第一个结果:
"multiCommand.commands": [
{
"command": "multiCommand.runSearchEditorwithArguments",
"interval": 200, // needs a small delay so results are loaded before focusing them
"sequence": [
{
"command": "search.action.openNewEditor",
"args": {
"query": "(TODO|NOTE|BUG):",
"regexp": true,
"includes": "${fileDirname}",
"showIncludesExcludes": true,
"contextLines": 3,
}
},
"rerunSearchEditorSearch", // runs the search
"search.action.focusNextSearchResult" // focus first result
]
}
]