JetBrains' IntelliJ is one of the best IDE's out there. With all those features packed into one package, is quite easy to not be aware of all of them. In this blogposting I will demonstrate some of IntelliJ's best hidden features, which will make developing with this IDE even a bigger pleasure!
The first feature I'm going to describe, is called 'Show Usages'. If you're like me, and like working with the keyboard instead of the mouse, then this is a great way of navigating through your code. Just put the caret on the Java element (variable, method, class, etc), and press Ctrl+Alt+F7. When one usage is found, IntelliJ will automatically jump to the usage. When multiple usages are found, IntelliJ will show a list of found references (see picture), and allow you to use the navigation keys to jump to the found references. Note that IntelliJ also found the reference in the Groovy file, though that might be a subject for a different blog posting.
Illegal Package Dependencies
The Illegal Package Dependencies setting is an Intention. These can be found by going to the settings screen (Ctrl+Alt+S), and clicking on Errors (6). The Errors configuration can be used to configure Inspection, so why it is called 'Errors' is unknown to me, and makes it a little harder to find the dialog. (You can also find it by double clicking on the 'Man Icon ' in the right side of the screen, which might be even harder to find!) When using the search in this dialog, and search for 'Illegal package dependencies', you will find an Inspection which will allow you to control the creation of new classes by specifying scope based rules, like shown in the picture. For a completion explanation of how this works, I'd suggestion checking out the screencast by JetBrains ('Using Depency Rules'), which can be found here.
If you have a wide screen, and you don't like unused space in your IDE, then 'Preview Usages' is a nice hidden gem in IntelliJ. It will allow you to preview the result of a find, directly in the search results. Since images tell more than a thousand words, I suggest checkout out the pictures below to see the difference.
|Without Preview Usages||With Preview Usages|
Smart code completion
Okay, this is not really a hidden feature, but it's something worth mentioning. Smart code completion is an extra addition to code completion, and it's called smart code completion because it takes the return type of the method into account. An example:
String name = person.*caret here*
When pressing Ctrl+Space at the '*caret here*' position, all the methods of the person instance will come up, including methods like 'equals', 'hashCode', and all other methods of the person instance. When pressing Ctrl+Shift+Space, the list will be filtered to match only the methods which have compatible return types, in this case String.
This feature is quite handy when there are multiple method calls on one line. Take the following line for example:
new StringBuffer().append(s.indexOf(0, s.length() *caret here*
Now, when I'm at the '*caret here*' position, how many closing braces do you need to complete the line? Well, Ctrl+Shift+Enter will complete it for you, and will add the '));' for you. It will also do the same for method definitions; it will place the curly braces at the right place. This feature might sound like a minor feature, but I've used it a lot, and it's a real time saver!
This is also quite hidden (and thus in line with my article): when an Inspection is configured to report about the code in your IDE (for example, hardcoded String likes in the example below), you can fix this one by one by pressing Alt+Enter, followed by Enter. You can also fix all of the same violated Inspections at the same time by pressing Alt+Enter, then the Right Arrow, and there's the 'Fix all' option, which, in this case, will create a new properties file, place the contents of the strings as key-value pairs in the file, and replace the strings in the code by "resourceBundle.getString("name");", or by your own defined template.
That's it for today. There are more hidden and not so hidden features in IntelliJ, but these will have to wait for a future posting. I hope this blog has provided some insights in the things IntelliJ has to offer, and that it provided some helpful insights!