Intellij limit the scope of search/find to current method

614 views Asked by At

I know by clicking on variable name Intellij highlights references to that local variable inside a method.

On so many occasions I want to find(Ctrl+F) a string(other than variable name) i.e think of

  • string inside log stmt
  • substring of variable name
  • substring of other method name -calls from inside the method you are in

I would like to limit the scope of this search/find inside the method I am working in.

However, Intellij takes me to another method(matching the search string) inside the same class and I loose the location of the method I was working on. I have to do Back(Ctrl+Alt+Left) to back to the method I was in. Doing this 100's of times a day is rather unproductive and I loose focus.

Is there a way/intellij action to limit the scope of your search/find to the method you are in instead of the whole class.

3

There are 3 answers

1
Dmitry On

It is impossible.

But you can click "All" button:

enter image description here

then expand your method in "Find:" tab in bottom left corner(by default).

2
matt helliwell On

I have the same problem. The closest solution I've found is to use ctrl-shift-alt-j (select all occurrences) to highlight all occurrences of the selected text in a nice bold colour. It's then pretty easy to spot other uses of the string within the method.

1
Bas Leijdekkers On

You can use the "find in selection" feature. Unfortunately this is only available for Replace, not for Find. Here's how to use it:

  • Use the Extend Selection action to grow your selection to the entire method
  • Invoke Replace Ctrl+R (this will automatically have the in selection checkbox enabled
  • Type the string you want to find

The locations of that string will be highlighted in the selection and you can navigate between the occurrences in the usual way.