Using CodeFuse in Visual Studio Code

Visual Studio Code is a popular code editor among programmers and occupies a mainstream position in front-end development and various scripting language development. CodeFuse intelligent R&D assistant has specially developed a plug-in for VS Code. You can use CodeFuse as long as the plug-in is installed. Various functions provided, let’s take a look at how to use the CodeFuse plug-in in VS Code?

CodeFuse official website: codefuse

CodeFuse currently supports installation in 10 IDEs, including Alipay Mini Program Cloud Development, Visual Studio Code (hereinafter referred to as VS Code), and 8 IDEs in the JetBrains series, namely IntelliJ IDEA, PyCharm, WebStorm, GoLand, CLion, DataGrip, PhpStorm and RubyMine. This article explains how to install and use the CodeFuse plug-in locally in VS Code.

Note: Currently, the CodeFuse plug-in is only supported in versions above VS Code 1.75.0.

Prerequisite

Before installing the CodeFuse plug-in, you need to download and install Visual Studio Code.

Install plug-in

Currently, the CodeFuse plug-in in VS Code only supports downloading the plug-in installation package for installation. After completing the installation, you need to complete the login and pass the application to use the plug-in. The steps to install the plugin are as follows.

Note: The CodeFuse plug-in installation package currently available for download from the official website is a beta version, so stay tuned for the official version.

  1. On the CodeFuse official website, download the Visual Studio Code plug-in installation package.

  2. Open VS Code, click the icon in the left navigation bar of the editor, then click the … icon, and select Install from VSIX ….
  3. Select the downloaded CodeFuse-x.x.x.vsix file and click Install.

  4. In the left navigation bar of the IDE, click codefuse to enter the plug-in panel.
  5. In the plug-in panel, click Login.

  6. In the pop-up window, click Open, then use Alipay to log in to the CodeFuse official website and apply for a trial.

    You can choose one of the two application portals below to apply.

    • Application entrance 1: Apply for trial on the homepage of CodeFuse official website.

    • Application entrance two: Apply for experience in the plug-in panel.

  1. In the Apply for Trial pop-up window, fill in the reason for the application, read and check the User Service Agreement and Privacy Agreement, and click Submit Application< /strong>.

  2. After the application is approved, check the plug-in panel, as shown on the left side of the picture below, and you can start using it.

Use plug-in

The CodeFuse plug-in supports the following two usage modes, and the operations supported by the corresponding modes are as follows.

Usage Mode

Support operations

IDE code editing area

  • Type the comment text and press Enter to complete the code

Right mouse button

  • After selecting the code, right-click and select Add Comment
  • After selecting the code, right-click and select Explain Code
  • After selecting the code, right-click and select Generate Single Test
  • After selecting the code, right-click and select Code Optimization

The CodeFuse plug-in provides code suggestions for multiple programming languages and various frameworks, especially in Python and Java. Next, we will use Java as an example to demonstrate how to use the plug-in in VS Code.

Code Completion

The code completion function provides real-time code completion services based on massive data, including inline completion (single-line completion) and fragment completion (multi-line completion). Currently, this feature supports multi-line and single-line code completion in five mainstream programming languages, including Java, Python, TypeScript, JavaScript, and Go, as well as single-line code completion in 40 other programming languages.

Currently, CodeFuse supports two triggering methods: automatic code completion and manual code completion. After turning off automatic code completion, you can still trigger code completion manually using the shortcut key (Alt/Option + \). The code completion function also supports switching between multiple completion results. Currently, it supports switching up to 2 results. You can use the following shortcut keys to switch completion results.

Windows Key

Mac Buttons

Description

Alt]

Option]

Show next completion results.

Alt[

Option[

Show previous completion results.

Single line code completion
  1. Create a Java file in the IDE editor.
  2. In Java files, CodeFuse will be able to provide completion hints for what you type based on the context of the code. For example, if you press the Enter key after arr[i] = arr[j]; in the following example code, the plug-in will give a code completion prompt.
public class BubbleSort {
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i + + ) {
            for (int j = 0; j < arr.length - 1 - i; j + + ) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j]; ## Press Enter here to trigger completion, or use the shortcut key (Alt/Option + \) to actively trigger completion

    }
}
  1. To accept code completion suggestions, press Tab.

Multi-line code completion
  1. The plug-in also supports inputting comment text to complete multi-line code completion. For example, type the following comment content and press the Enter key. CodeFuse will automatically generate multiple lines of code based on the comment content, and the generated code will be displayed in gray text.
/*
* Determine whether the string is in English
*/
  1. To accept code completion suggestions, press Tab.

    After typing and pressing the Enter key, you can see the status of content generation in the lower right corner of the editor.

    • Content is being generated, and a circle animation and running prompt will be displayed.
    • After the content is successfully generated, the system will display the CodeFuse icon.

Turn off automatic code completion

The automatic code completion function is enabled by default. You can turn off this function in the settings of the plug-in panel to prevent automatic triggering of single or multi-line code completion.

  • Single line code completion: For example, type a function name in the editor and press the Enter key.
  • Multi-line code completion: For example, type a comment text in the editor and press the Enter key.

