Add a menu context to search specific strings. (如何使用 workbench.action.findInFiles?)

huangapple go评论46阅读模式
英文:

Add a menu context to search specific strings.(How to use workbench.action.findInFiles?)

问题

我想要为搜索特定查询字符串添加一个上下文菜单选项。
这个查询字符串由常量字符串(带有正则表达式)和选择的字符串连接而成。

例如:

字符串 = "HANDLE*"
当我在编辑器中选择"EVENT"并右键单击菜单,然后点击选项。
它将跳转到搜索视图并自动执行搜索"HANDLE*EVENT"。

根据#99575
这是我的package.json中的代码:

"contributes": {
    "commands": [
        {
            "command": "testext.hello",
            "title": "HELLO"
        }
    ],
    "menus": {
        "editor/context": [
            {
                "when": "editorTextFocus",
                "command": "workbench.action.findInFiles",
                "args": {
                    "query": "HANDLE*${selectedText}",
                    "regexp": true
                },
                "group": "navigation"
            }
        ]
    }
}

该选项已成功添加到菜单中,单击它将跳转到搜索视图。
但是查询字符串没有传递到搜索视图。

我想知道如何正确传递查询字符串?谢谢。

英文:

I want to add a menu context option to search specific query strings.
This query string is concatenated by constant string (with regex) and selected string.

For example :

The string = "HANDLE*"
When I select "EVENT" in editor and right click menu and click option.
It will jump to search viewlet and perform searching "HANDLE*EVENT" automatically.

According #99575.
Here is my code in package.json:

"contributes": {
    "commands": [
      {
        "command": "testext.hello",
        "title": "HELLO"
      }
    ],
    "menus": {
      "editor/context": [
        {
          "when": "editorTextFocus",
          "command": "workbench.action.findInFiles",
          "args": {
            "query": "HANDLE*${selectedText}",
            "regexp": true
          },
          "group": "navigation"
        }
      ]
    }

The option has add to menu succesfully and click it will jump to search viewlet.
But query string has no pass to search viewlet.

I want to know How to pass the query string correctly?Thanks

答案1

得分: 0

使用`keybindings.json`中的内置命令:
可以添加一个键盘快捷键来搜索带有参数的字符串。

{
    "key": "alt+f",
    "command": "search.action.openEditor",
    "args": {
        "query": "${selectedText}",
        "isRegexp": true,
        "matchWholeWord": true
    }
}

如果要从菜单触发搜索命令,应遵循以下扩展开发流程。
在`extension.ts`中使用`executeCommand`调用`openEditor`和参数:

vscode.commands.executeCommand('search.action.openEditor', {
    query: "${selectedText}",
    matchWholeWord: true
});

P.S. `workbench.action.findInFiles`在使用${selectedText}时存在一些问题。
我已经报告了这个问题。 [#173653][1]

[1]: https://github.com/microsoft/vscode/issues/173653
英文:

Use build-in commands in keybindings.json:
It can add a key shortcut to search string with args.

    {
        "key": "alt+f",
        "command": "search.action.openEditor",
        "args": {
            "query":"${selectedText}",
            "isRegexp": true,
            "matchWholeWord":true
        }
    }

If want to trigger search command from menu should be following extension dev flow.
Call executeCommand with openEditor and args in extension.ts:

vscode.commands.executeCommand('search.action.openEditor',{
			query:"${selectedText}",
			matchWholeWord:true
		});

P.S. workbench.action.findInFiles has some problem with ${selectedText}.
I have reported it. #173653

huangapple
  • 本文由 发表于 2023年2月6日 17:03:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定