Archive for the ‘Tips & Tricks’ Category

Creating live templates from… templates

Tuesday, December 22nd, 2009

Sometimes I find myself writing repetitive code constructs without being able to extract common code due to Java syntax hmm… let’s call them peculiarities. ‘Sounds like a job for a live template’, I think, and I simply create it. Here’s how. The documentation has it too, but a good example never hurts.

Let’s suppose you have read/write synchronization in your project, and from time to time you need to wrap a bunch of statements in, say, a read action. This involves putting them into a Runnable and giving the latter to some runReadAction method. Do you still do this manually? You don’t need to, IntelliJ IDEA will help you!

First, find an existing code which already invokes the read action and select it:

Go to Tools menu and choose Save as Live Template there:

You’ll see a typical Edit Live Template dialog with the text you’ve selected. Note that all the class references are qualified there and Shorten FQ names checkbox is on, so the mentioned classes will remain the same no matter where you choose to insert it.

What we want to keep is the external syntax: the method call and the runnable. So remove everything inside the run() method braces and replace it with a $SELECTION$ on a single line. It means that you may select something and it’ll be wrapped in this particular construct.

Enter some abbreviation to easily invoke it each time, and some description (optional, but useful). Finally, the formatting looks not quite pretty, so turn on the Reformat according to style checkbox.

That’s all! Click OK and start using it. Find a fragment you want to wrap into a read-action, select it and invoke Surround With… action (Ctrl+Alt+T). You’ll see the read action template in the list:

Select it and the read action is in there:

IntelliJ IDEA PSI Viewer

Thursday, November 19th, 2009

If you’re an IntelliJ IDEA plugin developer, you’re surely going to enjoy IntelliJ IDEA PSI Viewer (Tools -> View PSI Structure), which displays internal structure of various files, as they’re interpreted by IntelliJ IDEA. If you’re implementing a new language support, or trying to add some cool new features to what’s already supported — this viewer is what you’re gonna need a lot.

As you see, PSI Viewer is made up of of tree parts:

  • An editor with file content
  • PSI structure view with parent-child tree of its elements
  • Area that displays references of element selected in PSI structure view.

Note that references marked as red resolve to null.

Available since build 92.35

ThreadLocal in One Click

Friday, October 30th, 2009

Most of applications initially are single threaded, and IntelliJ IDEA was no different; though luckily, now it isn’t — but we had to adapt our code to use multiple threads. In this post I’m going to tell you how.

In our example we see SAXBuilder, which is too expensive to be created every time we need it, so it is stored in a static final field.

Because SAXBuilder is not thread safe, multiple calls to loadDocument from different threads cause a lot of interesting exceptions. This is why we need either to make access to this field synchronized, or to make the field ThreadLocal. In our case, we’re choosing the latter. We encapsulate the field, change its type and initializer, then fix a generated getter. Quite a lot of work, right? Luckily in Maia you can do it all in just one click. Just place caret on a field and press Alt+Enter to smoothly migrate it to ThreadLocal.

Invoking Run/Debug Actions in IntelliJ IDEA 9

Monday, October 26th, 2009

In IntelliJ IDEA 9 (Maia), we’ve slightly changed the way you run and debug your applications. You’re still able to quickly run or debug a previous configuration (Shift+F10/F9), as well as to create and run/debug a temporary configuration in a context-dependent manner (Ctrl+Shift+F10/F9). However, it’s now much easier to select and run/debug different configurations from the keyboard.

In IntelliJ IDEA 9, you can choose a configuration that you wish to run with one of the following shortcuts:

  • Alt+Shift+F9 to debug your application.
  • Alt+Shift+F10 to run it.

Invoking any of these actions displays a pop-up window with available configurations, like this:

You can edit a configuration before launch (press F4) or show a configuration dialog box to edit all your configurations and their default settings (press 0). You can also instantly delete a temporary configuration by pressing Delete.

That’s not the end of the story: you’re now able to temporary switch to an opposite action directly from this pop-up window by pressing and holding Shift. When you do that, Run becomes Debug and vice versa.

And even more: we’ve extended the pop-up window with context-dependent configurations. That means whenever you press Alt+Shift+F10 right in a test method’s body, the pop-up displays a temporary configuration to run the current method only or all tests from the current test class.

We’ve also added a number of quick access shortcuts:

  • 1 for a previously selected configuration.
  • 2 and 3 for context-dependent configurations.
  • 0 to display the Edit Configuration dialog box.

One more tip: we’ve mapped this pop-up to F9 (debugger’s Resume Program action). The pop-up now displays on pressing F9 when you’re unable to actually resume anything - that is, when you’re not yet running any debug session.

Intelligent Copy&Paste

Friday, October 2nd, 2009

Do you find yourself copying and pasting parts of bodies between different methods and then facing broken code?

