Introduction
Today I want to give a small review-tutorial of templates in the JSR from Jetbrains. The following examples will be implemented in Java in Intellij IDEA, but the creation mechanism is the same for other languages and products of the company. I think everyone programming in this IDE used built-in templates. For example, when you write fori, press TAB or ENTER and you have a for loop in which you need to specify the variable name and condition, and the rest of the code is generated for you.
for (int i = 0; i < ; i++) { }
Or sout -> System.out.println (), which is familiar to all java programmers. So, in Intellij there is support for creating your own code templates. Surprisingly, I didn’t find almost any information in Russian and quite a bit of English (well, there is good
documentation ) when I came across this feature, although it speeds up a lot and helps in development. And now, when the hands reached, I decided to write a small article about it. Perhaps it will be of little help to experienced programmers, they themselves will be able to understand everything, or they have been using it for a long time, but for everyone else I think it will be fine. So let's get started.
Creating a template
First, let's make a simple example, for example, a logger constant. Go to
Settings -> Editor -> Live Templates . Here you can view already prepared groups of templates and create your own by clicking on the plus sign on the right side of the panel. You can also create a separate template, then it will be added to the user group. After clicking you will have a window at the bottom in which you need to enter an abbreviation, descriptions and the actual text of the template itself. There will also be a warning label "
No applicable context " and the
Define button next to it, by clicking on which we will see a list of possible contexts - HTML, XML, Java, Javascript, CSS, and so on. Some have sub-clauses, for example, in Java it is possible to use a template in a method, in a comment, at the class level, somewhere else or everywhere at once. For example, we’ll select the entire Java package.
As an abbreviation, we write log, as a test, here’s a line like this:
private static final Logger LOGGER = Logger.getLogger($CLASS_NAME$.class.getName());
Next, click on the button which is located on the right side -
Edit variables . In the opened window we see a table with one entry - CLASS_NAME, which corresponds to the variable that we specified in the text of the file as the name of the class. It has fields
Expression ,
Default value ,
Skip if defined . The first field is a select in which we are offered all sorts of values that can be substituted. The second is just a string in which we can write anything by quoting it. Well, the third field suggests us to skip editing the variable if it is set in one of the first properties. In our case, we need the value of the
className () from the
Expression field. If you wish, you can choose to skip editing. Click
OK , after
Apply and you can check. Create a class with any name, write inside log, press
TAB and voila - we have a logger with which instead of CLASS_NAME the name of the current class.
Example:
public class CodeTemplates { private static final Logger LOGGER = Logger.getLogger(CodeTemplates.class.getName()); }
Features and capabilities:
1. When creating templates, there is a
$ END $ key variable indicating where the cursor will be when you create a template (the default is at the end of the template code).
System.out.println($END$);
2. The second key variable is
$ SELECTION $ . If it is present in your template, it will appear in the
Surround With menu (
Ctrl + Alt + T ). This is necessary if you want to enclose your code in a wrapper.
System.out.println("$SELECTION$");
T e let's say you have a word or sentence, for example TEST - you put the cursor at the end of the word, press Ctrl + Alt + T and you can choose your own among the templates, and then you get:
System.out.println("TEST");
PS: for a block of code or text, just select it first.
3. Some of the extensions in the Extentions may take values of various types in the arguments, including other extensions. To make it clearer give an example. Perhaps many are familiar with the JPA
Table annotation, which can include, among other things, the name of the table corresponding to a given class (entity). Usually the names of the table and class are the same, with the only difference that the className becomes CLASS_NAME. The template for creating this annotation will be something like this:
@Table(name = "$TABLE NAME$")
In Extentions, we select
capitalizeAndUnderscore (String) , only instead of String we prescribe the className ourselves:
capitalizeAndUnderscore(className)
Example:
@Table(name = "CODE_TEMPLATES") public class CodeTemplates { ... }
PS:In the Extentions field, you can also set your own values, as in the input, but I don’t see the point, because you can do this in the Default value field.
4. It is also possible to import (
File -> Import Settings ) and export (
File -> Export Settings ) templates and other settings as jar-files. If the templates are exported as xml files, then, if you wish, you can go to the .IntelliJIdea $ VERSION $ / config / templates directory and add your xml there. In this case, it is necessary that the group of templates be indicated in the file, otherwise you will not see them in the list. If you do not specify a group, then individual templates can be added to one of the existing files, as an option - user.xml.
Example user.xml with logger and
Table :
<templateSet group="user"> <template name="log" value="private static final Logger LOGGER = Logger.getLogger($CLASS_NAME$.class.getName());" description="Logger Template" toReformat="false" toShortenFQNames="true"> <variable name="CLASS_NAME" expression="className()" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_DECLARATION" value="true" /> </context> </template> <template name="tb" value="@Table(name = "$TABLE_NAME$")" description="" toReformat="false" toShortenFQNames="true"> <variable name="TABLE_NAME" expression="capitalizeAndUnderscore(className)" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_CODE" value="true" /> </context> </template> </templateSet>
Conclusion
I hope today's article will be useful and will help in speeding up the development, for those who have not yet used their templates.
PS: If you wish, you can write your own plugins for the IDE, including extensions for templates.
Corrections or additions are welcome.
Link to official documentation:
HEREJustboris thanks for the addition