vscode:进行简单全局搜索的扩展 [英] vscode: Extension to do simple global search

查看:242
本文介绍了vscode:进行简单全局搜索的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我从命令托盘调用命令:

<代码>>命令名称:查询

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

SomeToken AnotherToken (some|regex*|prefix?|foo) 查询

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

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

解决方案

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

 findInCurrentFile":{upcaseSwap2":{title":swap iif <==>你好",找到":(iif)(你好)","replace": "_\\u$2_ _\\U$1_",//双转义大小写修饰符restrictFind":选择"},upcaseSelectedKeywords":{标题":大写选择的关键字",查找":(epsilon|alpha|beta)",替换":\\U$1",//"restrictFind": "selections";}},runInSearchPanel":{删除数字":{标题":从阿图罗中删除数字",查找":^(\\s*Arturo)\\d+",替换":$1",isRegex":真,triggerSearch":真,//"filesToInclude": "${file}";filesToInclude":${file}",}},


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

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

<代码>{"key": "ctrl+shift+g",//任何你想要的键绑定命令":search.action.openNewEditor",参数":{查询":(enum|struct|fn|trait|impl(<.*>)?|type) ${selectedText}",isRegexp":真,//"includes": "${relativeFile}",//有效showIncludesExcludes":真,triggerSearch":真,上下文线":2,focusResults":真,},何时":editorTextFocus"}

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

<代码> {键":ctrl+shift+f",命令":workbench.action.findInFiles",参数":{查询":(enum|struct|fn|trait|impl(<.*>)?|type)",isRegex":真,//替换":$1",triggerSearch": true,//似乎是默认值//"filesToInclude": "${relativeFileDirname}",//findInFiles 中没有变量preserveCase":真,useExcludeSettingsAndIgnoreFiles":false,isCaseSensitive":真,//matchWholeWord":真,//"filesToExclude": "";}},


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

让一次性 = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {//获取选中的文本或在此处打开一个输入框const selection = vscode.window.activeTextEditor.selection;const selectedText = vscode.window.activeTextEditor.document.getText(selection);const 搜索选项 = {查询:"(enum|struct|fn|trait|impl(<.*>)?|type)"+ 选定的文本,触发搜索:真,isRegex: 真};vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);});context.subscriptions.push(disposable);


这会打开一个 InputBox 来获取用户的查询词:

让一次性 = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {vscode.window.showInputBox({ prompt: "Enter a search query" }).then(query => {const 搜索选项 = {查询:"(enum|struct|fn|trait|impl(<.*>)?|type)"+ 查询,触发搜索:真,isRegex: 真};vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);})});context.subscriptions.push(disposable);

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

I want to create an extension that does a very simple task:

I invoke a command from the command pallet with:

> commandName: Query

And it should start a global search, in regex mode with:

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

Where the point is that I don't want to type the SomeToken AnotherToken (some|regex*|prefix?|foo) prefix all the time.

This sounds like a very simple process and I expected it to be possible in vscode, but I haven't found anything yet about it in the VSCode API and relevant tutorials.

解决方案

I have released an extension which does allow you to create and save pre-defined find/replaces or search/replaces: Find and Transform and call them via commands in the Command Palette or through keybindings. Sample settings:

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


  • I don't think there is any way to invoke a command from the Command Palette with an argument as you seem to indicate you wish to do. You could open an InputBox and ask for that value (that is quite easy to do) or use the currently selected text. Below I showed both ways.

First with the currently selected text you can just try a simple keybinding and see if that is sufficient:

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

It opens up a search editor rather than using your Search view/panel. If you want it in the Search view/panel that isn't as easy. Unfortunately the workbench.action.findInFiles args do not support ${selectedText}. Nevertheless you can do this keybinding and simply type the rest of your query at the end.

  {
    "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": ""
    }
  },


Here is the guts of extension code using the selected text that would be in your vscode.commands.registerCommand call:


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);


And this opens an InputBox to get the query term from the user:


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);

Obviously you'll have to add some error checking.

这篇关于vscode:进行简单全局搜索的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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