Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - Include context in search results

Is there a way to show context for search results in Visual Studio Code?

By default, if I search "debug" for example I might get 2 lines of code returned.

filea.rb
  def debug(str)
fileb.js
  function debug(str) {

I want to see what the code is for, say, 3 lines above and below each match.

filea.rb
  def somefunca
    puts "some func a"
  end

  def debug(str)
    puts str.inspect
  end

  def somefuncb

Is it possible to add context like this to the search results?

like image 434
steel Avatar asked Jul 30 '18 17:07

steel


1 Answers

v1.41 is adding a preview of a feature which will display search results in an editor thus allowing for some context lines around the actual search result. See search.enableSearchEditorPreview

Preview: Search Editor

In this milestone, we've started work on showing the results of a search in an editor. This provides much more space to view search results and allows users to maintain multiple collections of search results simultaneously. With this release, in a search editor you can:

  • Navigate to results using Go to Definition-family commands, including Peek Definition and Open Definition to Side.

  • Rerun a search to update the list of results

  • View lines of context surrounding a result

  • Persist results to disk to be referenced later or even tracked in SCM

We will be continuing to add functionality and increase usability in the coming releases.

Note: You can preview this feature by enabling the setting search.enableSearchEditorPreviewstrong text.

preview search in an editor demo


v1.42 is adding a bit more functionality, see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#search-editor. Like selecting the context width around the search result and running another search right in the search editor itself.

search context demo


By the way, you can directly open a search editor without first doing a search in the panel with the command New Search Editor (search.action.openNewEditor) currently unbound (and renamed in v1.48). That command will always open a new Search Editor.

If instead, you wish to re-use a Search Editor (rather than opening a new one), a command is being added to v1.48:

Open Search Editor : search.action.openEditor // also unbound by default


v1.43 release notes https://code.visualstudio.com/updates/v1_43#_search-editors

In a search editor, results can be navigated to using "Go to Definition" actions, such as kb(editor.action.revealDefinition) to open the source location in the current editor group, or kb(editor.action.revealDefinitionAside) to open the location in an editor to the side. Additionally, double clicking can optionally open the source location, configurable with the search.searchEditor.doubleClickBehaviour setting.

search editor image

You can open a new search editor with the Search Editor: Open New Search Editor command, or using the "Open New Search Editor" button at the top of the search viewlet. Alternatively, you can copy your existing results from a search viewlet search over to a search editor with the "Open in Editor" link added to the top of the results tree, or the Search Editor: Open Reuslts in Editor command.

Note You can try out the experimental Search Editor: Apply Changes extension to synchronize edits you make in a search editor back to source files:

search and apply changes

------------------------------- see edit below:

Showing the context lines does not appear to be persistent between uses of the search editor. But Alt+L acts as a toggle to show/hide the context. The value chosen for the number of context lines is persistent.

However, in v1.44 and the Insiders' Build are two new commands fro increasing/decreasing the number of context lines surrounding each search result:

{
  "key": "alt+-",
  "command": "decreaseSearchEditorContextLines",
  "when": "inSearchEditor"
},
{
  "key": "alt+=",
  "command": "increaseSearchEditorContextLines",
  "when": "inSearchEditor"
}

They are unbound by default - these are just sample keybindings. The context lines input box does not need to be visible for these to work. So Alt+L to enable context lines or these new commands to change the number.


In v1.46 there is a new setting to make the amount of context lines shown persistent:

 "search.searchEditor.defaultNumberOfContextLines": 4,  // default is now 1

and

search.searchEditor.reusePriorSearchConfiguration - Reuse the last active search editor's configuration when creating a new search editor

(defaultNumberOfContextLines seems to take precedence over reusePriorSearchConfiguration)

See v1.46 release notes: Search Editor improvements

like image 58
Mark Avatar answered Oct 30 '22 22:10

Mark