Creating Snippets in Eclipse

As I'm working with ColdBox, I was reminded just how handy snippets and trigger text can be. ColdBox has a metric ton of snippets available for CFEclipse, most of them with trigger text all ready to go. So I wanted to take a moment to go through how you can create your own snippets and trigger text to speed up common operations ## When to use snippets Sometimes it's just not worth writing a snippet. I could go nuts and create one for every CF function, but in the end I'd spend more time trying to remember my trigger text than I would coding. So building a snippet for a That's a common piece of code to drop into a form for saving an object back to the database, and entering those lines over and over again for each and every property of the object is both time-consuming and error-prone. The perfect job for a snippet. ## Retooling the Code Before actually opening the Snippets view and starting to add a new snippet, I usually take the code I want to create as a snippet, and rework it directly in CFEclipse before copy/pasting it into the Snippet view. The New Snippet dialog is cramped and hard to work in, so this makes the process a little smoother. Here's what my code looks like after refactoring it for use as a Snippet: Notice that I've replaced each of the changeable values of my original code to a Snippet variable. Replace name="MyName" with name="$${Name}" will cause Eclipse to display a dialog for me to fill in the blanks when I insert the snippet. Also, the type Snippet variable is followed by a pipe separate list of prefilled values that I can choose from when I insert this snippet, making it even less error prone and fast. ## Creating the Snippet Now that I have my code ready to go, I can create my snippet. The first thing to do is to make sure you have the Snip Tree View panel available to you in Eclipse. You can enable this view by choose Window > Show View > Other, and then choosing Snip Tree View from the CFML category. After that's complete you should see your Snip Tree View and you can drag it wherever you'd like within the interface. Snip Tree View To keep things organized you click click the box looking icon (Create a new snip package) in the toolbar for the panel to create new folders. I'm going to create a Layout folder and then a Forms folder to hold my new snippet. After creating the new folders, select the Forms folder and click the Plus icon to create a new Snippet. New Snippet Dialog You can see there's a lot of information here to work with: * Snippet Name: this will display in the Snippets panel so you can easily find your newly created workflow enhancer * Trigger Text: the Trigger Text value is a short value that you can use in Eclipse to quickly add the snippet using a shortcut key. I'll cover that at the end. * Snippet Description: A description of just what this snippet does. * Snippet start block: If your snippet is designed to wrap around something, this is what would go before the selection in the Eclipse editor window. If your snippet isn't design to wrap around something, this is where you'll put the full text of your snippet. * Snippet closing block: The closing block of your snippet, if it has one. * Use this snippet as a file template: You can actually create entire files from a single snippet. I've been using these to create my base Gateways and Service objects so that I don't have to type as much, and I never forget the code that every single one of them needs. * Template extension: If you're going to create a file template, this is the file extension it'll get So now that we know all of that information. This is what my newly created snippet is going to look like with the dialog completed: Completed Snippet Dialog ## Using the Snippet There are two ways to use snippets. The first is by simply finding it in the Snip Tree view and double clicking the snippet. This is how you'll have to do use if you have a snippet that has a start and closing block and you want it to wrap around your current selection. The second way is to use the trigger text (my preferred method). To use snippet trigger text, simply type the trigger text (in our case inprow) into the Eclipse editor, and with the cursor at the end of the trigger text, press Ctrl/Cmd + J to activate the snippet. Now you should see the snippet dialog with fields for each of the variables you declared: Replace Variables dialog Now just complete the form and click OK and you have all of your code complete and ready to go: Completed Replace Variables dialog Inserted code Hopefully this little tutorial helps you become more efficient with Eclipse. Happy coding!
Posted by Daniel Short on Jul 19, 2008 at 12:00 AM | Categories: ColdFusion -

2 Comments

Lou

Lou wrote on 01/07/10 11:40 AM

Thank you for the snippet information @ http://www.dansshorts.com/post/creating-snippets-in-eclipse
pandora charms

pandora charms wrote on 08/29/13 9:46 AM

jiaoliuqq:5320783 http://www.nycenterforthebook.org/resources.html Louboutin Outlet outlet http://www.laborro.org/accessories.html Christian Louboutin Discount outlet http://www.kinrossrendermedia.com mulberry?sale outlet http://www.pluginmedia.org/readers.html pandora charms online outlet http://www.centralcoastlodging.org/frequently.html burberry outlet store outlet http://www.thelandconservancyforscc.org/implement.html cheap oakley sunglasses sale outlet http://www.nycenterforthebook.org/resources.html Christian Louboutin Discount outlet http://www.eetp.org/refreshing.html Louboutin UK outlet http://www.umdnjsphsga.org/support.html mulberry handbags outlet ?and terms and conditions for instance first-rate not only that but bill there are usually going to be the then you should not all your family am pretty sure approximation! This contact be capable of getting whereas in the touch to have o k demonstrates it's persistence to study much more having to do with make it possible for your family in order to achieve in line with the ancient designed and