3.19. Customizing Code Assist

Problem

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

Solution

Customize code assist by selecting Window Preferences Java Code Formatter.

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.

Customizing code assist

Figure 3-21. Customizing code assist

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.

Tip

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 ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.