Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intelliJ -> is there any way to scope your search to be within a function

Very often while coding, I need to search for keywords which are common and happen throughout a large file, but I am looking for an instance within a function.

The default search functionality often gives me all results starting from the top. Is there any way/workflow to search only within a specific method/function or block of code?

like image 204
tsar2512 Avatar asked May 31 '18 15:05

tsar2512


1 Answers

Using IntelliJ IDEA 2018.1.4 (Ultimate Edition), Build #IU-181.5087.20 on Ubuntu 18.04, you can do it like this (mentioned key maps are for Linux):

Let's take as example the following code, where we will be searching for the word "key" inside the method "clear". As you can see there are many occurrences of that word (in red, highlighted the ones we want in our results, and in blue other occurrences, which we do not want to appear in our search results)

Image of IntelliJ IDEA Editor with the searched word

1- Select the area you want to limit the search scope to.

Image of IntelliJ IDEA Editor with the searched word scoped inside some area

2- Press Ctrl+Shift+F. It will pop up a search window with the selected text pre-filled in the search box.

3- Select the options "Scope" and, in the combo next to it, "Selection" (if they are not selected by default)

Image of the pop up shown by IntelliJ IDEA for searching the word in the selected scope inside the current editor

4- Type the word(s) you want to search (in this example, the key word).

enter image description here

5- The results shown will be limited to the selected area. Notice how there are only 9 matches in the upper right corner ("Match case" option is checked), despite in the same file there are many other ocurrences of the same word.


However I was neither able to find a key combination in order to do this as a single action nor assign a new one. Also, notice how the search includes the text in the method documentation.

like image 176
lealceldeiro Avatar answered Sep 19 '22 21:09

lealceldeiro