Anton Arhipov is a Software Developer and Product Lead for JRebel, the productivity tool for Java developers. Anton’s professional interests include Java and related, programming languages and middleware. You can follow Anton on Twitter (@antonarhipov) and find him on LinkedIn (linkd.in/aarhipov). Anton has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

What’s Cool In IntelliJIDEA. Part I

07.05.2011
| 10478 views |
  • submit to reddit

Eclipse or IntelliJ? NetBeans or Eclipse? IntelliJ or NetBeans? The dispute about the IDEs is the most popular among the software developers and hardly will ever end. I consider myself being a big IntelliJ fan, but I do realize that there are a lot of things that IntelliJ could do better and that NetBeans and Eclipse are better in some ways. In this post I’d like to make an overview of IntelliJ’s features which I like the most, and and beyond that. Also I’d like point out some of the aspects in which IntelliJ could do better.

One of my friends said once:

I don't like IDEs. They help to write verbose code. In real life 90% of time I spend on code reading. Verbose code kills my productivity

I think he's wrong. The reality is that IDEs are used not only for writing/generating code, but to a greater extent IDEs are used to read and analyze the code. You might be a hard-core Emacs/Vim ninja but still, there are things that you just cannot do with the text editors.

Navigation

Navigation through the source code is one of the activities that programmer executes on a regular basis. Basically, it is not hard to navigate the code with Emacs or Vim, but it would limit you only to a basic navigation options. IDEs take navigation abilities one step further.

Let's see what are the navigation features in IntelliJ. First of all, "Open Type" which is executed via Ctrl+N:



The same can be achieved with Ctrl+Shift+T in Eclipse and the behavior is exactly the same.

Ctrl+Shift+N will help you to open any resource. Eclipse does the same with Ctrl+Shift+R.

And it could have been all but there's more - Ctrl+Shift+Alt+N will find you all the appearances of a specific symbol, e.g. a method name:



It doesn't mean IntelliJ will scan the code for the occurrence of the specific symbols but it rather will match the code structure and look for the elements in your code so you will get a list of method declarations but not just any appearance of the method name in the code. This is quite useful if your classes are not sharing the same hierarchy.

Often when you navigate the source code you jump over class hierarchies, browse different methods, open type declarations, etc. It is quite common that once in a while you would like to step several steps back in your navigation and proceed with some other direction. Ctrl+Alt+(left|right) arrow is a very useful shortcut for such purpose. Whenever a caret hits any place in the code this place is remembered and you can go back and forward in the your browsing history. This is quite cool as if you navigate a large code base you might not even remember which classes you actually opened just a second ago.

Actually you can get a list of the recently visited files by pressing Ctrl+E - also quite often used to reopen the recently closed files.

Navigating back in forward is the same in Eclipse (with Alt+(left|right)arrow shortcuts). But I failed to find a list of recently edited files there, while this can be done in IntelliJ by hitting Ctrl+Shift+E shortcut.

There are plenty of other feature that make your code browsing experience more pleasant. For instance, both IntelliJ and Eclipse provide a feature to quickly browse the location of the currently opened class - Alt+Home will popup a navigation bar with the path to the class.

The cool feature in IntelliJ for this navigation par is that it is possible to brows the content of any file with Ctrl+Shift+I without having to really open the file:



In Eclipse a similar feature is called "Browse in Breadcrumb" called via Alt+Shift+B. The difference is that in Eclipse it works only if a Java type is opened and it acts only as a navigation bar - no quick preview available. Also, I didn't find an easy way how to get rid of that "Breadcrumb" in Eclipse besides clicking a dedicated button on the toolbar.

Ctrl+Shift+I also works in IntelliJ for any symbol that you'd probably like to inspect. For instance, you may press this shortcut on any method call, and it will raise a popup that displays the methods source code:



Also, if you try to browse the code that maybe involved in the type hierarchy, it is possible to select the implementation by choosing the type in the very same quick view popup:



Talking about navigation on the hierarchy of classes, IntelliJ implements that also quite nicely - quick hierarchy view is available the same way as in Eclipse. The feature that distinguishes IntelliJ is the ability to easily navigate to implementation. For instance, from interface method declaration to any of the implementations. If there are multiple implementations of an interface method Ctrl+Alt+B will call a popup dialog with the list of implementations:





Find Usages

"Find Usages" functionality is something that always made me nervous in Eclipse. Ctrl+Shift+G (find references in workspace) alone is ridiculously unusable. I mean, fine, you can find all the references to a method in the workspace with a single shortcut, but to do the same in a project scope you have to touch the mouse.

