【问题标题】:vscode: Extension to do simple global searchvscode:进行简单全局搜索的扩展
【发布时间】:2021-05-10 12:45:18
【问题描述】:

我想创建一个执行非常简单任务的扩展:

我从命令托盘调用命令:

> commandName: Query

它应该在正则表达式模式下启动全局搜索:

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

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

这听起来像是一个非常简单的过程,我希望它可以在 vscode 中实现,但是我在 VSCode API 和相关教程中还没有找到任何关于它的信息。

【问题讨论】:

  • 为什么Search Bar 对你不起作用(Ctrl+Shift+F),还要寻找此栏顶部的Open New Search Editor 按钮
  • 我并不热衷于手动打开搜索栏(即使通过键盘,我已经这样做了),因为输入正则表达式前缀会很麻烦,我想优化我的工作流程:)。我会在问题中说明这一点。
  • 搜索和替换字段有历史记录,使用向上和向下箭头键。您可以让扩展使用所需的字符串填充剪贴板,或者您可以尝试设置使用type 命令键入所需字符串的键绑定,或者让扩展在焦点位于时使用type 命令在搜索查找字段上(键绑定where 子句)
  • 历史记录很酷,但对我的用例来说并不是最佳的。剪贴板要好一步,但仍然增加了一个冗余步骤,您还需要手动调用搜索。 type 命令似乎是最有希望的,但我想知道,您是否也可以强制(以编程方式)将焦点放在搜索查找字段上?然后我可以通过一个命令调用来端到端地完成整个过程。
  • 这些代表什么:SomeToken AnotherToken (some|regex*|prefix?|foo)?如果我们不确切知道您想在哪里输入什么,这很难提供帮助。 SomeToken 是搜索词,AnotherToken 是替换词,(some|regex*|prefix?|foo) 到底是什么?

标签: visual-studio-code vscode-extensions


【解决方案1】:

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

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

    "runInSearchPanel": {
        "removeDigits": {
            "title": "Remove digits from Arturo",
            "find": "^(\\s*Arturo)\\d+",
            "replace": "$1",
            "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"
}

它会打开一个搜索编辑器,而不是使用您的搜索视图/面板。如果您希望它在搜索视图/面板中并不那么容易。不幸的是,workbench.action.findInFiles 参数不支持${selectedText}。不过,您可以执行此键绑定,然后只需在最后键入查询的其余部分。

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

      "query": "(enum|struct|fn|trait|impl(<.*>)?|type) ",
      "isRegex": true,
      // "replace": "$1",
      "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);

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

【讨论】:

  • 抱歉耽搁了@Mark,我忙了几天,并将其添加为书签,以便稍后查看。您的回答非常棒,现在我有一些非常好的选项可以根据我的需要进行调整。谢谢!一旦我尝试了所有方法,我会写进一步的反馈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多