X

Geertjan's Blog

  • November 7, 2005

Magic Multiline Code Templates in NetBeans IDE 5.0 (Part 1)

Geertjan Wielenga
Product Manager
While working on the NetBeans 5.0 Keyboard Shortcuts and Code Templates card, I discovered something pretty amazing. Not only will NetBeans IDE 5.0 provide multiline code templates (that's not really news anymore), but when the code includes variables, you can change all instances of the variable in one go.

For example, when I am in a Java file in the Source Editor, I type fori and then press Space. Now I see the following:

Do you see that the integer i has a blue background? If I now change that first i to something else, then the other two instances change as well. (But if I change one of the other two, the relationship breaks and the instances no longer change when I change the first instance.)

So, how does this work? Take a look at the definition of the fori code template (click to enlarge):


This is the definition of the code template:

for (int ${i index} = 0; ${i} < ${arr array}.length; ${i}++) {
${cursor}
}

I can't say that I understand everything here (e.g., what's that ${arr array} thing about?), but two things I do know—when you set an index, you create a relationship between instances of that index and, in addition, you also set the tab order (when you tab across the result of the fori code template, you'll see what I mean).

All in all, this really lets you create very powerful code templates—the old days of having just short, single-line, code templates are over. Instead, complex, multi-layered, highly sophisticated code templates are what you'll find in NetBeans IDE 5.0.

Join the discussion

Comments ( 8 )
  • Benoit Nadeau Monday, November 7, 2005
    I made my own macro on my blog: http://www.livejournal.com/users/benad/6237.html . The second word in a {} block seem to be the "type". While I don't know what the "index" type is, you can use any primitive, or any class type using instanceof="java.io.InputStream"...
    I guess we'll have to look at the code until this is properly documented.
  • Pete Royle Monday, November 7, 2005
    I'm not exactly sure of the significance of ${arr array} either, but I tried your tip and check out what happened:
     List excludePropertiesList = new ArrayList();
    for (int i = 0; i < excludeProperties.length; i++) {
    }

    This is the code immediately after typing "fori ". Notice the presence of excludeProperties.length in the guard! Nice.
  • Pete Royle Monday, November 7, 2005
    Oh, by the way, I guess it's handy to know that excludeProperties is a String[] defined earlier in the function. I guess I just got a bit excited.
  • Ian Schneider Tuesday, November 8, 2005
    heres my "toArray" template:
    ${Class}[] ${cursor} = (${Class}[]) ${collection instanceof="java.util.Collection"}.toArray(new ${Class}[${collection}.size()]);
    Which would be vastly cooler if somehow the generic type of the collection could be obtained...
    and my "newCollection" template:
    ${Class class}<${Type class}> ${name} = new ${Class}<${Type}>();
    Which would also be vastly cooler if somehow class could have a hint like "isAssignableFrom"...
    Code completion and next/previous matching word shortcuts are exceptionally useful in abbreviations.
  • Ian Tuesday, November 8, 2005
    Crap-diddly. You'd think if you formatted your text
    in
    that
    little
    editor,
    it would
    stay so.
  • Geertjan Tuesday, November 8, 2005
    Have to use HTML tags, unfortunately... Could you try again? Can't really parse what your code templates are doing... By the way, it should work with generics.
  • Ian Tuesday, November 8, 2005
    Sorry bout that...using html

    toArray:
    ${Class}[] ${cursor} = (${Class}[]) ${collection instanceof="java.util.Collection"}.toArray(new ${Class}[${collection}.size()]);



    With respect to generics, if the ParameterizedType of the collection could be made available (as I know it could from using netbeans jmi) then specifying the value of 'Class' would only be needed if no Type was available.

    newCollection:
    ${Class class}<${Type class}> ${name} = new ${Class}<${Type}>();


    The latter being not especially useful, but still reduces some typing.

    <blink>Yay, html</blink>
  • Alicia Monday, July 31, 2006
    Nice !
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha