New in CFEclipse: Templates

Wednesday, October 28, 2009

This is part of an ongoing series on Timesavers. The goal is simple: short, easily-digestible posts designed to help developers get faster and more productive

The recent 4th episode of This Week in ColdFusion inspired me to finally get the word out there regarding this sweet new feature of CFEclipse: Templates. This has been in CFEclipse since June, but we never advertised it or blogged about it. If you’re a java developer who uses Eclipse, you probably rely on templates so heavily that you’d think they were just baked right into the IDE. You know how when you’re editing java, and you type “for”, and it pops up a bunch of suggestions? That’s what this is.

Denny Valliant’s initial implementation only came with two default templates, though I suspect more will come down the road. It’s easy to write your own, though, and here’s how:

  1. Window – Preferences – CFEclipse – Editor – Templates
  2. In the Templates window, hit “Add New”
  3. Type in a name, a description, and your templates. You can use “Variables” by using ${} syntax, and the contents in between the brackets can be any arbitrary name. Trust me, you’ll see how this works in a minute
  4. If you want custom stuff – like where to put the cursor or some other “standard” variables, you can insert them with the dropdown list
  5. Hit OK and get out of the preferences window. The window looks like this:

cfe_templates_1

Now, in your editor, I created a new array, went down a line, and started typing the characters that begin with my new template name. For example, in my screenshot, I had a template named “looparray”, and so I started typing “loop”, and in my editor, that template appeared:

cfe_templates_2

I hit “Enter” to accept the template, and now I’m in the template, and with each tab, I’ll move on to the next part of the template.

cfe_templates_3

In this example, I have an array named “stogies”, so I tell my template that my array is named “stogies”, and it replaces my “template variable”. I want my index variable to be named “stogie”, so I type that in, and it replaces it in the cfloop tag AND in the block of code that followed.

cfe_templates_4

This can save you a lot time right now, for free. This has the potential to be a huge timesaver, especially if developers write their own templates and share them with the community via the “export” functionality.  Hopefully, as the CFEclipse developers get more time, more templates will come bundled with CFEclipse.

Viva la TimeSavers!

6 comments:

Rob said...

This is nice, but what am I missing here that already is not available for us in using the Snip Tree view in Eclipse?

hodgepodge said...

SCHWING!

I never got into snippets. Not that there's anything wrong if you prefer them--java editor templates feel a lot better to me.

It's fantastic that both are supported.

Rob said...

Don't misunderstand, I'm not trying to downplay this. It just sounds like pretty much what snippets give you with only a few modifications in how the same thing is accomplished. I just wanted to make sure I wasn't missing something.

bill shelton said...

Nice, Marc! Thanks ... This is especially for us *nix users. --bill

Marc Esher said...

Rob, completely fair question. I think the full potential of the CFE templates has yet to be realized. This is no doubt a first-stab at it, and I am hoping that when Denny gets more time to flesh out this functionality, they will become even more useful than snippets.

Here's what I see so far:

1) greater variable control. For example, if you look in that "insert variable" dropdown, you'll see stuff you don't see options like "word_selection" and "line_selection" and "dst" that you don't get with regular snippets. I believe these are not yet functional but will be useful down the road. I'll try to get Denny to come on here and explain them.

2) ability to share via "export" and "import". Currently, sharing snippets is a manual affair (unless you use ANT to copy them... check out the mxunit/cfeclipse directory in the mxunit download for all the cfe snippets that come bundled with mxunit). But with templates, you could export yours, give them to your friends/team/whomever, and they could easily import them.

3) Potential for bundling a whole slew of common templates. If you look at the window -- preferences -- java -- Editor -- Templates, check out all the templates that come with Eclipse. I can envision CFEclipse coming bundled with tons of commonly typed chunks of code.

For me, this ability to share, and the ability to bundle, are where the potential lies.

One other out-there possibility is in evaluation of variables already on the page for use in the templates themselves. You get this when editing java, for example, and you declare a variable, then type "for", and tab through the various templates. It will not just let you type in any old variable name, but give you suggestions based on the variables you've declared in your file already. This is, admittedly, a huge PITA for a dynamic language like CF. Frankly, I wouldn't bet on this getting in there, particularly since there's currently a lot of debate going on in the CFE world regarding the state of the parser -- whether to keep it and try to fix it, whether to ditch it, and if so, what to replace it with. Nonetheless, that'd be a kickass feature of templates that you wouldn't get with snippets.

