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

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

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

问题

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

例如:

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

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

  1. "contributes": {
  2. "commands": [
  3. {
  4. "command": "testext.hello",
  5. "title": "HELLO"
  6. }
  7. ],
  8. "menus": {
  9. "editor/context": [
  10. {
  11. "when": "editorTextFocus",
  12. "command": "workbench.action.findInFiles",
  13. "args": {
  14. "query": "HANDLE*${selectedText}",
  15. "regexp": true
  16. },
  17. "group": "navigation"
  18. }
  19. ]
  20. }
  21. }

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

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

英文:

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:

  1. "contributes": {
  2. "commands": [
  3. {
  4. "command": "testext.hello",
  5. "title": "HELLO"
  6. }
  7. ],
  8. "menus": {
  9. "editor/context": [
  10. {
  11. "when": "editorTextFocus",
  12. "command": "workbench.action.findInFiles",
  13. "args": {
  14. "query": "HANDLE*${selectedText}",
  15. "regexp": true
  16. },
  17. "group": "navigation"
  18. }
  19. ]
  20. }

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

  1. 使用`keybindings.json`中的内置命令:
  2. 可以添加一个键盘快捷键来搜索带有参数的字符串。
  3. {
  4. "key": "alt+f",
  5. "command": "search.action.openEditor",
  6. "args": {
  7. "query": "${selectedText}",
  8. "isRegexp": true,
  9. "matchWholeWord": true
  10. }
  11. }
  12. 如果要从菜单触发搜索命令,应遵循以下扩展开发流程。
  13. `extension.ts`中使用`executeCommand`调用`openEditor`和参数:
  14. vscode.commands.executeCommand('search.action.openEditor', {
  15. query: "${selectedText}",
  16. matchWholeWord: true
  17. });
  18. P.S. `workbench.action.findInFiles`在使用${selectedText}时存在一些问题。
  19. 我已经报告了这个问题。 [#173653][1]
  20. [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.

  1. {
  2. "key": "alt+f",
  3. "command": "search.action.openEditor",
  4. "args": {
  5. "query":"${selectedText}",
  6. "isRegexp": true,
  7. "matchWholeWord":true
  8. }
  9. }

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

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

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:

确定