Author Archive

Groovy stacktrace folding

Wednesday, March 17th, 2010

When you look at a typical Groovy stack trace, you usually see something like this:

This is not terribly helpful, since there are only three lines from the actual source code. Everything else refers to Groovy’s internals. From now on, though, reading Groovy stack traces will be much easier:

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 Completion Feature List

Tuesday, September 22nd, 2009

How are you learning stuff about IntelliJ IDEA? Reading this blog, using Productivity Guide from the Help menu, or even maybe reading the documentation, right? None of these contain all the information you need, though. This is why we decided to start listing details of different features — complete and uncensored, directly on Confluence. We begin with Java completion features. Hope reading this will help you improve your productivity. Enjoy!

Super Completion

Tuesday, September 15th, 2009

Do you often find yourself calling a super method with a large number of parameters? Typing all those arguments one by one is soooooo boring, isn’t it?! Don’t be upset anymore — latest Maia EAP can rescue you. Just use Smart Completion (Ctrl+Shift+Space):

Debugger Evaluate Expression Enhancements

Monday, September 14th, 2009

Say, you hit a breakpoint in Java code:

And you cleanly see that component is a label:

You now want to investigate label internals via Evaluate Expression (Alt+F8), e.g. invoke its getText() method, but component declared type is Component and it does not have this method! The only way out is to use Surround with run-time type (Ctrl+Alt+T) first to cast it to JLabel and only then completion becomes available:

Not anymore it isn’t. IntelliJ IDEA tries to help with casting wherever possible, so you can now get the same results using Basic code completion (Ctrl+Space) only:

…press Enter and the cast is done for you:

Needless to say, it works with Smart completion (Ctrl+Shift+Space), too. It works even for complex expressions with method calls. Note, that method evaluation can take a lot of time, not to mention the possible side effects, so evaluating expressions every time you hit completion is not so nice. This is why you need to press the same shortcut twice to get the run-time type variants. That’s what you’re going to see at the bottom of completion list:

And you can play with all this now!

Groovy-related Configuration Changes

Tuesday, September 8th, 2009

The next Maia EAP brings you massive changes in Groovy, Grails, Griffon, Gant & Gradle support, mostly affecting framework configuration.

First, Grails/Griffon application options are available when you create a project or a module:

You may also choose to import an application from existing sources:

Earlier, you needed Gant & Gradle in module dependencies to run build scripts — quite awkward, because the way you are building your project should not affect its classpath. Now, we have per-project settings for Gant & Gradle:

Also, you can now write Groovy code and get full coding assistance with compilation as long as you have Groovy jars anywhere in a module classpath. Together with the new compiler Resource patterns path constraints feature (/ - path separator; /**/ - any number of directories) this made Groovy facet obsolete. So, there is no Groovy facet anymore.

You can still add any Gr8 libraries to a module via Add Framework Support context action:

Everything you’ve just read about will be available in the next EAP.

Gradle Support

Friday, August 21st, 2009

The latest Maia EAP features lightweight support for Gradle build framework. You can add Gradle library to a Groovy facet:

You can also run Gradle scripts via Ctrl+Shift+F10 in a script file:

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!

Initial Griffon Support

Wednesday, July 29th, 2009

The next Maia EAP brings you basic Griffon support, similar to Grails integration. You’ll be able to add Griffon library to Groovy facet:

IntelliJ IDEA will then create a Griffon application or plugin directory structure and take all the steps necessary to keep it up-to-date, as you install plugins and add libraries. As with Grails, a dedicated Griffon project view will appear together with a run/debug configuration:

Also, Run Griffon target with target autocompletion action can be found under Tools menu.

Compact Create Class Action

Friday, May 29th, 2009

In the upcoming Maia EAP you won’t find separate actions for creating classes, interfaces, enums or @interface anymore. Just one action that does not clutter your desktop — New Java Class.
This does not mean you won’t be able to create interfaces, enums and annotation types, though:

The same functionality will be available for Groovy classes.