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

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

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

Related

How to sort context menu elements in a TreeView in a VS Code extension?

I build a Git Flow Extension.
In my actions inside view, context menus are sorted alphabetically.
I want them to be sorted as in the order I entered commands in configuration. How can I control order? I can see that the same action menu of git is well organized yet has separators.
Then you have to add them to a group and number the entries
"menus": {
"view/title": [
{
"command": "myExt.cmd1",
"when": "view == myView",
"group": "mygr#1"
},
{
"command": "myExt.cmd2",
"when": "view == myView",
"group": "mygr#2"
},
]
}

How to search in currently opened folder using selected line

I am using vscode. How can I search (workbench.view.search) in currently opened folder using selected line (expandLineSelection) via a keyboard shortcut.
Using an extension I wrote, Find and Transform, and this keybinding:
{
"key": "alt+z", // whatever keybinding you want
"command": "runInSearchPanel",
"args": {
"isRegex": false,
"triggerSearch": true,
"filesToInclude": "${relativeFileDirname}", // many other path variables as well
// all the other search options are available, like 'matchCase',
// 'find', 'replace, etc.
}
}
any selection will be searched for in the current file's parent directory. If you don't specify a find query, the selection will be used as the query.
You could put this together with a macro extension, here using multi-command, like this:
{
"key": "alt+z",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"expandLineSelection",
"cursorLeftSelect", // to get rid of the trailing newline
{
"command": "runInSearchPanel",
"args": {
"isRegex": false,
"triggerSearch": true,
"filesToInclude": "${relativeFileDirname}"
}
}
]
},
"when": "editorTextFocus"
}
[I see that expandLineSelection includes the trailing newline. But it doesn't seem to affect the resuts. In any case, I added "cursorLeftSelect" to the above macro to get rid of that trailing newline.]

How do I disable inserting a new empty line when I press Enter when the cursor is in brackets?

Steps to reproduce:
simple code:
if () {}
cursor is between {}
i press Enter
result:
if () {
}
expected result:
if () {
}
I want a empty line not to be inserted.
It may be that it works by default(adds a empty line), and when Alt+Enter it doesn't add a empty line.
I did not find settings in vscode. I didn't find anything on google.
I tried this:
{
"key": "alt+enter",
"command": "type",
"args": {
"text": "\n"
},
"when": "editorTextFocus"
}
Because Alt+Enter does nothing by default.
However, the onEnterRules function used with the editor.autoIndent option detects the addition of the \n character and adds an extra empty line anyway. :(
I want to use editor.autoIndent. But I want to turn off (do not turn on) using the shortcut Alt+Enter.
Worst option: look for an extension that does exactly the same as editor.autoIndent, but has the ability to create a shortcut Alt+Enter to work the way I want.
You can use the extension multi-command and construct a command that does what you want.
Add this to your settings.json (global or workspace)
"multiCommand.commands": {
"multiCommand.lineBreakNoEmptyline": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
}
}
Add this to your keybindings.json:
{
"key": "alt+enter",
"command": "multiCommand.lineBreakNoEmptyline",
"when": "editorTextFocus"
}
Or using the keybinding only method
{
"key": "alt+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
},
"when": "editorTextFocus"
}
I'll show the info from my comment here so that it is clearer. This keybinding:
{
"key": "alt+enter", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
// "lineBreakInsert",
{
"command": "type",
"args": {
"text": "\n"
}
},
"editor.action.clipboardCutAction"
]
},
}
The type command acts differently than a lineBreakInsert command so it is a little easier to then delete that extra line as the cursor is already there. It is just a small improvement, 2 less commands.
Demo:

How to see the activity menu of a specific extension

I am trying to add a shortcut to show the activity menu of gruntfuggly.todo-tree extension in the activity bar. For the other ones I have something like this:
{
"key": "ctrl+k ctrl+e",
"command": "workbench.view.explorer"
},
{
"key": "ctrl+k ctrl+g",
"command": "workbench.view.scm"
},
{
"key": "ctrl+k ctrl+d",
"command": "workbench.view.debug"
},
{
"key": "ctrl+k ctrl+x",
"command": "workbench.extensions.action.showInstalledExtensions"
},
{
"key": "ctrl+k ctrl+t",
"command": "workbench.view.todo-tree"
},
but I do not know what command use to that specific extension. Any idea? Where can I look up these commands? I would like to do it with ms-azuretools.vscode-docker extension too.
EDIT: I'm referring to this menu:
At the end, I had to open an issue in the vscode repository. It looks like there's a view command for every extension added automatically. You only need to open the Keyboard Shortcuts and type workbench.view.extension. and look for the name of the desired extension.

How to pre-filter showCommands in VSCode's quickOpen panel

I got a few extensions that add 4, 6 or more commands and I don't want to set a shortcut for each of them.
Ideally I could create a shortcut to show all commands of that extension.
Alternatively something that shows the workbench.action.showCommands list with some text set already in there so that just the interesting commands are shown.
Something like this:
{
"key": "ctrl+[Backquote] z",
"command": "workbench.action.showCommands",
"args": { "text": "task marks" },
"when": "editorTextFocus"
}
Any ideas how to do this? I could not find workbench.action.showCommands in VSCode's API documentation.
Try using this (with whatever keybinding you choose) :
{
"key": "alt+z",
"command" : "workbench.action.quickOpen",
"args" : ">task marks",
"when" : "editorTextFocus"
}
I got the syntax from issues: adding an argument to the quickOpen panel.
Oddly, this will not work:
"command": "workbench.action.showCommands",
"args": "task marks",
Only the workbench.action.quickOpen command will take the argument.