Visual Studio Code - Include context in search results - visual-studio-code

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?

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.
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.
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.
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:
------------------------------- 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

You can single click the results which will open the relevant code in a "preview" editor. With the preview you can navigate the results list (clicking, ↑/↓, ctrl+n/ctrl+p) without opening new editors.
But it sounds like you want to avoid the preview altogether. In that case, here's a feature request, but it looks like it was closed prematurely and needs to be submitted again. The only solution that was actually implemented was a setting for placing the search results in the panel, rather than the sidebar: "search.location": "panel".

Related

VSCode will copy full line when only a word is selected (single click)

I'm on OSX and running Version 1.42.0 of Visual Studio Code. I have noticed that when I single click a word it will highlight. But if I copy CMD + c and then paste CMD+v, the full line will be in the clipboard. This causes problems from time to time, when the screen has given me every indication that I have only selected a single word. Is there some setting that I can set that will make the default behavior to select a word on a single quote and never ghost select a full line?
What it looks like when I single click a word:
And what it looks like after I've copied and pasted:
After filing an issue, it turns out that this behavior is by design.
The word the cursor is on (from a single click) is highlighted along with every occurrence of that word. The word is not selected (that would be a deeper blue).
By default copying without a selection copies the current line.
This in my opinion is an Accessibility issue, as there are strong visual clues that a word is selected. I have found that the behavior can be made more intuitive if you set these to settings.
// Controls whether copying without a selection copies the current line.
"editor.emptySelectionClipboard": false,
// Controls whether the editor should highlight semantic symbol occurrences.
"editor.occurrencesHighlight": false
With these two settings the word the cursor is on will not be highlighted (nor any other occurrences of that word). And if you do happen to copy, with an empty selection, the editor will behave similar to how other applications behave and not copy the current line.

In VS Code file searching, can I expand (or collapse) all results?

In the Search pane of the program, after I hit Enter, all files are listed, with some expanded to show results in a file, and others collapsed. I'm wondering firstly what determines the expansion of any given file, and secondly what I can do to expand all of them at once.
This question seems closest to mine, but it's about a different IDE, and the key commands it suggests for Windows had no apparent effect: Automatically expand all in Eclipse Search results
See this setting:
Search: Collapse Results in the Settings UI or
search.collapseResults: alwaysExpand in your settings.json file
The options are auto,alwaysCollapse, and alwaysExpand. auto is the default.
auto: Files with less than 10 results are expanded. Others are collapsed.
So you want the alwaysExpand option.
You can also toggle any file expanded/collapsed with the Space key or just expand any collapsed file with RightArrow.
Collapse with LeftArrow and collapse all with Ctrl+LeftArrow. Oddly, there is no expandAll binding or command.
And see https://stackoverflow.com/a/67307225/836330 for a command to collapse all the results that you can set to a keybinding:
workbench.files.action.collapseExplorerFolders as in
{
"key": "alt+l", // whatever you want
"command": "search.action.collapseSearchResults",
"when": "searchViewletFocus" // if you want to limit it when focus is already on the search results area
}
in your keybindings.json.
v1.41 is making expanded search results the default, see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_41.md#search
Expand all search results by default
Previously, if a full text search returned more than 10 results in a
single file, it would appear collaped in the results tree. However,
this sometimes made finding a particular result difficult, so with
this release all results will appear expanded. You can set
search.collapseResults to auto to revert to the old behaviour.
And see Visual Studio Code - Include context in search results for showing the search results in an editor.
There are two quick ways to expand all at any time:
Click the icon in the top right of the search panel that has a "+" inside a square. This toggles all items expanded/collapsed.
Use the command palette (Cmd-Shift-P) to trigger the "Search: Expand All" command. To make it quicker to access, you can add a custom keyboard shortcut: when looking at the "Expand All" search result in the command palette, click the gear icon next to it and it will take you to an editor for adding a shortcut binding.
Both of these only work for the Search panel though – despite the similarity, they do not work for the "Find All References" results panel.

How can I get Eclipse to auto-indent code blocks within if and for statements?

I tried to get Eclipse to convert all of the tabs in my project to spaces like this:
Java Editor:
Click Window » Preferences
Expand Java » Code Style
Click Formatter
Click the Edit button
Click the Indentation tab
Under General Settings, set Tab policy to: Spaces only
Click OK ad nauseum to apply the changes.
And now my code is formatted without any indentations within if and for blocks, like this:
private void addAppointment(Resource resource) {
if (resource != null) {
Appt appt1 = new Appt();
appt1.setTime(new Date());
resource.setAppointment(appt1);
}
}
I really don't want to have to manually fix this in the hundreds of files in the project, how can I format to indent within if and for blocks in the whole project?
I should also say that the "Statements within blocks" checkbox in the active Formatter profile is checked. The preview it shows has a for block with an indented body, so I have no idea why that isn't being applied to my project.
#gnac provides some good options, in addition to:
Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly
You can select the project(s) and do Source menu -> Format to format everything in that project in one go. (No keyboard shortcut for it AFAIK.)
So once you set your formatting options you have a couple of options. You can set the preferences to format your files when saving.
Preferences->Java->Editor->Save Actions
However, if you have a lot of files this will be a pain as well. Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly, again having to do it on each file individually.
Inside Eclipse you can use Search->Find, enter "\t" in the text box and select the "Regular Expression" check box and then click the "Replace..." button. When the search is done, it will ask you what to replace it with. Enter 4 spaces into the "With" text field. Click Preview to see what it will do, or OK to make the changes.
I would use a find and sed to find all of the java files in a directory and replace the tabs, although this is outside of eclipse
find -iname ".java" -exec sed -i.orig 's/\t/ /g' {} +
If you're not on Linux you could use cygwin to do the same on Windows.

