Qualified programmers are good at using tools, is the so-called gentleman is not different also, good at fake things.

Use automation tools to reduce your workload and increase your productivity. In everyday programming, we often need to write repetitive snippets of code, such as

private static final Logger LOGGER = LoggerFactory.getLogger(HashServiceImpl.class);
Copy the code

Is there a way to quickly generate this code, which requires typing a lot of keys each time you write it? Similarly, how do you save commonly used code snippets in a fixed format and then quickly generate them when needed? IDEA’s Live Template is a viable approach.

I have only recently begun to use the Live Template function of IDEA. Although I knew about this function before, I did not form the habit of using it. Recently, I have been reviewing and rethinking my programming, work and life habits, and found that there are many areas that can be improved.

This is also the “Programmer training way” said Think! About Your Work.

IDEA is a powerful programming tool, and learning to use it can greatly improve productivity and focus your energy on more important things rather than wasting time writing repetitive code.

As a Java programmer, the frustration is that Java development often involves writing code that has a fixed format, such as declaring a private variable, Logger, or Bean. For this kind of small-scale code generation, we can take advantage of the Live Templates feature provided by IDEA.

The Live Template is not a simple Code Snippet, it even supports Groovy function configuration, can write some complex logic, support very complex Code generation.

The basic use

IDEA comes with many commonly used dynamic templates, which are commonly used statement formats when coding. Take the following four giFs for example.

The four diagrams are declaring static String member variables, nulling strings, for loops, and printing function arguments.

The custom Template

Open the configuration page and enter the Live Template TAB, where we can see the Template configuration of IDEA pre-set. These templates are some of the most commonly used statements, so let’s first look at how they are defined.

Abbreviations are aliases for templates identified by IDEA, as shown at the beginning of this article when you type soutm, IDEA automatically identifies that template.

The application context indicates the context in which the template is in effect. For example, a System. Out statement is only valid in the body of a Java function, so its context is set to Java: Statement. This template cannot be used in other types of files or in member variable declarations in Java files.

Template content is the content that IDEA automatically generates when you press Tab. It usually consists of two parts, plain text and parameters. Parameters can be value bound and support automatic cursor jump. As shown above, $CLASS_NAME$and $METHOD_NAME$are arguments, and $END$is a special argument that indicates where the cursor last jumped.

Parameter Settings are all about setting the values of these parameters, using some of the built-in functions provided by IDEA, as well as powerful Groovy scripts. Check out the details of these functions on the IDEA website.

Let’s cover the use of groovyScript(” Groovy code”, arg1). It gives you everything you need, including the ability to execute Groovy scripts to process input and then output the processed strings

groovyScript("code",...). | code | a Groovy code or the absolute path to the Groovy script code | |... | can be selected to join, these parameters will be bound to a ` _1, _2, _3,... _n ', used in Groovy code. |Copy the code

For example, the template for printing function parameters was defined like this.

groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
Copy the code

MethodParameters is a built-in function of IDEA that returns the result as a parameter into Groovy’s script, generating a string that prints the parameter function.

Afterword.

Thank you for reading, and I hope you’ll continue to follow us, as well as share your favorite programming tools and tricks in the comments.

Original text of wechat blog