【问题标题】:VSCode How to _enable_ Regex Find by command, rather than "toggle"VSCode 如何 _enable_ Regex 通过命令查找,而不是“切换”
【发布时间】:2019-08-16 16:13:07
【问题描述】:

我想编写一个宏来在 VSCode 中搜索 Regex。

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

但是,对于宏,“切换”是行不通的。结果可能是“开”或“关”,从宏的角度来看是随机的 550 个。

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

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

【问题讨论】:

  • 我认为不可能(在扩展之外)在宏中设置 findRegex 状态。我已经调查了两次,但一无所获。请参阅 stackoverflow.com/questions/54305750/…stackoverflow.com/questions/55281939/…,这可能对您有所帮助。
  • 谢谢马克,我担心会是这样。我想问题就变成了:“我怎样才能从插件中做到这一点?”除了我还没有涉足插件创建,甚至不知道正确地提出问题或理解答案。尽管如此,如果有更多知识的人对此提出任何想法,我们将不胜感激。

标签: visual-studio-code vscode-settings


【解决方案1】:

您可以在宏中使用此命令,因为最近添加了对其参数的支持:

{
  "command": "editor.actions.findWithArgs",
  "args": {

    // "findInSelection": false,
    // "isCaseSensitive": true,
    // "matchCaseOverride": 0,       // appears to not be implemented
    // "preserveCase": true,        
    // "preserveCaseOverride": 0,    // appears to not be implemented

    "isRegex": true

    // "regexOverride": 1,           // appears to not be implemented
    // "replaceString": "asdasd",
    // "searchString": "qweqwe",
    // "matchWholeWord": true,
    // "wholeWordOverride": 0        // appears to not be implemented
  }
}

这将打开查找小部件并选择正则表达式选项。当然,您可以根据需要使用其他 args - 有些尚未实现,并且智能感知在键绑定中的此命令上做得很差 - 我已指出哪些 args 尚未实现和/或正确的键名。

-----上一个答案--------------

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

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,
  // triggerSearch: boolean,             // to be in v.47
  // focusResults: boolean               // to be in v.47
} 

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 2012-05-31
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多