Visual Studio 代码 - 在搜索结果中包含上下文

Visual Studio Code - Include context in search results

有没有办法在 Visual Studio 代码中显示搜索结果的上下文?

默认情况下,如果我搜索 "debug",我可能会返回 2 行代码。

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

我想看看代码的用途,比如说,每个匹配项上下 3 行。

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

  def debug(str)
    puts str.inspect
  end

  def somefuncb

是否可以将这样的上下文添加到搜索结果中?

您可以单击结果,这将在 "preview" 编辑器中打开相关代码。通过预览,您可以浏览结果列表(单击 /ctrl+n/ ctrl+p) 不打开新编辑器。

但听起来您想完全避免预览。那样的话,here's a feature request,不过貌似过早关闭了,需要重新提交。实际实施的唯一解决方案是将搜索结果放置在面板中的设置,而不是侧边栏:"search.location": "panel".

v1.41 正在添加一项功能的预览,该功能将在编辑器中显示搜索结果,从而允许在实际搜索结果周围添加一些上下文行。参见 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.


v1.42 正在添加更多功能,请参阅 https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#search-editor。就像选择搜索结果周围的上下文宽度和 运行 在搜索编辑器本身中进行另一个搜索一样。


顺便说一句,您可以直接打开搜索编辑器,而无需先使用当前未绑定(并在 v1.48 中重命名)的命令 New Search Editor (search.action.openNewEditor) 在面板中进行搜索。该命令将始终打开一个新的搜索编辑器。

如果相反,您希望重新使用搜索编辑器(而不是打开一个新编辑器),将向 v1.48 添加一个命令:

Open Search Editor : search.action.openEditor // 默认也未绑定


v1.43 发行说明 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.

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:

----------------------------- 请参阅下面的编辑:

上下文行的显示在搜索编辑器的使用之间似乎并不持久。但是 Alt+L 充当 切换 到 show/hide 上下文。为上下文行数选择的值是持久的。

但是,在 v1.44 和 Insiders' Build 中有两个新命令 increasing/decreasing 每个搜索结果周围的上下文行数:

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

默认情况下它们是未绑定的 - 这些只是示例键绑定。上下文行输入框不需要对这些工作可见。所以 Alt+L 启用上下文行或这些新命令来更改数字。


在 v1.46 中有一个新设置可以使显示的上下文行的数量保持不变:

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

search.searchEditor.reusePriorSearchConfiguration - 在创建新的搜索编辑器时重复使用最后一个活动搜索编辑器的配置

(defaultNumberOfContextLines 似乎优先于 reusePriorSearchConfiguration)

v1.46 release notes: Search Editor improvements