[ajug-members] Eclipse 'snippets' plugin = ?
James Mitchell
jmitchell at apache.org
Thu Aug 3 14:42:20 EDT 2006
On Aug 3, 2006, at 12:18 PM, Howard Kapustein wrote:
> 1. Can I pick and choose 'templates' for a hierarchial list (some
> panel
> in the IDE with groupings)? See Exadel and MyEclipse.
>
Still not sure what you mean here. Can you provide a link to a site
with a screenshot?
> 2. Can the IDE prompt for template substitutions before injecting the
> text, e.g.
>
> private static final Log log = ${factory}.get(String $
> {name}, int ${id});
>
> And the user's displayed a dialog prompting for values for 'factory',
> 'name' and 'id'? See Exadel and MyEclipse.
>
Oh, Eclipse does it better. There is no need for dialogs. The code
is injected and every replacement that you are supposed to provide
has a smart (see below) pre-filled-in value with a small box around
them so you can see immediately which values can be replaced. At
this point, you are not in normal free-text edit mode, you fill in
values or tab to a different box. As you type, the replacement
values are written, then you can tab to the next one. You can tab
backwards and forwards from box to box.
When you are in one of those "boxes", hitting enter doesn't key a new
line, but it completes the line and takes you out of that special
mode. This means that if you type sysout<ctrl+space>, it writes
System.out.println([cursor here]); <- see where the cursor is? Then
type something and hit enter. You are taken to the end of the line
and puts you back in free editing mode. Hitting escape also changes
you back to regular editing mode without moving the cursor.
Also, if you need some value more than once in the template, and it
should be the same for each one, such as a variable name like in a
for loop iteration, it is handled for you. As you type, each box
shows the same value.
If you decide you don't need the template, ctrl + z (undo) or ctrl +
d (line deletion).
> 2a. Can I mark these as optional vs. required and omit any optional
> ones
> not valued?
>
Not sure why a template/snippet would need optional values. I mean,
if the default value is good enough, just tab right through.
> 2b. Can I provide a small descriptive help for the prompt dialog
> guiding
> the user what to enter for a value?
>
Yes, that's the 'description' in the create dialog (from Window-
>Preferences, Java->Editor->Templates)
> 2c. Can I provide a default value for the fields, so the user can
> accept
> them or change them and not be forced to work with an initially blank
> prompt they MUST enter something?
>
There are defaults. In fact Eclipse provides smart defaults. So if
your snippet needs a Collection, it will look above in *your* code
for any declared Collections by scope, closest to farthest, and will
use that variable, which you can change or leave it.
> 3. Can I abort the process before completion and no code is changed?
>
> - Howard
See above for ctrl+z or ctrl+d.
When you create the template, you can specify whether it is
automatically added or not. This is when there is nothing else that
matches what you've typed when you press ctrl+space. Also, like all
Eclipse preferences, you can import or export your custom templates.
So you can provide them to your team or keep them around for when you
upgrade Eclipse. Same for snippets.
(Did someone say smart refactoring?)
One slightly unknown feature in Eclipse is the 'Extract constant'
feature. There isn't a default key binding for it (at least not on
the Mac), but that's easy to fix. I set mine to ctrl+alt+. (period)
This cool feature let's you highlight any java code that can be
assigned to a variable and creates a private static final for you, by
type and is even smart about it. So:
...
... (down in some method)
...
String smart = "smart defaults";
...
...
// ======= becomes ========= //
class Whatever{
private static final String SMART_DEFAULTS = "smart defaults";
...
...
String smart = SMART_DEFAULTS;
...
...
By the way, there are templates available for different editors (Ant,
CSS, DTD, XML, JSP). All the standard java editor templates also
work in Javadoc comments. Eclipse even provides a few for you.
Check it out.
--
James Mitchell
678.910.8017
More information about the ajug-members
mailing list