DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 3.19 Customizing Code Assist

3.19.1 Problem

Code assist is great, but some of its features don't fit your coding style.

3.19.2 Solution

Customize code assist by selecting Window Preferences Java Code Formatter.

3.19.3 Discussion

One common issue with code assist is that it puts curly braces on the same line as other code.

public void display( ) {
System.out.println("No problem."); }

Some programmers, however, prefer that curly braces appear on separate lines:

public void display( ) 
{
System.out.println("No problem."); }

You can customize the location of curly braces by selecting Window Preferences Java Code Formatter, as shown in Figure 3-21. Here, check the "Insert a new line before an opening brace" checkbox, as shown in the figure; the sample code below will change to match.

Figure 3-21. Customizing code assist
figs/ecb_0321.gif


You also can create new code assist items in Eclipse. Say you want to create a shortcut to print the current date. To do that, select Window Preferences Java Editor Templates. In this case, we're going to create a new shortcut named datem to print the date, as shown in Figure 3-22. We'll use the code:

System.out.println("${date}");

to print the date.

Besides ${date}, you can use other terms, such as ${cursor}, which indicates where to position the cursor after the insertion has been performed. When you type ${ in the New Template dialog, code assist displays the possible values you can use in code assist expressions.


Figure 3-22. Creating a new shortcut for code assist
figs/ecb_0322.gif


Now when you type datem in your code and press Ctrl-Space, code assist will enter the code needed to print the current date, as shown in Figure 3-23. In fact, if you type d and then press Ctrl-Space, code assist lists all options that begin with the letter "d," including datem.

Figure 3-23. Using the new code assist shortcut
figs/ecb_0323.gif


You can customize code assist in other ways as well. For example, when you create a new Java code file, the following comment is inserted automatically:

/*
 * Created on Feb 4, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

You can change the code in this comment by selecting Window Preferences Java Code Generation Code and Comments Code New Java Files, as shown in Figure 3-24. Then simply edit the template used to create this comment so that it reads the way you want it.

Figure 3-24. Configuring code assist comments
figs/ecb_0324.gif


You also can customize code assist by specifying the prefix or suffix for fields, static fields, parameters, and local variables that code assist will create. Select Window Preferences Java Code Generation Names, and then set the prefixes and suffixes for variables used in code assist, quick fix, and refactoring.


3.19.3.1 Eclipse 3.0

As you might expect, in Eclipse 3.0 you can customize code assist in numerous additional ways. For example, you can customize getter/setter code, as well as the comments used when a new field is inserted.

    Previous Section  < Day Day Up >  Next Section