How to filter files shown in Visual Studio Code?

How would you filter the files shown in the folder view in Visual Studio Code?
Reference: Filter files shown in folder
Hiding files and folders
The files.exclude setting lets you define patterns to hide files and folders from several places in VS Code like the explorer and search. Once defined, files and folders matching any of the patterns will be hidden.
{
"files.exclude": {
"**/*.js": true
}
}
Hide derived resources
If you use a language that compiles to another file at the same location of the source file, like TypeScript does to JavaScript, you can easily set an expression to hide those derived files:
"**/*.js": { "when": "$(basename).ts"}
Such a pattern will match on any JavaScript file (**/*.js), but only if a sibling file with the same name and extension, *.ts in this example, is present. The same technique can be used for other transpiled languages, like Coffee Script or Less/Sass, too.
Source: VS Code v0.5.0 (July 2015)
In version after VScode 1.70 (July 2022) all you need to do is press Ctrl+F or F3 to search.
Please refer following post
Searching in Explorer panel after VSCode 1.70
Only applicable for v1.69 and below.
Step #1
Click on Explorer window. This is critical as without focus on Explorer it will not work.
Step #2
Start Typing name you want to filter. It's weird that there is no textbox to take input but... take a leap of faith and type. You will see your typed text in top-right corner in brown background. Now hover on that text.
Step #3
Click on 3 stacked lines to filter..
They look like hamburger menu but they are not. They are saying if it's filtered or not. They are toggled between filtered and just highlight. So, make sure they are like inverted pyramid.
That's it. It should be filtered now.
If you only want to change the setting for this project, then do the following:
File > Save Workspace As > ... enter your {project name}
Then open file: {project name}.code-workspace
And update section settings.
Sample:
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.exclude": {
"**/*.log": true
}
}
}
VScode 1.70 (July 2022) should improve on this "tree filter" feature.
(Available today in Code insiders)
See issue 70646 and PR 152481:
Support find widget in lists/trees
This PR replaces the existing list/tree type filter:
with an bona fide find widget:
While a seemingly simple change, this has some (desired) consequences when searching/filtering in trees. Namely:
We will restore simple keyboard navigation by default.
That is: pressing the letter A will focus the next element which starts with A.
Initiating a search operation requires pressing Ctrl-F or F3, like the editor.
While searching, focus is in the find input box as opposed to the list itself.
Pressing DownArrow will focus the first list element which was found.
We'll preserve all custom behavior of context keys, eg. used by the VIM extension).
In VIM, the pre-existing / command will trigger simple keyboard navigation, as opposed to opening the find widget.
The VIM extension has the option to change this behavior themselves.
And:
In general:
Keyboard navigation is now called type navigation
Filter on type is now called find mode, aligned with a new find concept
Settings
workbench.list.keyboardNavigation has been renamed to workbench.list.defaultFindMode
workbench.list.automaticKeyboardNavigation has been deleted
Commands
list.toggleKeyboardNavigation has been renamed to list.triggerTypeNavigation
list.find has been added
list.closeFind has been added
list.toggleFilterOnType has been renamed to list.toggleFindMode
Context Keys
Mainly used by the vim extension:
listSupportsKeyboardNavigation has been renamed to listSupportsTypeNavigation
listAutomaticKeyboardNavigation has been renamed to listTypeNavigationMode
"With the focus on the File Explorer start to type part of the file name you want to match.You will see a filter box in the top-right of the File Explorer showing what you have typed so far and matching file names will be highlighted."
"Hovering over the filter box and selecting Enable Filter on Type will show only matching files/folders."
documentation: https://code.visualstudio.com/docs/getstarted/userinterface#_filtering-the-document-tree

How to ignore Eclipse task tags in certain directories?

In Eclipse (HELIOS) there is an option to scan all source code and search for task tags such as "TODO", "FIXME" etc. The result is then shown in a fine list.
One can access this list by: Windows->Show View->Tasks.
However, it also scans resources directory and libraries, whose task tags are not of my interest. How can I filter Task Tags searching by directory exclusion filter?
10x
It is possible.
Open the Tasks view. Then press icon with the down arrow (top right corner of the window, next to minimise button) and press "Configure Contents..."
Either add new configuration or modify TODOs
In the Scope section select "On working set:" and press button "Select..." to create a new workspace
Create a new workspace with only selected folders that you want to include
Also there is the way I work around that limitation: Define your own tags, i.e. by adding your name or some shortcut and use the task-list's filter function to ignore the rest.
In the preferences you can define your own TODO Tags (for highlighting purposes etc.) or you can leave it with TODO, FIXME, XXX..
Also in the preferences you can redefine the default Comments like "TODO: Auto-generated something" to "TODO Nir: Auto..."
In the Task List you can then filter for exactly those tags.
Cheers,
Rob
Unfortunately, the answer is that this is not currently possible. You can configure the set of task keywords on a per-project basis along with the priority of each, but no more than that.