Author Archive

Surround With…

Tuesday, July 25th, 2006

IntelliJ IDEA makes it very easy to surround a code block with if, while, for and other statements or to make a code block become part of such statments as try/catch or synchronized. Simply select the code block to surround (don’t forget to use Ctrl + W to increase the current selection) and then press Ctrl + Alt + T (or right-click the selection and select Surround with… from the menu). IntelliJ IDEA will show a list of options to choose from.



Once you have made your choice, IntelliJ IDEA will add the necessary code and put the caret to the position that allows you to complete the surrounding statement, if needed.



After that, you’re done. As you can see, adding such a surrounding if statement does not break the flow of coding at all and there is no need in any additional key stroke or mouse move.



Furthemore, IntelliJ IDEA not simply surrounds the code, but makes it as smart as possible. For example, if you choose to surround a code block with a try/catch statement, IntelliJ IDEA will add all necessary catch statements for all thrown exception types that it is able to determine within the selected code block.

Another useful scenario of using ‘Surround With…’ is when inspecting objects in the debugger’s Expression Evaluation window. Let’s say you want to evaluate a method call on the inspected object where the method is a part of the runtime type but not a part of the declared super type. IntelliJ IDEA helps you with casting the object to the runtime type: just press Ctrl + Alt + T in the expression box, and select Surround With ((RuntimeType) expr) from the list.



You can then invoke any method that is a part of the runtime type of the inspected object. Again, the described operation of casting to the runtime type does not break the flow of coding due to IntelliJ IDEA’s intelligent and unobtrusive assistance.

By the way, the Expression Evaluation view supports code completion, JavaDoc popup, etc. just like the normal Java editor.


Technorati tags: , ,

Spring Bean Support

Monday, July 10th, 2006

IntelliJ IDEA is aware of the Spring beans that are part of your project. If you apply Find Usages on a property setter method, for example Customer#setLastName(), and there is a Spring bean of the matching type and with the matching property being declared in a Spring XML file, IntelliJ IDEA will find it and will properly list it as a usage.



IntelliJ IDEA also respects Spring bean definitions during Safe Delete actions. On a side note, it does not make a difference whether Find Usages, Safe Delete, Rename Method, etc. are applied on the property setter method in the Java code or on the property attribute value in the Spring XML file.

You can also apply Goto Declaration (Ctrl-B or middle-button mouse click) on the name attribute’s value in the Spring XML file, for example on lastname, and IntelliJ IDEA will take you to the property setter method in the Java code.

Technorati tags: , , ,

Numbered Bookmarks

Monday, June 19th, 2006

On top of anonymous bookmarks (F11 to toggle between activate/deactivate, Shift-F11 to manage), IntelliJ IDEA provides so called numbered bookmarks. What this mean is that on any line of any file managed by IntelliJ IDEA (Java, HTML, XML, JDK source files, etc.), you can define a bookmark that is identified by a number between 0 and 9. Simply put the caret on the desired line and press Ctrl-Shift-<Number> to toggle between activate/deactivate the numbered bookmark. You can then jump to such a numbered bookmark from anywhere within IntelliJ IDEA by triggering Ctrl-<Number>.

For example, place the caret at a point of interest in a Java method and press Ctrl-Shift-1. The gutter to the left will then show a tag labeled with the number 1. You can now jump to this bookmark from anywhere within IntelliJ IDEA by pressing Ctrl-1.



Personally, I find numbered boomarks very handy when working on a specific task where I have to jump back and forth between certain code locations repeatedly. It takes one keystroke to create the numbered bookmark and it takes one keystroke to jump to such a numbered bookmark. It couldn’t be any more efficient.

Technorati tags: , ,