Here's what you have in IntelliJ. First of all Alt+F7 would give you almost the same as Gtrl+Shift+G in Eclipse besides that you don't have to touch the mouse in order to define scope of the lookup. Arrow down will give you a choice of scopes available for the reference loopup, the hit Enter and you're done. Even such simple feature is much usable in IntelliJ.



Besides that a quick lookup is also available - with Ctrl+Alt+F7 you will get a popup with the list of the references for a method of interest and directly from there you can use the Ctrl+Shift+I (mentioned before) to view the sources code in which the method is used:



Marking the occurrences of a variable works the same way in IntelliJ and Eclipse, besides that in Eclipse you can toggle the highligh and in IntelliJ you press a shortcut (Ctrl+Shift+F7) but no essential difference in the result for me. In IntelliJ, though, the highlight works on multiple variables while in Eclipse only on a single one.




Bookmarks

Navigating through the code is easier if there are means to bookmark some places in the code in order to be able return back to the location of interest. It seems that IntelliJ developers think of it as a priority as all the Ctrl+Shift+# and Ctrl+# shortcuts are reserved for bookmarking needs.

Ctrl+Shift+# (e.g Ctrl+Shift+1) sets a quick bookmark on the line where the cursor is located. After that you can return back to the bookmarked location by pressing Ctrl+# (e.g. Ctrl+1). The character symbols can also be used for bookmarking the locations but without quick access to the location afterwards - a special menu must be used. The screencast below demonstrates this feature:


In Eclipse, the bookmarking functionality is also available, but it lacks same level of usability as in IntelliJ - no shortcuts by default and not that simple to use without the mouse.

Local History

Both the IDEs, IntelliJ and Eclipse, keep track of local changes and provide local history view where you can browse the changes you've made. The feature is somewhat similar to using VCS, but without VCS.

At first it seems that the feature is implemented the same way in IntelliJ and Eclipse but if you dig deeper and start really using it - you'll notice the difference. Again, IntelliJ takes the feature a bit further in respect to usability (but I'm quite biased on this).



Specially the diff-view, is quite intuitive for this feature - you can clearly see what are the changes and easily migrate 'em. In Eclipse, the view is really similar but when I tested it - added new methods to a class, introduced new statements - I could not find those changes afterwards although an entry was created in the history view. In IntelliJ's view you can create a patch or revert to a desired version.



Smart Code Completion

Typing is just tying, right? It is actually amazing how intelligent cat the IDE be for this purpose. Isn't it cool when the IDE can autocomplete your statements just as you wanted?

Here's a simple example. I'd like to introduce a field in a class - I'll type List<String> list = new .. and hit Ctrl+Space then. And what does Eclipse suggests me? List and a name of the class I'm declaring this field in. Not really intelligent, uh?



In IntelliJ, after tying the same line of code, and pressing Ctrl+Space the popup gives me a list of List implementations available in classpath. Also, Ctrl+Shift+Space tries to be even smarter and proposes me ArrayList as a preferred choice.




Another cool autocomplete behavior I discovered in IntelliJ is that if you try smart type completion twice (Ctrl+Shift+Space) then IntelliJ will search for chained expressions which have expected type.



Live Templates

Templates is the other feature that helps you to write code fast. Usually an IDE provides you a set of templates but you're allowed to add new templates also. Let's see how it is done in IntelliJ.

Ctrl+J will popup a list of all the templates available and can be filtered by the template name. In case if you have just a few templates in the list it is not hard to find a correct one. But in case the list is too long it would be nice to have some other means for filtering the list.

Just a couple of use cases for the templates:


It is quite common to check the method parameter for null equality. The corresponding if-statement can be generated with inn template. So by typing "inn" in the editor and pressing the expansion key (TAB by default) we get the null-check-if-statement.



Next, quite common piece of code, is iteration over a collection of elements. IntelliJ provides a number of iteration templates for various types of loops. The common thing among the iteration templates is that all they start with "it" - iter, itco, itar, etc. So for instance we have a collection of elements and by typing iter and the expansion key we'll generate a foreach loop iterating the collection in context.



Templates could also be used for surrounding statements in case it is applicable. Ctrl+Alt+T does the trick. For instance, id like to surround the objects retrieval with try/catch block but I've already managed to generate a loop iterating the resulting collection:



Another template that I would mess in Eclipse by default is soutv that expands to a System.out.println(...) statement with a variable in context:



Editing and creating the templates is quite straightforward and well-documented. Usually I define my own try/catch template as it is not provided out-of-the box (which is strange).



The unique feature of the templates in IntelliJ is that you can define new templates that are backed with available features from the IDE, e.g smart code completion and other useful functions.

Refactoring

