microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.61k stars 29.04k forks source link

Secondary search #118128

Open meganrogge opened 3 years ago

meganrogge commented 3 years ago

I'd like to be able to search for something, for example ipad and then in the files which contain that term, search for something else, for example terminal.

IE find files which contain && .

roblourens commented 3 years ago

Do you want to search in the line with the match, and the surrounding lines, or anywhere in the file? If the first, try Search Editors.

meganrogge commented 3 years ago

Anywhere in the file

meganrogge commented 3 years ago

It would've saved me so much time to have this capability on 3 unique occasions today. Talked w @kieferrm and I hope to work on a PR for ripgrep in the coming iterations so that we have a:

SearchTerm1 && SearchTerm2 option

roblourens commented 3 years ago

Could also be a good extension candidate using the (proposed) findTextInFiles API.

For your original case, it sounds like what I often use "files to include" for, when I want to search for the use of a term like ipad in some feature area like notebooks, I would use notebook or *Notebook* (note that it's case sensitive)

ArturoDent commented 3 years ago

I made an extension which applies a workaround by copying the current search results, regex-ing for the filenames, getting their relative paths and then putting those paths into the 'files to include` filter. Find and Transform.

Specifically Using the Search Results files in another search.

Use from the context menu, Command Palette or a keybinding like:


{
  "key": "alt+shift+f", 
  "command": "runInSearchPanel",   
  "args": {
    "find": "Second",                            // plus all the other args from the findInFiles command 
    "replace": "Third",
    "filesToInclude": "${resultsFiles}",   // !!
    "triggerSearch": true                
  },
    "when": "hasSearchResult"      // I suggest this but it isn't mandatory
}

There are other ways to use the ${resultsFiles} variable as well. Such as in the find-and-transform.searchInResults command where it is automatically applied.

Using this keybinding:

  {
    "key": "alt+z",
    "command": "runInSearchPanel",
    "args": {
      "filesToInclude": "${resultsFiles}",
      "triggerSearch": true,
    }
  },

Demo:

searchWithoutSelection2

[Since the context menu item for this really belongs in the Search results context menu but that is not currently exposed to extensions, I am going to file an issue requesting it. See https://github.com/microsoft/vscode/issues/127896]