VSCode 如何 _enable_ Regex 通过命令查找,而不是“切换" [英] VSCode How to _enable_ Regex Find by command, rather than "toggle"

查看:32
本文介绍了VSCode 如何 _enable_ Regex 通过命令查找,而不是“切换"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个宏来在 VSCode 中搜索正则表达式.

I want to write a macro to search for a Regex in VSCode.

检查编辑器中可用的不同功能,我发现 toggleFindRegex(默认绑定到 Alt-R 键)非常适合交互式使用.

Examining the different functionalities available in the editor I find toggleFindRegex, (bound to key Alt-R by default) which works well for interactive use.

然而,对于宏来说,切换"是行不通的.结果可能是开"或关",从宏的角度来看是五十五十个随机.

However, for a macro a "toggle" won't do. The result could be "on" or "off", fifty-fifty random from the macro's point of view.

理想情况下,我需要一个似乎不存在的 enableFindRegex 函数,或者在决定是否切换之前,宏检测当前 findRegex 状态的方法.

Ideally I would need an enableFindRegex function, which doesn't seem to exist, or alternatively a way for the macro to detect the current findRegex state before deciding whether to toggle it or not.

那么有人能指出我正确的方向吗?

So could anybody point me in the right direction on this?

推荐答案

在 v1.46 中能够打开一个新的搜索编辑器(不是搜索面板),带有各种参数,包括正则表达式按钮是打开还是关闭.

In v1.46 is the ability to open a new search editor (not the search panel), with various arguments including whether the regex button should be on or off.

SearchEditor:使用 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"
},

之前是否选择了正则表达式选项.其他可用参数:

whether the regex option was previously selected or not. Other arguments available:

{
  query: string,
  includes: string,
  excludes: string,
  contextLines: number,
  wholeWord: boolean,
  caseSensitive: boolean,
  regexp: boolean,
  useIgnores: boolean,
  showIncludesExcludes: boolean,
  // triggerSearch: boolean,             // to be in v.47
  // focusResults: boolean               // to be in v.47
} 

在将 triggerSearchfocusResults 添加到稳定版(大概是 1.47)之前,这里是一个宏,可以立即运行搜索并聚焦第一个结果:

Prior to triggerSearch and focusResults being added to the Stable version (presumably 1.47) here is a macro that immediately runs the search and focuses the first result:

"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
    ]
  }
]

这篇关于VSCode 如何 _enable_ Regex 通过命令查找,而不是“切换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