3.3: Non-Modal Local Refactorings

This article was originally published in the EclipseZone Tips & Tricks section of Javalobby.org

3.3 M4 includes the introduction of several 'local' refactorings as quick assists (aka Ctrl+1) that work in linked mode. There are some definitions I should supply just to make sure we're all on the same page. By 'local', I mean isolated to the currently active editor. Linked mode refers to the special boxes you get around editable areas in the editor when applying a template or some other similar task:

The advantage of linked mode is it's lack of modality; since you are already typing in the editor, breaking out of the refactoring to perform some other changes is entirely natural, and requires no unusual key or mouse gymnastics.

The refactorings that have been added to the quick assist system include 'Extract Constant', 'Extract Local Variable', 'Inline Local Variable', 'Convert Local Variable to Field', and 'Convert Anonymous Member to Member Type'.

To initiate one of these refactorings with quick assist, the process is no different than with the traditional refactor menu. First, your caret must be placed in context of what you want to refactor. The context is different for each of these examples, but I will choose 'Extract Local Variable' as an example. For extract local variable, the context (as you could probably guess) simply needs to be a value of some sort that isn't already being assigned to a local variable in the method. Here is a good example:

I want "123" to be captured in a local variable, so I'm going to highlight that string in the editor.

Next, I hit Ctrl+1 and I am given my assist options:

I choose extract local, and I'm given this linked edit session:

I change the name, and that's it. These refactorings can really improve your agility as a developer; the first step is simply building a 'muscle memory' to start using them as opposed to editing the old fashioned way by cutting, pasting, and re-typing the new format.

Note that the Eclipse team is hard at work to make the refactoring process much more agile in general - another feature in 3.3 M4 is the ability to refactor without saving any dirty editors. In addition, I wrote a few months ago about the changes to the rename refactor to also make it work in 'linked mode' ; this change is more significant because of the cross-file dependencies.