Note: After turning off automatic triggering of code completion, you can still trigger code completion manually using the Alt/Option + \ shortcut.

Explanation code

  1. Create a Java file in the IDE editor.
  2. Select the code fragment that needs to be explained in the Java file.
  3. Right-click the mouse and select CodeFuse: Explain Code. The plug-in will generate a code explanation in the dialog window on the left.

Add comments

Note: The current annotation generation function of the model has relatively complete support for the entire function level, so it is recommended that you give priority to generating annotations at the function level.

  1. Create a Java file in the IDE editor.
  2. Select the code fragment that needs to be commented in the Java file.
  3. Right-click the mouse and select CodeFuse: Add Comment to automatically generate comments on the selected code.

Generate single test

  1. Create a Java file in the IDE editor.
  2. Select the code fragment that needs to generate a single test in the Java file. For example, generate a single test for the following code snippet:
public class Conversion {
    public static byte binaryToByte(final boolean[] src, final int srcPos, final byte dstInit, final int dstPos,
            final int nBools) {
        if (src.length == 0 & amp; & srcPos == 0 || 0 == nBools) {
            return dstInit;
        }
        if (nBools - 1 + dstPos >= 8) {
            throw new IllegalArgumentException("nBools-1 + dstPos is greater or equal to than 8");
        }
        byte out = dstInit;
        for (int i = 0; i < nBools; i + + ) {
            final int shift = i + dstPos;
            final int bits = (src[i + srcPos] ? 1 : 0) << shift;
            final int mask = 0x1 << shift;
            out = (byte) ((out & amp; ~mask) | bits);
        }
        return out;
    }
}
  1. Right-click the mouse and select CodeFuse: Generate Single Test. The plug-in will generate test cases for the selected code in the left dialog window.

Code optimization

Based on large model code understanding capabilities and static source code analysis capabilities, CodeFuse supports analysis and understanding of selected code fragments, puts forward optimization and improvement suggestions, and can also directly form code patches based on improvement suggestions to help you write better code. . The steps to use code optimization are as follows.

  1. Create a Java file in the IDE editor, write and select a piece of code that needs optimization.
  2. Right-click the mouse and select CodeFuse: Code Optimization, which will provide multiple code optimization suggestions in the plug-in panel.

  3. Click Optimize the selected code according to the above suggestions to generate optimized code.

  4. Place the mouse on the generated code and click to view the code change Diff.

  5. In the reconstruction preview interface, check and click to change the content (Image ①), and then click Apply (Image ②) can replace the code. If you click Discard, you will exit the code Diff interface and abandon this change.

shortcut

shortcut key

Windows Key

Mac Buttons

Description

Tab

Tab

Accept coding suggestions. Press the Tab key in the editor to use the code generated by the plug-in; press the Esc key in the upper left corner to not accept the code suggestions.

Alt\

Option\

Actively trigger code completion. Press this shortcut key in the editor to manually trigger code completion at the cursor.

Note: After turning off automatic triggering of code completion, you can still use this shortcut to trigger completion.

Alt]

Option]

Show next completion results.

Alt[

Option[

Show previous completion results.

Note: CodeFuse supports custom shortcut keys. If you encounter a shortcut key conflict, you can follow the steps below to modify the shortcut keys.

  1. In the shortcut area of the CodeFuse panel, click Go to Settings.

  2. In the search box on the shortcut key page, enter CodeFuse to search for the shortcut key, then select the shortcut key and click to edit.

Quick Operation

Shortcut

Description

Clear session

Help documentation

Jump page

In the upper right corner of the plug-in panel, you can do the following:

  • Clear all content in the current session with one click.
  • Help documentation can be viewed.
  • You will go to the CodeFuse official website.

Right mouse button

Select the code fragment and right-click to choose to add comments, explain the code, generate unit tests, and optimize the code.

Quickly copy, paste, and expand code

In the plug-in panel, place the mouse in the lower right corner of the generated content and you can perform the following operations:

  • Replace the selected code with the generated code. For specific steps, see Optimizing Code.
  • Paste the code to the cursor in the editor with one click.
  • Copy code with one click.
  • Expand the code with one click, making it easier for you to read the complete code.

Shortcut commands

The following two shortcut commands are supported at the bottom of the plug-in panel.

  • /Explain: Explain the selected code.
  • /Test: Generate test cases for the selected code.

The steps to use shortcut commands are as follows:

In the editor, select a piece of code and click /Explain or /Test to send the content.

Feedback code quality

You can rate the content generated by CodeFuse and support likes and dislikes.

  • Like: If the code generated by CodeFuse meets expectations and can be used directly, you can like and comment.
  • Dislike: If the code generated by CodeFuse has obvious loopholes or gives wrong responses, you can click Dislike for feedback to help us continue to optimize. The quality of the model’s responses.
syntaxbug.com © 2021 All Rights Reserved.