Vscode: SearchEditor: Open new editor with args

Created on 18 Apr 2020  路  12Comments  路  Source: microsoft/vscode



I'd like to define a keybinding to open a new search editor with args like workbench.action.findInFiles. The following is what I want to define:

    {
        "key": "ctrl+shift+g",
        "command": "search.action.openNewEditor",
        "args": {
            "query": "${selectedText}",
            "filesToInclude": "${relativeFileDirname}"
        },
        "when": "editorTextFocus"
    }

Context: I work on a project which has a lot of files and directories. "Find in Files" takes a long time so I want to limit search target under a specified directory. #95420 is similar but I want to open search editors for different directories so "search.searchEditor.experimental.reusePriorSearchConfiguration": true doesn't fit my cases.

feature-request search-editor verification-needed verified

Most helpful comment

search.action.openNewEditor and search.action.openNewEditorToSide can now be called with args like:

{
    query: string,
    includes: string,
    excludes: string,
    contextLines: number,
    wholeWord: boolean,
    caseSensitive: boolean,
    regexp: boolean,
    useIgnores: boolean,
    showIncludesExcludes: boolean,
}

All 12 comments

search.action.openNewEditor and search.action.openNewEditorToSide can now be called with args like:

{
    query: string,
    includes: string,
    excludes: string,
    contextLines: number,
    wholeWord: boolean,
    caseSensitive: boolean,
    regexp: boolean,
    useIgnores: boolean,
    showIncludesExcludes: boolean,
}

Note that while "query": "${selectedText}", works, the ${selectedText} variable throws if there's no selected text. By default search editors use selected text anyways, so you can just leave this out to use selected text if it exists and nothing otherwise.

I note that there is no intellisense for the args as of

Version: 1.46.0-insider (user setup)
Commit: 2b472d1e4aaaecdda5ca96289869f90ae82de3a1
Date: 2020-05-11T10:00:12.459Z
Electron: 7.2.4
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.19624

Nor is an error indicated if I omit the args key as in

   {
      "key": "alt+g",
      "command": "search.action.openNewEditor",
      "query": "flatten",
      "regexp": true,
    },

@ArturoDent unfortunately intellisense and advanced schema validation for keybinding arguments is not supported for any commands.

Hmmm, maybe I'm talking about something different things?

keybinding intellisense

oh cool. added

seems like adoption is pretty limited. other commands I tried didnt have any intellisense

If you know of any commands that accept arguments in keybindings.json but don't have IntelliSense then you could create issues for those commands.

@JacksonKearl Thank you for this!

I'm using this to build a "saved search" for all my TODOs, like so:

    {
        "key": "alt+k alt+t",
        "command": "search.action.openNewEditor",
        "args": {
            "query": "(TODO|NOTE|BUG):",
            "regexp": true
        },
        "when": "editorTextFocus"
    }

Now, this launches the search editor with the input box focused and the "query" selected.
After which I have to press enter to actually run the search.
And then, click the search results part to focus that area so I can use Arrow Keys / Page Up/Down to browse the results.

I want two little improvements - perform the search automatically - don't wait for me to press Enter.

And, when you've already run the search, just focus the search results for me - avoiding the need to click that area.

I suppose they could implemented as other optional arguments? performSearch: true and focusResults: true.

@dufferzafar that sounds like a good idea (have you tried out contextLines btw?), but could you open a separate issue? It鈥檚 much easier to manage work items as issues versus issue comments

Yeah, I tried contextLines as well, but I seem to like the initial value of 0 for this particular use case.

Issue opened: #97823

Sorry for the late comment before presumed release, but just today I discovered that you can do something similar with the workbench.action.findInFiles command. Namely:

{
  "key": "ctrl+shift+f",
  "command": "workbench.action.findInFiles",
  "args": {
    "query": "(conc)a.",
    "replace": "boy $1 howdy",
    "triggerSearch": true,
    "filesToInclude": "",
    "isRegex": true 
  }
}

The args for this command are here: https://github.com/microsoft/vscode/blob/9a987a1cd0d3413ffda4ed41268d9f9ee8b7565f/src/vs/workbench/contrib/search/browser/searchActions.ts#L163-L172

Notice that some of the args are different and some the same as those proposed above:

{
    query: string,
    includes: string,
    excludes: string,
    contextLines: number,
    wholeWord: boolean,
    caseSensitive: boolean,
    regexp: boolean,
    useIgnores: boolean,
    showIncludesExcludes: boolean,
}

I know that is in search/browser/searchActions.ts but it works in the standalone version of vscode if search/browser was meant to signify it wouldn't necessarily?

I would think that for consistency purposes they should be same where possible, especially since they both can be used in keybindings.

Also note that the workbench.action.findInFiles version contains a triggerSearch arg which is very nice.

[I am also a little bummed that I just discovered this today thanks to https://stackoverflow.com/questions/62251045/workbench-view-search-vs-workbench-action-findinfiles-commands-in-vscode and that these commands are not reflected in intellisense (I'll open a new issue).]

97823 is about triggering search when running the keybinding.

Was this page helpful?
0 / 5 - 0 ratings