vscode:进行简单全局搜索的扩展

vscode: Extension to do simple global search

我想创建一个扩展来完成一个非常简单的任务:

我从命令托盘调用命令:

> commandName: Query

它应该开始全局搜索,在正则表达式模式下:

SomeToken AnotherToken (some|regex*|prefix?|foo) Query

重点是我不想一直输入 SomeToken AnotherToken (some|regex*|prefix?|foo) 前缀。

这听起来像是一个非常简单的过程,我希望它在 vscode 中成为可能,但我还没有在 VSCode API 和相关的教程。

我发布了一个扩展,它允许您创建和保存预定义的 find/replaces 或 search/replaces: Find and Transform 并通过命令面板中的命令或通过键绑定调用它们.示例设置:

    "findInCurrentFile": {
        "upcaseSwap2": {
            "title": "swap iif <==> hello",
            "find": "(iif) (hello)",
            "replace": "_\u_ _\U_",  // double-escaped case modifiers
            "restrictFind": "selections"
        },
        "upcaseSelectedKeywords": {
            "title": "Uppercase selected Keywords",
            "find": "(epsilon|alpha|beta)",
            "replace": "\U",
            // "restrictFind": "selections"
        }
    },

    "runInSearchPanel": {
        "removeDigits": {
            "title": "Remove digits from Arturo",
            "find": "^(\s*Arturo)\d+",
            "replace": "",
            "isRegex": true,
            "triggerSearch": true,
            // "filesToInclude": "${file}"
            "filesToInclude": "${file}",
        }
    },

  • 我认为没有任何方法可以像您所希望的那样使用参数调用命令面板中的命令。您可以打开一个 InputBox 并询问该值(这很容易做到)或使用当前选定的文本。下面我展示了两种方式。

首先,对于当前选定的文本,您可以尝试简单的键绑定,看看是否足够:

{
  "key": "ctrl+shift+g",                    // whatever keybinding you wish
  "command": "search.action.openNewEditor",
  "args": {

    "query": "(enum|struct|fn|trait|impl(<.*>)?|type) ${selectedText}",

    "isRegexp": true,

    // "includes": "${relativeFile}",  // works

    "showIncludesExcludes": true,
    "triggerSearch": true,
    "contextLines": 2,
    "focusResults": true,
  },
  "when": "editorTextFocus"
}

它会打开一个搜索编辑器,而不是使用您的搜索 view/panel。如果您想在搜索 view/panel 中找到它,那就没那么容易了。不幸的是 workbench.action.findInFiles 参数不支持 ${selectedText}。不过,您可以执行此键绑定,然后只需在末尾键入其余查询即可。

  {
    "key": "ctrl+shift+f",
    "command": "workbench.action.findInFiles",
    "args": {

      "query": "(enum|struct|fn|trait|impl(<.*>)?|type) ",
      "isRegex": true,
      // "replace": "",
      "triggerSearch": true,                          // seems to be the default
      // "filesToInclude": "${relativeFileDirname}",  // no variables in findInFiles
      "preserveCase": true,
      "useExcludeSettingsAndIgnoreFiles": false,
      "isCaseSensitive": true,
      // "matchWholeWord": true,
      // "filesToExclude": ""
    }
  },

以下是使用您的 vscode.commands.registerCommand 调用中所选文本的扩展代码的内容:


let disposable = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {

  // get selected text or open an InputBox here

  const selection = vscode.window.activeTextEditor.selection;
  const selectedText = vscode.window.activeTextEditor.document.getText(selection);

  const searchOptions = {
    query: "(enum|struct|fn|trait|impl(<.*>)?|type) " + selectedText,
    triggerSearch: true,
    isRegex: true
  };

  vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);
});

context.subscriptions.push(disposable);

这将打开一个 InputBox 以从用户那里获取查询词:


let disposable = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {

  vscode.window.showInputBox({ prompt: "Enter a search query" }).then(query => {

    const searchOptions = {
      query: "(enum|struct|fn|trait|impl(<.*>)?|type) " + query,
      triggerSearch: true,
      isRegex: true
    };

    vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);
  })
});

context.subscriptions.push(disposable);

显然,您必须添加一些错误检查。