My Eclipse Templates
One indispensible feature of Eclipse is their templates. Essentially,
these are canned macros that can be inserted into your editor with parameters
that are substituted.
For instance, while editing a Java file, type for
and hit Control-Space
and
the content-assist menu will appear. In this menu will be a number of
templates that begin with the word for
. Select the first one (probably
for - iterate over array
and hit Return. You will end up with something like
the following:
for (int i = 0; i < array.length; i++) {
|
}
Where the i
and the array
have boxes around them. Change the i
to something
like index
and hit the tab, and you can then change the array
parameter.
Hitting the tab key again, will put you inside the loop in order to finish
the loop. Yeah, pretty slick.
But you can customize these templates. To do this, go under Window -> Preferences
and then select Java -> Editor -> Templates
. Here you have a list of all of
the templates available. Click the New…
button to add in your own.
Allow me to give you a small collection of the templates I always add
to each new workspace (oh yeah, they are saved per workspace, so if you change
workspaces, you'll want to do an Import/Export of these).
Name: FormatMessage
Description: General purpose template for getting a string based on "parts'
Pattern:
final Object[] parms = { ${parameters} };
final String message =
MessageFormat.format( "${message}", parms);
Anything listed as ${…}
is a "parameter" and essentially becomes a
variable that can be used through-out the rest of the template. Take a look
at the details of the for -iterate over array
template, and you'll see what
I mean.
In my FormatMessage
template, both the parameters
and the message
are
not so much variables as they are hints as to what you add. Typically, I use
this template to create things like:
final Object[] parms = { file, dir };
final String message = MessageFormat.format
("The file, {0}, is not contained in {1}", parms);
throw new IllegalArgumentException(message);
Name: copyright
Description: Adds a GPL comment.
Pattern:
/*
* Copyright (c) ${year}, Howard Abrams
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
* http://www.fsf.org/licensing/licenses/lgpl.txt
*/
This template makes it easy to add a copyright to an already created
file.
Tell others about this article: