Meet and hate late IDEA usage skills, which can make your code fly

What is Live Templates , it sounds quite mysterious. Some students feel that it is really easy to use after using it. It cannot be said that the development efficiency has been greatly improved, at least it is a small improvement, which saves a lot of time typing and repeating code. Some students said after using it: It's useless, it's just a trick.

Even if you haven’t heard of this concept, you may have used it more or less. Even if you haven’t used it, you may have seen some. If you haven’t eaten pork, you may have seen pigs running, like the following operate.

To sum up, it is similar to a shortcut command (magic code). As long as you enter a specific letter combination in IDEA, IDEA will help you insert a pre-defined code at the current input position. The code can be fixed , you can also define some context variables, which are suitable for those commonly used and frequently used declarations or repeated code segments.

For example, to declare static  final ,  Stringwithout using Live Tmeplates, we will manually type out the line of code "public static final String" in IDEA one by one. Of course, it is possible to use IDEA's only prompts, and only need to type the first two for each word. letter. In the case of using Live Tmeplates, we only need to type  psfsthese four letters, and then press the Enter key, IDEA will help us  psfsinsert the line "public static final String" at this position.

Students who have never used it, should you quickly open IDEA and try it out? In the setting interface, enter  Live Templates, and then find the built-in templates on the right  Java. Not only Java, but also support for various file types, such as SQL, JavaScript, JSP, Kotlin, etc.

The following introduces several built-in and commonly used templates in IDEA.

Commonly used Live Templates

main and psvm

The main method is inserted. Although we can blindly type the following code proficiently, wouldn’t it be faster to only type four letters?

public static void main(String[] args){
}

variable declaration

The following are some commonly used variable declarations

psfs

public static final String 

psfi

public static final int 

prsf

private static final 

St

String 

console output

The following is the console output, and there are some, not listed one by one.

salt

Text output, the most commonly used.

System.out.println();

breath

Formatted text output.

System.out.printf();

loop iteration

fori

After inputting, press Enter, the cursor will be at  i<the position, waiting for the input of the critical value.

for (int i = 0; i < ; i++) {

}

iter

Loop with forEach, used below an array or list variable.

List<String> array = new ArrayList<>();
for (String s1 : array) {

}

itco

Iterating with an iterator is also used under a list variable.

List<String> array = new ArrayList<>();
for (Iterator<String> iterator = array.iterator(); iterator.hasNext(); ) {
  String next =  iterator.next();
}

Surround Templates

This kind of template is to select a piece of code, and then use the shortcut key  option+ command+ jto bring up the prompt box, and then select a template type.

This shortcut key is under the MAC system. If you are using windows, you can click  codethe menu item to find  Sorround Withand see what the shortcut key is.

C

achieve a Callable

Callable<Object> callable = new Callable<Object>() {
  public Object call() throws Exception {
    System.out.println("hello");
  }
};

RL and WL

Insert a read lock or write lock plus unlock code.

// 要先声明一个读写锁实例
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
readWriteLock.readLock().lock();
try {
  System.out.println("hello");
} finally {
  readWriteLock.readLock().unlock();
}

Custom Template

Sometimes we often use a similar piece of code with high-frequency words. For example, students who do network development often use Socket-related initialization codes.

For some similar code segments that we often write, we can extract a Template for our convenience.

Next, I will implement a custom Template to see if it greatly simplifies repetitive labor. Some students may think that it doesn't matter, and copying and pasting is also quite easy. Different people have different opinions, you can try it if you think it is useful, and it doesn’t matter if you think it is tasteless, anyway, it will not affect the final function realization.

For example, I am currently doing some system optimization work. During this process, I will frequently use it  commons-lang3to  StopWatchsee the execution time of certain methods or certain code segments. For example the following code segment:

StopWatch stopWatch = new StopWatch("代码段");
stopWatch.start();
try {
  Thread.sleep(1000);
} catch (InterruptedException e) {
  throw new RuntimeException(e);
}
stopWatch.stop();
System.out.printf("执行时间 %s%n",stopWatch.toString());

Since I'm lazy, I don't want to type the duplicate content every time, I don't even want to paste and copy. So I thought of Live Template.

1. Open the settings window of IDEA, find under Editor  Live Templates, find it on the right  Java, and click the plus sign on the far right.

2. Enter the name of this Template, and then you can insert this template by entering this name in the editor.

Enter a description to help us remember.

Finally enter the content of the template.

I named this template  watch, and the content of the template is as follows:

StopWatch stopWatch = new StopWatch("$MESSAGE$");
stopWatch.start();
$SELECTION$
stopWatch.stop();
System.out.printf("执行时间 %s%n",stopWatch.toString());

$SELECTION$Indicates the selected part. What we want to monitor is just a certain method or a certain code segment, so Surround Templates can be used, and if it is used in the content  $SELECTION$, the template will default to Surround Templates.

$MESSAGE$It is the function of a placeholder. When we insert this template, the cursor will be positioned on this placeholder, and we can enter the content we want where we need to customize. Here, since multiple StopWatches may be used in one of my methods, I just take a place here and give different StopWatches different names.

Variables supported by Live Template

In some cases, what we want to insert is not just a fixed code segment, but some context-related content, such as the input parameters of the current method, such as the current class name, such as the current method name, etc. Use the following example to illustrate.

There is a built-in template called  soutm, and its description is as follows: Prints current class and method names to System.out, print the current class and method names, let's see the effect, directly fill the current class and method names into the parameter  Study.mainpositions println.

The content of the template is like this, in which two variables are used to represent the current class name  $CLASS_NAME$and the current method name $METHOD_NAME$.

System.out.println("$CLASS_NAME$.$METHOD_NAME$");

More available variables can be viewed on the IDEA official website. After seeing these variables, it may open your mind and further improve your development efficiency. But it doesn't matter if you don't need these variables, the above is actually almost enough.

Official website address: https://www.jetbrains.com/help/idea/template-variables.html#predefined_functions

Guess you like

Origin blog.csdn.net/weixin_45740811/article/details/128642213