[AI helps me write code, it is not a dream to fish at work] Hand-to-hand illustration of the installation and use of CodeWhisperer

IDEA plugin

In addition to using ChatGPT to generate code through question and answer, you can also use the IDEA plug-in to directly help us generate code when writing code.

Currently, IDEA plug-ins include CodeGeeX, CodeWhisperer, and Copilot. Among them, CodeGeeX and CodeWhisperer are completely free, and Copilot is charged at $10 per month.

Let's learn about the installation and use of CodeWhisperer. If you want to know more, you can tell me in the comments.

Introduction to CodeWhisperer

CodeWhisperer is a general-purpose machine learning-based code generator produced by Amazon that provides code suggestions in real time.

As you code, it automatically generates suggestions based on your existing code and comments. It gives you personalized advice of all sizes and scopes, from one-line code suggestions to complete functions.

CodeWhisperer can also scan your code to highlight and define security issues.

CodeWhisperer provides IDEA plug-ins, which are currently free to use and relatively easy to use.

Install CodeWhisperer

Open the configuration window in IDEA, select Plugins, search for "AWS Toolkit", click Install, and click the OK button, as shown below:

Restart IDEA after installation, as shown below:

Reminder: If you cannot find the plug-in, please upgrade IDEA to a newer version. Pro-test is that IDEA 2022 can be installed.

Open the AWS Toolkit view (menu View/Tool Windows/AWS Toolkit), click the "Developer Tools" tab page, and select "CodeWhisperer/Start", as shown below:

Select "Use a personal email to sign up and sign in with AWS Builder ID" in the pop-up window, and click the "Connect" button, as shown in the figure below:

In the pop-up window, select "Open and Copy Code", as shown below:

At this point, a page will be opened in the browser, press ctrl-v to paste the code value, and click "Next", as shown in the figure below:

Enter the email address and click "Next", as shown below:

Enter the name, click "Next", and CodeWhisperer will send a verification code to the mailbox, as shown below:

Open the mailbox, you can see the verification code, as shown below:

Copy the verification code, paste it into the input box, and click the "Verify" button, as shown below:

Set a password and click "Create AWS Builder ID", as shown below:

Click the "Allow" button on the last page, as shown below:

After the following prompt appears, it means that the registration of AWS builder ID is successful, as shown in the figure below:

Back to IDEA, the code generation function can be turned on or off in the Developer Tools in the AWS Toolkit view, as shown in the figure below:

Using CodeWhisperer

Create a new class and write comments for bubble sorting. The code is as follows:

package one.more;

public class SortUtils {
    
    
    /**
     * 冒泡排序
     */
    
}

You can see that there is a circle in front of the CodeWhisperer icon in the lower right corner, indicating that the code is being generated, as shown in the figure below:

After the code is generated, the following interface will appear:

It can be seen that CodeWhisperer has generated the code. At this time, you can press the Tab key to confirm the code, or you can click "Previous" or "Next" to switch between different codes, and finally press the Tab key to confirm.

After the function name is generated, the function body can also be generated, as shown in the following figure:

Ultimately, the generated code looks like this:

package one.more;

public class SortUtils {
    
    
    /**
     * 冒泡排序
     */
    public static void bubbleSort(int[] arr) {
    
    
        for (int i = 0; i < arr.length; i++) {
    
    
            for (int j = 0; j < arr.length - i - 1; j++) {
    
    
                if (arr[j] > arr[j + 1]) {
    
    
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

The more precise the comments, the better the quality of the generated code. Of course, the code generated by CodeWhisperer is not always correct or optimal, and needs to be modified or optimized according to the situation.

Guess you like

Origin blog.csdn.net/heihaozi/article/details/129582762