Intelli IDEA official document

IDEA is a well-known Java integrated development environment, its function is very powerful, today to introduce you to the creation and use of IDEA template.

The template function of IDEA allows us to define abbreviations of commonly used statements in development. IDEA automatically helps us expand according to the abbreviations of the template, and its effects are as follows:

Classification of templates

  • Simple template: It just sets up some fixed plain text content. This template is simply an abbreviation substitution. For example, if you type PRSF in the code and press Enter, IDEA will automatically replace it with a private static final statement.

  • Parameterized template: it can set some variables so that we can replace them with input variables we want. For example, if we type fori in the code and press Enter, IDEA will automatically replace it with the following code:

    for(int i = 0; i < ; ++i) {
    
    }
    Copy the code
  • Surround template: You can select a piece of code as the implementation part of the template. For example, if you type C in IDEA and press Enter, IDEA will automatically replace it with the following code:

    Callable<Object> callable = new Callable<Object>() {      
        public Object call(a) throws Exception {}};Copy the code

    The Surround template also allows you to select a piece of code by pressing Option + Command + T. The Surround template places the selected code in the body of the Call function.

    Callable<Object> callable = new Callable<Object>() {
        public Object call(a) throws Exception {
          // The code segment you selected}};Copy the code

Template creation

Go to the IDEA Preferences -> Editor -> Live Templates or use the shortcut command +,(comma), select Java, and click the “+” symbol in the upper right corner to add a custom template.

In Abbreviation, write your Template Abbreviation, in Description write your Description of the Abbreviation, and in Template text write the extension code for the Template (Abbreviation code you want to replace). In addition, select an application under Template text to indicate the application scope of the Template. Common application scopes include Declaration and Statement.

In the bottom right corner of the create template there are a few options to check:

  • Expand With: What key do we need to press to Expand With
  • Reformat According: Whether to set the style of the extended code according to the style
  • Use static import if possible: Use static import if possible
  • Shorten FQ Names: Indicates that fully qualified names (package name + class name) can be truncated and the required packages imported automatically. For example, if we write java.util.List in Template text, the extension automatically truncates the package name, leaving only the class name, and automatically imports the java.util package

For the Surround and parameterized templates, you can set a number of variables. In general, these variables can help you position yourself during the deployment. You can also set the default values for variables. To distinguish the variable from the code, it is surrounded by two $signs. After expansion, the cursor will automatically stop at the position of the first variable for us to Enter the code we want to Enter, and press Enter to automatically jump to the second variable. IDEA has two predefined variables:

  • $END$: indicates that the END is reached. If you press Enter, the jump will not continue.
  • $SELECTION$: Used in the Surround template to represent the selected code segment.

In addition to the above two variables, you need to define the other variables.

By selecting a Template in Live Templates and clicking Edit Variables in the lower right corner, the Edit Template Variables dialog box will appear, where you can set the variables used by the Template.

The Edit Template Variables dialog box has four fields:

  • Name: The Name of the variable
  • Expression: Expression. This field is used when defining more complex templates. IEDA provides some functions for us to use, which will be described later
  • Default value: indicates the Default value of a variable
  • Skip if defined: Skip options if defined

Once the variable is defined, it is ready to use in template text.

There are 58 functions available in the IDEA template variable. Here are some common ones:

  • SuggestIndexName () : Returns a suggested IndexName from an iteration, I,j,k… suggestIndexName() Turn to the back. For example, when we define a template that uses loops, we can use this function to generate suggested index subscripts when defining variables
  • SuggestVariableName () : Set a suggested variable name based on the type of the variable based on the code style you set up
  • ArrayVariable () : Returns the most recently declared array in the current range. If there are more than one array, the most recent array is selected by default, but other arrays are listed for you to select when changing the variable
  • ComponentTypeOf () : Returns the data type in an array, which is the name of the array variable we define, for example, using arrayVariable()
  • For additional functions, check out the official IDEA documentation provided at the beginning of this article.

Sharing templates

If you want to share your templates with others, you can find the group XML file in templates under the configuration path of IDEA (~/Library/Application Support/JetBrains/) and share it with others.

Note: The operations in this article are based on the Mac system. For other systems, you can go to the official website to view the configuration path of IDEA