I think I'll skip the refactoring part at large - this is just insane to compare the huge amount of refactorings available in the IDEs. Only one note on the usability for a simple refactoring use case - extract local variable/method/etc. In Eclipse you have to select the full statement before you press Alt+Shift+L (or position the cursor just after the expression), otherwise the IDE refused to understand what do you want to do popping up a message "An expression must be selected to activate this refactoring".

IntelliJ seems to be a bit more intelligent still detecting the context and providing you options for the same refactoring:




More to come, stay tuned :)

From http://arhipov.blogspot.com/2011/06/whats-cool-in-intellijidea-part-i.html

Published at DZone with permission of its author, Anton Arhipov.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Oleg Varaksin replied on Tue, 2011/07/05 - 2:24am

Hi Anton,

I thought I know all key combinations in Eclipse and IntelliJ, but what you exposed is really fantastic. I didn't know about Ctrl+Shift+G (Eclipse) and Ctrl+Shift+I (IntelliJ). Thanks for the useful info!

Anton Arhipov replied on Tue, 2011/07/05 - 6:06am in response to: Oleg Varaksin

Enjoy! :)

Slava Lo replied on Tue, 2011/07/05 - 8:08am

Another short-cut for navigation that I find very useful is 'Ctrl-Tab' in IDEA which is close to Ctrl-F6 in Eclipse - allows you to jump to previous editor/view (similar to Alt-Tab in Windows)

Duc Tung, Vu replied on Tue, 2011/07/05 - 5:44pm

Generally, i love IntelliJ IDEA much more than Eclipse because of following general aspects: - Quality of the features, when IDEA says, it supports some technology, frameworks, then it also actually does it well and simply gives you the most important things to work with the technology and especially it works and works after versions. Quality of Eclipse's plugins is still far from that. - It's smart, all the time it gives you the pleasant feeling "Aha, you do that too!", e.g when coding a mix of code (GSP code + HTML + Groovy + JavaScript) its really smart, it always knows what you are writing and offers you helpful auto-complete options. What i also really like is the feature Open-and-JumpTo (Ctr+right click, or F3 on elements/references under caret), most of the time it understands whatis being under caret and open it for you (java code references XML files, i18n message code etc.), its just far smarter than Eclipse. - And a very basic feature what many open source editors also have(NotePad++) is vertical-indent-guide-lines. They help alot to format code structure and improve readability. But Eclipse... after more that 10 years still doesn't have it! - Last but not least is the exellent integration with the CVS systems. The russian guys, somehow, they understand us, what we want + need. ^^

Steven Hatfield replied on Tue, 2011/07/05 - 6:14pm

One of the things that bugs me about Eclipse (which I used for a while) that is much better in IntelliJ IDEA is that Eclipse doesn't automatically detect and reload changes to the underlying files in the workspace, while IDEA does. This makes for some pretty uncomfortable moments when I've updated from SCM and Eclipse is in some kind of limbo-land until I manually refresh the workspace. IDEA does this automatically.

Sebastian Mueller replied on Wed, 2011/07/06 - 1:44am in response to: Duc Tung, Vu

I agree that the CVS integration is excellent (although for reasons unknown to me they managed to break many aspects of it during the last few releases again and again, and CVS by no means is a moving target), also GIT seems to be ok, but the state of the Mercurial integration is horrible. It would have been so easy to integrate TortoiseHG, at least for those dialogs that IDEA really really sucks: No revision graph (!), mediocre push/pull facility, no support at all for hg extensions. Please someone (Jetbrains) - create an IDEA plugin that integrates tortoisehg and you can easily beat Eclipse's mercurial support.

That said, I am using IDEA since version 4 and will continue to do so - the only time I'm leaving is for Visual Studio to do C# (of course only with Jetbrains' Resharper plugin ;-) )

Anton Arhipov replied on Wed, 2011/07/06 - 2:54am in response to: Sebastian Mueller

I'm using Mercurial integration with IntelliJ myself. Indeed the graph is missing, but that is generally the only thing that I miss. AFAIK, the current integration was a 3rd-party plug in which was taken over by JB guys. The more problems you submit to youtrack regarding this the more chances that it will be improved.

Viktor Nordling replied on Mon, 2011/07/11 - 11:44am

Excellent post!

Regarding the Mercurial integration, I just noticed that the Incoming and Repository tabs in the Changes view are missing when I use Mercurial. They are there when I use Subversion. 

Do you know if this is just a setting or maybe that is not implemented for Mercurial yet?

Anton Arhipov replied on Mon, 2011/07/11 - 2:50pm in response to: Viktor Nordling

I suspect it is not implemented. Probably it is a good reason to file a feature request.

Sirikant Noori replied on Sun, 2012/01/15 - 11:46am

Eclipse has "Smart Code Completion" support I guess what you didn't do it to import the List before you went over to the assignment where you expected smart code completion.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.