Unfortunately, parameter names tend to differ — like in this (very simplified) example:

Nothing is easier than fixing this problem with a few keystrokes in IntelliJ IDEA!

First, move caret on any red reference (highlighted with red color), or simply press F2, and then invoke available Quick-Fixes via Alt+Enter:

Now select Rename Reference (again, you can use arrow keys or just start typing Ren.. + Enter) and… IntelliJ IDEA fixes code for you!

See, sometimes it’s just the small features making it possible to achieve maximum productivity.

Quick Lists to Group Your Favorite Actions

Thursday, October 1st, 2009

There’s a lot of useful actions in IntelliJ IDEA that don’t have shortcuts for many different reasons: they’re not used too often or there’s simply no more convenient key combinations left.

For example, I often want to get access to VCS actions like Compare with latest repository version or Revert current file or even submit a single file with a quick NPE fix without opening a menu or going to Changes view.

So how can I call any of these actions with a single shortcut? That’s really easy: I just need to define my own Quick List and map it to a shortcut (Control-X in my case). After I’ve done that, every time I press Control-X I will get a pop-up like this:

Quick List in action

Quick List in action

Now I can press 5 to quickly commit the current file or 2 to revert all my changes. OK, now let’s see how to create a Quick List and assign it a shortcut.

(more…)

Improved Linking of Perforce Jobs

Thursday, August 27th, 2009

In the upcoming Maia EAP you will find a slightly different Commit Changes dialog.

  • Automatic use of JobView filter on search results.
  • Support for Perforce native search query syntax (in an example below we’re looking for Job in any field)

search result

Original IntelliJ Cast

Friday, August 14th, 2009

Let’s talk about type casts — those things you hardly can avoid in JVM-based code. Being a helpful IDE, IntelliJ IDEA now does all the casting stuff for you when you are using its code completion in Java and Groovy.

To get you started, have a look at this Groovy example. In a dynamic language there’s almost no need in type casting at all — you just invoke any method on any object and hope it won’t fail. IDE can help you here, suggesting a list of acceptable choices based on preceding type checks:

In Java, there’s always a need in explicit casting. It’s easy of course, when you know the type to cast expression to. Smart completion after opening parenthesis has been able to do this for ages:

Imagine you’ve just checked an expression type via instanceof and now you want to invoke a method on the cast expression. Quite a natural wish, isn’t it? A week ago you had to write a cast manually, use a light bulb intention action or inst live template. Now you just invoke code completion after the cast expression and it suggests the members of the cast type. A lot simpler, right? Of course, the cast will be inserted automatically:

This works for both basic (Ctrl+Space) and smart completion (Ctrl+Shift+Space). I’ve been using this feature for 2 days now and I already can’t understand how could I survive without it through all the previous years.

Impressed already? But, there’s more. Say you’ve just checked that a certain expression has certain type. Then, you may want to cast that expression and assign the result to a variable, or pass it to a method. All you need is smart completion:

Moreover, as you know, IntelliJ IDEA has second smart completion feature. It’s now also aware of the run-time type checks that you made. After such a check you can perform cast and method invocation in a single action! Well, actually, you have to press Ctrl+Shift+Space two times, but that still counts:

Enjoy!

Analyzing Dataflow with IntelliJ IDEA

Wednesday, August 5th, 2009

Upcoming IntelliJ IDEA version, Maia brings you an improved version of the Dataflow to this feature and the completely new Dataflow from this.

Here I am describing how these features work and how they can help you the Code Archeologists better understand your code.

I am using Apache Tomcat source code as an example.
Let’s have a look at the SingleSignOnMessage class and its obscure String authType field.

(more…)

5 Cool Shortcuts You Have Probably Never Tried

Thursday, July 30th, 2009

I’d like to tell you a bit about a bunch of cool IntelliJ IDEA shortcuts you have probably never heard of.

Ctrl+W/Ctrl+Shift+W When it comes to precisely selecting a value, expression, statement or any other block of code, these shortcuts are a real killer. Just try it to see how quicker it works than traditional Ctrl+Shift+NavKeys combo.

Ctrl+Shift+F12 This hides everything but the editor and so leaves you tête à tête with the code.

Ctrl+Tab, Delete Pressing Delete when Switcher is open (Ctrl+Tab) allows you to close arbitrary editor tabs and hide tool windows (available in Maia builds 10612 and later).

Ctrl+Alt+Shift+Up/Down If you are using Version Control integration, you gonna love this because so you can quickly navigate between changes in a file.

Alt+Home Summons Navigation Bar to an active window, so you can now hide it from under main menu and quickly use whenever needed. By the way, all the cool shortcuts (Alt+Insert, for example) work nice inside of Navigation Bar, which lets you create new files, classes, and everything really really quickly.


Hope this all makes development even more a pleasure!