Saturday, September 20, 2008

Custom IntelliJ key-bindings and tips (part 2) - Navigation and searching

Navigation shortcuts

I had some doubts whether to write about these shortcuts since they are really basic. But I felt it worth doing anyway because in my view these are the most valuable ones in terms productivity and I've seen experienced java-developers who had used IntelliJ for at least a couple of years and weren't aware of navigating capabilities or weren't using them fully. The idea is that you can navigate to a class or file by typing its name or part of it rather than by browsing "project view" and clicking on a class or file. It significantly speeds up and simplifies navigation because you don't need to concentrate on where class is located and can use your brains for something more useful. In a sense it's like another mindset when don't go to the place to get something but rather search for it (the same idea can be applied to everyday work with files as well, especially with tools like Quicksilver). The following are default IntelliJ navigating shortcuts:

Ctrl+N - go to a class. Note that you can use camel humps in class name. So instead of using “*” or typing full class name, it's possible to do things like this:

Obviously it works well only if class has name which is long and unique enough to identify the class (as you can see in the above picture this is not the case for NPE). If capital letters from class name are not unique, you can add more letters:

Ctrl+Shift+N – go to a file. You can also use camel humps with it.

Ctrl+Shift+Alt+N – go to a symbol. I don't use it often but it is invaluable in cases when you want to navigate to some place but the only thing that pops up in your mind is a variable, constant or method name. You can use camel humps with it as well.

Although navigating to a class through "project view" is something I don't usually do, I find it rather useful to do the reverse operation of locating in the "project view" file which is currently open in editor. The default shortcut goes like this: Alt+`, 1. After the first key-stroke you get a pop-up window:

There you can choose the place to locate currently edited file. (Alternative way is to use "autoscroll from source" option in "project view".) Besides locating file in "project view" I found it handy to locate file in the current change list. The shortcut is Alt+`, 4. It's a bit harder to type but is still much faster than doing it manually.

Searching shortcuts

The next on my list of usefulness are searching shortcuts. I use them mostly in browsing mode when trying to make sense of existing code or making sure I'm not going to break anything by adding new code.

The basic searching shortcut I use is the action called “highlight usages in file”. It works best of all with private fields, method parameters, local variables and the like, since they can only have usages in one file and therefore all usages will be highlighted. One of the benefits of highlighting is that it's easier to see how a variable used in a piece of code, so it's basically an aid for reading. Another use case of “highlight usages in file” is actual searching.

I found default shortcut (Ctrl+Shift+F7) too inconvenient to use since it's involves two hands, so I use left-handed easier to type Alt+A. In order to search I position cursor on a symbol, press Alt+A, and then press F3 several times to go through its usages. (Note in the picture below that IntelliJ is clever enough to highlight all exit points from the function.)

There is also similar textual search with highlighting. It works as described above except that it does only textual comparison. I didn't find it particularly useful when working with source code but it's invaluable for highlighting in text documents (apart from shortcuts, this is the main reason I prefer IntelliJ for editing plain-text documents). It's possible to use text search instead of AST-based search by selecting a part of code and pressing Alt+A.

There are two ways to cancel highlighting:

  • press Esc twice, which clears all the highlighting in the current file;
  • position cursor on a symbol and press Alt+A, which will clear highlighted usages of this symbol (works only for AST-based search).

The major downside of the above search is that it doesn't search outside the current file. Therefore, it's not possible to use this kind of search to make sure that, for example, protected field is not being set somewhere in a subclass. In this case action “show usages” comes to rescue. (IIRC “show usages” has been added in IDEA 7, so you won't find it in earlier versions of IntelliJ.) As with "highlight usages" I also found default shortcut too inconvenient to use, not to mention the fact that Ctrl+Alt+F7 is not very linux-friendly combination. So I use Alt+S, where “S”, I believe, stands for “search”. The way I use it is the same as with highlighting: with mouse I position cursor to some symbol and press Alt+S. After the window with usages appears, I use mouse to navigate to the one I'm interested in.

The advantage of “show usage” is that it works pretty fast (by that I mean that it seems to be much faster than "find usages" action) and it makes it very easy to navigate to usages outside current file. However, when amount of usages is more than 10, it becomes hard to analyze which usage you want to look at. “Find usages” action is the best choice in this case. I won't get into details but it's a very powerful action. Once again I don't use default shortcut but rather use Alt+Shift+S, which is left-handed and easier to reach.

Note that “show usages” and "find usages" work for searching package usages as well. To do that position cursor on a package name in "import" or "package" statement and use one of the above searches. The usecase may be to quickly find all the classes that import and use some library (given that the library has distinct package).

I cannot say for sure but it seems likely that default shortcut for “Find usages” has come from good old days of DOS and was originally “Find file” shortcut in Norton Commander or alike. So using Alt+F7 to search something has always been a very sensible combination which I didn't have to remember. It's also likely that shortcuts for other search actions (Ctrl+Shit+F7, Ctrl+Alt+F7) were based on this one, which was probably quite a good decision in terms of being easy to keep it in mind.

0 comments: