IDE tip: AST-based Selection

AST-based selection is a feature which lets you select the surrounding "block", based not on syntactic clues (such as braces and parentheses, as is offered by practically all editors), but based on the parse tree created by the compiler (or the "AST", for abstract syntax tree).

In NetBeans, there are two complementary actions to deal with AST selection: Select Surrounding and its reverse. They have other names which I can never remember, but the keybindings are as follows:

Select surrounding block

  • Mac: Ctrl-Shift-. (period)
  • Windows/Linux/Solaris/Others: Alt-Shift-.


  • Mac: Ctrl-Shift-, (comma)
  • Windows/Linux/Solaris/Others: Alt-Shift-,

On my U.S. keyboard, the comma and period keys are marked with "<" and ">" for the shift key which is somewhat reminiscent of blocks.

These two actions cooperate and are stateful: they remember the original caret location such that you can "go back".
Here's how it works. You want to select something surrounding the caret, say the whole method. In that case you just
hit the surrounding action. With your fingers holding ctrl and shift you hit dot -- once, twice, bam bam bam. If you do it
quickly you might "overshoot" and go one too far and select say the whole class instead of just the method. If so just press
comma and voila, it goes back one step down the selection hierarchy you just came up.

This makes this a very quick way to select code blocks -- no more need to grab the mouse and try to hit exactly the selection
boundaries between characters.

Here's an example of where this comes in useful. I sometimes remove long expressions from method call arguments by pulling it
out as a separate variable. Here's my call, and I want to extract the whole Math.min expression.
First I move the caret anywhere within that expression:

Then I press Alt-Shift-. (or Ctrl-Shift-. on Mac):

One more time:

Let's pretend I was doing it quickly and hit it one extra time:

That's too far, so I press comma (with the modifier keys still pressed):

In practice (and with practice) this is done very quickly, faster than I can
accurately hit both ends of the expression with a mouse drag. And to finish
out the example, let me show a couple of more things related to the task.
Notice how there's a light bulb in the left hand side margin of the editor.
That tells me there are associated quick fixes. I press Alt Enter,
and the quick fix dialog is shown:

I choose "Introduce Variable" and press Return. This gives
a dialog where I can choose the variable name (and whether to replace all occurrences
when applicable). It suggests "min" since that's the function
name of the expression we're extracting.

Just press Return to apply the refactoring, and we've pulled out the argument

I just pressed return and got the default name. Then I realize that this may
not be a great name. What is that last parameter again? I can place the caret
somewhere within the last argument and press Cmd-P (or Ctrl+P
on other platforms) to get NetBeans to "Show Method Parameters".

Let's rename it to length instead. Press Ctrl-R (all platforms) to
apply rename refactoring, which (because this is a local or private variable)
will have "instant rename" applied where your edits are applied immediately
for every keystroke:

Just type the new name - notice that all the boxed expressions are edited synchronously.

Using AST-based selection to identify code blocks to extract as variables or methods
is useful, but it can be used in other ways as well. Let's say I want to add
a second validation step to this function. First I select the code block using
AST selection:

NetBeans has a keyboard command for duplicating the current line: Ctrl-Shift-Down
(and on Mac, it's Cmd-Shift-Down.) Note that when you have a selection, this
duplicates the entire selection. So all we have to do now is apply the duplicate command
and we've got a second if clause we can just tweak to whatever it is it needs to do.

AST based selection should be available in nearly all editor types in NetBeans.
In Ruby, JavaScript, Python, etc it also handles documentation
blocks well - so if you apply select surrounding in say a Ruby comment, it will first select
the current line, then the whole documentation block, then outwards in the AST hierarchy.

Hope you find this helpful. If there are Eclipse or IntelliJ readers who know corresponding
keybindings in Eclipse, feel free to leave a comment so others can find it easily.


In IntelliJ IDEA it's CTRL-W (to increase selection) and SHIFT-CTRL-W to decrease selection.

Works wonders indeed, also to determine the precedence of operators.

Posted by oenli on March 30, 2010 at 05:06 AM PDT #

Thanks! I looked, and in Eclipse the shortcuts (for Mac) are Ctrl-Shift-Up and Ctrl-Shift-Down. I think on other platforms It's Alt-Shift-Arrows.

Posted by Tor Norbye on March 30, 2010 at 05:16 AM PDT #

"AST based selection should be available in nearly all editor types in NetBeans. In Ruby, JavaScript, Python, etc (but unfortunately not yet in Java)". Perhaps you mean JavaFX. I tried it out in NB 6.7.1 java editor and it works.

Posted by Rohan Ranade on March 30, 2010 at 05:17 AM PDT #

Hi Rohan,
sorry, I didn't express that very clearly; AST selection definitely works for Java; what I was trying to say is that handling line-comments as selection blocks (because line comments don't exist in the parse tree) is something we handle for many language types (such as those listed) but not for Java. It should be trivial to fix, let's file an issue on it :) It hasn't really affected me since for my usage pattern (usually to extract code blocks) I don't need to select comments in this way. Anyway, I updated the blog entry to remove this confusing wording.

Posted by Tor Norbye on March 30, 2010 at 05:22 AM PDT #

Thanks for this series of posts. They are taking me back to a time I was pair programming with a friend and part of our game was to optimize for keystrokes. If the driver used more keystrokes than was necessary, he was called on it. It was good fun because it kept the observer engaged even when the code that was being written at the moment was otherwise uninteresting boilerplate. Needless to say it also had both of us spending our downtime exploring everything that the IDE could do for us. Since then I've switch IDEs (from NetBeans to Eclipse), but have not invested the time in committing the shortcuts to finger-memory. Learning shortcuts is easier when you aren't heads down writing the code (i.e., as the observer in a pair). You've inspired me to go back and relearn!

Posted by Brandon on March 30, 2010 at 06:35 AM PDT #

ZOMG! I mean, I have longed for that feature for years, never aware it was right there in front of me in the IDE I use daily.

Thank you very much!!

Posted by Tomás Lázaro on March 30, 2010 at 10:34 AM PDT #

In Eclipse on Linux & Windows:

"Select Enclosing Element": Alt+Shift+Up
"Select Next/Previous Element": Alt+Shift+Right/Left

Posted by Moandji Ezana on March 30, 2010 at 09:19 PM PDT #

I use this \*all the time\* on IntelliJ and Eclipse.
I changed the default biding to ctrl + down arrow, so I can do it easily with one hand.

Unfortunately, on Eclipse, this fails very often (mostly when the current code doesn't compile), and when it does, it selects the whole editor... And of course you loose the previous caret position.
Hours of frustration :)

Posted by BoD on March 31, 2010 at 03:20 AM PDT #

Great post. Please do more of these. :)

Posted by Les Stroud on March 31, 2010 at 07:14 AM PDT #

+1 on "ctrl + down" - "ctrl + up" key bindings. I never use text scrolling with ctrl arrows anyway.

Posted by Tomás Lázaro on April 01, 2010 at 01:22 AM PDT #

Post a Comment:
Comments are closed for this entry.

Tor Norbye


« July 2016