So where do I stand on this? I'm about as addicted to snippets as a guy can get. But the more I use templates, the more I use templates. Know what I mean?

Here's one more thing I like: the problem I have with snippets is that classic problem of "too much becomes a problem". I have many dozens of snippets. I can't possibly retain all the trigger words in my head. So I inevitably end up popping open the snippets view, hunting down the snippet in question, and then double clicking to insert.

But with Templates, if I'm at least in the ballpark of the word, then it'll suggest to me the different templates I have available.

For example, if I have 6 different templates for "for" loops, I don't need to remember "hmmm, did I have "forarray" as my snippet trigger? or was it "arrayfor"? or "looparray"? If I have them in snippets, I just start typing "for", or maybe "loop", and i'm going to get popups if they're in there. So it's a good noise reducer for me, and I'm all about noise reduction.

Again, thanks for asking! I'll see if I can get Denny to give us a better idea of where he'd like to take this feature

Denny said...

Hi Guys!

1) greater variable control. For example, if you look in that "insert variable" dropdown, you'll see stuff you don't see options like "word_selection" and "line_selection" and "dst" that you don't get with regular snippets. I believe these are not yet functional but will be useful down the road. I'll try to get Denny to come on here and explain them.

==

I'm thinking that when we add a key binding to the template assist -- something like control+space -- those selection-based options will just magically work.

Right now you can't have something selected, and then fire off the template assist, as it's going off of the current word you're typing- and if you type something after selecting, it wipes out the selection.

"dst" is the contextual variable test, basically. The options that come up there are dynamic-ish-- that's where the parser would give us some variables, perhaps (as you mentioned the java deal does).

We can add variables, like the current file name, and we can do things depending on editor context (context == within a cfscript block, or whatever we think of I think)

==

2) ability to share via "export" and "import". Currently, sharing snippets is a manual affair (unless you use ANT to copy them... check out the mxunit/cfeclipse directory in the mxunit download for all the cfe snippets that come bundled with mxunit). But with templates, you could export yours, give them to your friends/team/whomever, and they could easily import them.

3) Potential for bundling a whole slew of common templates. If you look at the window -- preferences -- java -- Editor -- Templates, check out all the templates that come with Eclipse. I can envision CFEclipse coming bundled with tons of commonly typed chunks of code.

==

The SnipEx server stuff is one thing I think is swell about Snippets, and that I'd like to incorporate into the templates somehow.

You can currently do something similar by creating a plugin that doesn't have anything besides template XML files, and then using the usual way of updating plugins to update your thusly shared templates.

I envision a plugin (or multiple plugins perhaps) of templates for the main CFE distro, and eventually adding something more dynamic, like SnipEx.

Anyways, one cool thing about the template stuff, is that it's using baked-in Eclipse functionality-- adding the feature was mostly a copy/paste type of deal.
Meaning less to maintain, and as Eclipse adds features, (hey, maybe something like SnipEx ;]) we'll be able to ride along. We should already be able to do something like "new from template" for creating whole files.

Course, I'm so lazy, I added templates for things like unit tests, so instead of "new from template", I just create a blank file and then type "mxu{pause}{enter}". Heh.

There's a freaking *ton* of improvements to be made.

I want to add smart insertion/indentation pretty soon, as that's the most annoying thing to me so far, and then the interaction between the variable assist within templates and the existing variable assist (like for tag attributes) is squirrelly as hell... that's just talking existing implementation, as feature-wise, there's just oodles of power left to get out of this stuff (as evidenced by the java editor templates-- imagine that but geared for cfml, basically =]).

Eh, even as is, I've gone the same route you've gone, basically-- never remembering snippet keys gradually had me adding more and more templates.

As an aside: noting this let me to thinking that it would also be pretty cool if we tied (or at least had an option to do so) the snippet assist into the content assist, the way templates are. God I'm lazy.

*maniacal giggle*

Ennywho, I guess that's that, for now. Marc, thanks for getting the word out, as it were, in your consistently kick-ass manner, and the chance to contribute to said kick-ass-ness. :)

|DeN