Happy programming with tips from IntelliJ IDEA (2)

foreword

The skills introduced in this article are IntelliJ IDEArelated to automatic code generation in China. Using these skills reasonably will greatly improve your coding efficiency :)

Trick 6. Use template code

idea provides many template codes by default, you can use ctrl + jto view, or use T1. command to find insert Live Template, or use T3. to set your own favorite shortcut keys

On the left is the template code trigger key, which can be triggered by using tab, as shown in the figure

There seem to be a lot of built-in template codes, but most of them are not used. Some of the more commonly used ones are as follows

trigger word meaning
psvm main method
salt System.out.println()
souf System.out.printf()
holes Create a positive loop
ifn Create an if(something == null) block of code
fixme Add a fix mark to the cursor
everything Add todo marker at cursor

Trick 7. Custom template code

The template code code provided by the system is good, but it is not enough. It doesn't matter, IntelliJ IDEA supports custom template code.
Use the T1. command to search Live templatesand enter the editing page. The specific editing details are as follows

Using the skills of custom templates, you can define your own commonly used code blocks. For example, the following figure customizes a code block that prints out the key-value pairs in the map, and organizes the code that you often write by template, which can effectively Improve your coding efficiency :)

Trick 8. Generate code using postfix expressions

I don’t know if you have encountered such a situation. For example, you are writing a chain of code or variables, and it is written that this variable needs to be printed/null value judged/various operations on variables. At this time What will be done? Select and cut such a piece of chain code sout, and then paste it? This kind of operation is obviously unpleasant, and at the same time, it is not in line with the thinking of normal people. Just imagine how good it would be if I was direct at this time .sout! Yes, you really should do this at this time, this is the suffix expression of idea.

As shown in the figure above, although the code in the above figure is meaningless, and the compilation cannot pass (in fact, it was written by me), this series of packaging operations are extremely smooth, and they are all done through suffix expressions. No more copy-paste :) Obviously this way of coding is very pleasing.

Trick 9. Custom Postfix Expressions

Similar to T7., we can also define our own suffix expression, and use the T1. command to search postfix completionto enter the setting of custom suffix expression (this function is only available in idea 2018.1 and later versions), the setting interface is as shown in the figure

For example, in the example in the figure above, I set up a suffix expression for converting an object to a json string. The applicable types are all types, and the trigger keyword is .json, so it is much more convenient every time you want to convert it to a json string. As shown

As mentioned above, this function is integrated in the version after idea2018.1. If your version is lower than this version and also want this function, you can use T2. Search plugin Custom postfix Templatesor click here is the github link to download, after downloading t1. Command to search for custom postfix to enter the setting page. For the specific editing method, you can check the github link, which readmeis similar to the one integrated with the idea above.

10. Use alt+insert to insert code

Search in T1. command search Generateor press the default shortcut key in the page to alt+insertautomatically generate some codes, including common getset 构造器 hascode & equals spring自动注入 版权信息etc.

It is also worth mentioning that such automatically generated code also supports customizing some templates ( velocitywritten in dsl), such as setter methods of chain calls, different styles of toString&equals&hashcodemethods, but because there are not many cases of application, here is a brief Skip it, interested students can search by themselves.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292138&siteId=291194637