Code completion method and device, storage medium, processor, and terminal
By obtaining the list of available variables and code elements and filtering candidates in combination with the syntax tree, the problem of low code completion accuracy in the existing technology is solved, and a more efficient code completion effect is achieved.
Patent Information
- Application Number
- CN202210227260.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-08
- Publication Date
- 2024-11-12
- Estimated Expiration
- 2042-03-08
Smart Images

Figure CN114721640B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of information processing technology, and in particular to a code completion method and device, a storage medium, a processor, and a terminal. Background Art
[0002] In the related art, when performing computer network programming or project programming, programmers often use intelligent code completion to assist programming. Existing intelligent code completion includes the following:
[0003] The first type, TabNine code completion and AiXCoder code completion, are both based on the latest open source deep learning models. Based on the historical massive open source code, a model suitable for code text completion is trained. In the prediction stage of actual application, the model reads the context to predict the list of completion candidates at the current cursor position and displays it to the user. The deep learning model can complete longer code statements, but the accuracy is low, and it often recommends code that is not suitable for the context or has grammatical errors, which brings trouble to users.
[0004] The second type, JetBrains IDEA built-in code completion, is based on accurate predictions of IDEA PSI syntax information. This code completion method does not have a deep learning model prediction process and generally recommends a single token. It is fast but short in length. It is a built-in capability of the code editor and cannot achieve intelligent code completion.
[0005] Current code completion methods basically rely on deep learning models, but the models are relatively black-box. Models based on historical training data are prone to recommending code that does not conform to the contextual grammar, forcing users to go back and modify large sections of completed code, which causes trouble to users and greatly reduces the efficiency of completion tools.
[0006] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0007] The embodiments of the present invention provide a code completion method and device, a storage medium, a processor, and a terminal to at least solve the technical problem that the code recommended by the model in the related art does not conform to the context grammar, the completion accuracy is low, and the user's interest in use is reduced.
[0008] According to one aspect of an embodiment of the present invention, a code completion method is provided, comprising: responding to a code completion instruction, using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, screening multiple candidate items in the candidate item set to obtain a processed candidate item set; using a pre-constructed syntax tree and the available variable list to filter the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0009] Optionally, the step of obtaining a list of available variables and a list of available code elements includes: obtaining a context code file at a location where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, obtaining all available variables at the location where code completion is required to obtain the list of available variables.
[0010] Optionally, the step of obtaining a list of available variables and a list of available code elements includes: obtaining a context code file at a location where code completion is required and syntax information of the context code file; obtaining a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; matching the scenario code template based on the context code file and the syntax information; and based on the scenario code template, determining all available code elements that meet the input prefix requirements at the location where code completion is required to obtain the list of available code elements.
[0011] Optionally, based on the available code element list, a plurality of candidate items in the candidate item set are screened to obtain a processed candidate item set, comprising: obtaining a context code file at a location where code completion is required; based on the context code file and the available code element list, searching for all candidate completion code classes that meet preset completion conditions to obtain an available class list; using the available class list to screen a plurality of candidate items in the candidate item set to obtain a candidate item set that meets context rules at a location where code completion is required; and using the candidate item set that meets context rules at a location where code completion is required as the processed candidate item set.
[0012] Optionally, after screening multiple candidate items in the candidate item set to obtain a processed candidate item set, it also includes: combining the candidate items in the processed candidate item set and the context code files of the locations where code completion is required to generate a code virtual file; and converting the code virtual file into a syntax tree according to preset conversion rules.
[0013] Optionally, after converting the code virtual file into a syntax tree according to preset conversion rules, it also includes: locating the first and last code elements of the code line at the position where code completion is required; based on the first and last code elements, querying the minimum common child node of the syntax tree of the code line at the position where code completion is required; and generating a serializable syntax tree according to the node type of the minimum common child node of the syntax tree.
[0014] Optionally, after generating a serializable syntax tree according to the node type of the smallest common child node of the syntax tree, it also includes: parsing the element information and symbol information of each candidate item in the serializable syntax tree; based on the element information and the symbol information, analyzing whether the element of each candidate item is missing, or analyzing whether the symbol of the candidate item is missing; in the case that the element of the candidate item is missing or the symbol is missing, completing the serializable syntax tree based on the element information of the known elements of the candidate item.
[0015] Optionally, the step of using the available variable list to filter the processed candidate item set to obtain a target candidate item set includes: using the available variable list to perform an associated position check on the variables referenced by the candidate items in the processed candidate item set, and filtering the candidate items with incorrect check; and / or, using the available variable list to check whether there are duplicate definitions of the variables referenced in the candidate items in the processed candidate item set, and filtering the candidate items to which the variables with duplicate definitions belong.
[0016] Optionally, after filtering the processed candidate item set to obtain the target candidate item set, it also includes: pushing each of the target candidate items in the target candidate item set; after receiving a selection instruction, displaying the selected target candidate item at a location where code completion is required.
[0017] Optionally, after the selected candidate item is displayed at the position where code completion is required, it also includes: based on the context code file at the position where code completion is required and the syntax information of the context code file, replacing the historical definition variable to be recommended with a continuation variable parameter that meets the position where code completion is required; or, positioning the cursor to the position where missing information exists in the selected candidate item; or, filling a preset end marker at the end of the selected candidate item.
[0018] According to another aspect of an embodiment of the present invention, there is also provided a code completion method, which is applied to a cloud server and includes: receiving a code completion instruction initiated by a client; responding to the code completion instruction, using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a location where code completion is required; obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the location where code completion is required, and the available code element list includes at least one available code element corresponding to the location where code completion is required; based on the available code element list, screening multiple candidate items in the candidate item set to obtain a processed candidate item set; using a pre-constructed syntax tree and the available variable list to filter the processed candidate item set to obtain a target candidate item set; transmitting the candidate item set to the client to display each candidate item in the candidate item set at a location where code completion is required.
[0019] According to another aspect of an embodiment of the present invention, a code completion device is further provided, comprising: a first response unit, for responding to a code completion instruction, and using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; an acquisition unit, for obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; a screening unit, for screening multiple candidate items in the candidate item set based on the available code element list to obtain a processed candidate item set; a filtering unit, for filtering the processed candidate item set using a pre-constructed syntax tree and the available variable list to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0020] Optionally, the acquisition unit includes: a first acquisition module, used to acquire the context code file at the location where code completion is required and the syntax information of the context code file; a second acquisition module, used to acquire all available variables at the location where code completion is required based on the context code file and the syntax information, and obtain the available variable list.
[0021] Optionally, the acquisition unit also includes: a third acquisition module, used to acquire the context code file at the location where code completion is required and the syntax information of the context code file; acquiring a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; a first matching module, used to match the scenario code template based on the context code file and the syntax information; and a first determination module, used to determine, based on the scenario code template, all available code elements that meet the input prefix requirements at the location where code completion is required, to obtain the list of available code elements.
[0022] Optionally, the screening unit includes: a fourth acquisition module, used to obtain the context code file at the location where code completion is required; a first search module, used to search for all candidate completion code classes that meet the preset completion conditions based on the context code file and the available code element list, and obtain an available class list; a first screening module, used to use the available class list to screen multiple candidate items in the candidate item set to obtain a candidate item set that meets the context rules at the location where code completion is required; and a second determination module, used to use the candidate item set that meets the context rules at the location where code completion is required as the processed candidate item set.
[0023] Optionally, the code completion device also includes: a combination unit, which is used to screen multiple candidate items in the candidate item set to obtain a processed candidate item set, and then combine the candidate items in the processed candidate item set and the context code file of the location where code completion is required to generate a code virtual file; a conversion unit, which is used to convert the code virtual file into a syntax tree according to preset conversion rules.
[0024] Optionally, the code completion device also includes: a first positioning unit, which is used to locate the first and last code elements of the code line at the position where code completion is required after converting the code virtual file into a syntax tree according to a preset conversion rule; a query unit, which is used to query the minimum common child node of the syntax tree of the code line at the position where code completion is required based on the first and last code elements; and a generation unit, which is used to generate a serializable syntax tree according to the node type of the minimum common child node of the syntax tree.
[0025] Optionally, the code completion device also includes: a parsing unit, which is used to parse the element information and symbol information of each candidate item in the serializable syntax tree after generating a serializable syntax tree according to the node type of the smallest common child node of the syntax tree; an analysis unit, which is used to analyze whether the elements of each candidate item are missing based on the element information and the symbol information, or to analyze whether the symbols of the candidate items are missing; and a completion unit, which is used to complete the serializable syntax tree based on the element information of the known elements of the candidate items when the elements of the candidate items are missing or the symbols are missing.
[0026] Optionally, the filtering unit includes: a first verification module, used to use the available variable list to perform associated position verification on the variables referenced by the candidate items in the processed candidate item set, and filter the candidate items with verification errors; and / or, a second verification module, used to use the available variable list to verify whether there are duplicate definitions of the variables referenced in the candidate items in the processed candidate item set, and filter the candidate items to which the variables with duplicate definitions belong.
[0027] Optionally, the code completion device also includes: a pushing unit, which is used to push each target candidate item of the target candidate item set after filtering the processed candidate item set to obtain the target candidate item set; and a display unit, which is used to display the selected target candidate item at the position where code completion is required after receiving a selection instruction.
[0028] Optionally, the code completion device also includes: a replacement unit, which is used to replace the historical definition variables to be recommended with continuation variable parameters that meet the position where code completion is required based on the context code file at the position where code completion is required and the grammatical information of the context code file after the selected candidate item is displayed at the position where code completion is required; or, a positioning unit, which is used to position the mobile cursor to the position where missing information exists in the selected candidate item; or, a filling unit, which is used to fill a preset ending marker at the end of the selected candidate item.
[0029] According to another aspect of an embodiment of the present invention, there is also provided a code completion device, which is applied to a cloud server and includes: a receiving unit, which is used to receive a code completion instruction initiated by a client; a second responding unit, which is used to respond to the code completion instruction and use a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; obtain an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; the processed candidate item set is filtered using a pre-constructed syntax tree and the available variable list to obtain a target candidate item set; a transmitting unit, which is used to transmit the candidate item set to the client to display each candidate item in the candidate item set at a position where code completion is required.
[0030] According to another aspect of an embodiment of the present invention, a storage medium is further provided, the storage medium including a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute any one of the code completion methods described above.
[0031] According to another aspect of an embodiment of the present invention, a processor is further provided, wherein the processor is used to run a program, wherein the program executes any one of the above-mentioned code completion methods when running.
[0032] According to another aspect of an embodiment of the present invention, a terminal is further provided, comprising: a first device; a second device; a third device and a fourth device; a processor, wherein the processor runs a program, wherein when the program runs, the following processing steps are performed on the data output from the first device, the second device, the third device and the fourth device: a first step, in response to a code completion instruction, a pre-trained completion model is used to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; a second step, obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; a third step, based on the available code element list, screening multiple candidate items in the candidate item set to obtain a processed candidate item set; and a fourth step, using a pre-constructed syntax tree and the available variable list to filter the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0033] In an embodiment of the present invention, after receiving a code completion instruction, in response to the code completion instruction, a pre-trained completion model is used to obtain a candidate set, and then an available variable list and an available code element list are obtained. Based on the available code element list, multiple candidates in the candidate set are screened and processed to obtain a processed candidate set, and a pre-built syntax tree and an available variable list are used to filter the processed candidate set to obtain a target candidate set, wherein the target candidate in the target candidate set is used to perform code completion at the position where code completion is required. In this embodiment, a syntax analysis method can be used to screen multiple candidates in the candidate set in combination with the obtained available variable list and the available code element list, and then the syntax tree is used to filter the wrong candidates recommended by the model to improve the accuracy of the recommended code candidates, thereby solving the technical problem that the code recommended by the model in the related art does not conform to the context syntax, the completion accuracy is low, and the user's interest in use is reduced. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0035] Figure 1 A hardware structure block diagram of a computer terminal (or mobile device) for implementing a code completion method is shown;
[0036] Figure 2 A block diagram using a computer terminal 10 (or a mobile device) as a transmitting end is shown;
[0037] Figure 3 is a flowchart of an optional code completion method according to Embodiment 1 of the present invention;
[0038] Figure 4 This is a process of another optional code completion method according to an embodiment of the present invention. Figure 1 ;
[0039] Figure 5 is a schematic diagram of an optional code completion syntax filtering mechanism according to an embodiment of the present invention;
[0040] Figure 6 This is a process of another optional code completion method according to an embodiment of the present invention. Figure 2 ;
[0041] Figure 7 is a schematic diagram of an optional code completion device according to an embodiment of the present invention;
[0042] Figure 8 It is a structural block diagram of a computer terminal according to an embodiment of the present invention. DETAILED DESCRIPTION
[0043] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0044] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0045] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following explanations:
[0046] Code completion: When developers enter code, this function can help developers enter the following code with one click. There are phrase completion, line-level completion, and block-level completion for large sections of code.
[0047] JetBrains IDEA: IDEA for short, is a Java code editor developed by JetBrains. It has rich development assistance functions and is deeply loved by developers.
[0048] Syntax tree: also known as abstract syntax tree, abbreviated as AST, it represents the grammatical structure of a programming language in the form of a tree, and each node on the tree represents a structure in the source code.
[0049] PSI: Program Structure Interface is a concept used for syntax analysis in JetBrains products. PSI in IDEA can abstract Java code element file directories into PSI elements, forming an AST-like structure, and open interfaces for plug-in developers to use.
[0050] token: the number of words in the completion result.
[0051] The following embodiments of the present application can be applied to applications or intelligent coding plug-ins related to code programming. Compared with the code completion in the prior art, there are many inaccuracies caused by false positives. After the user selects the completion item, the completed code needs to be modified, resulting in reduced efficiency and loss of value of code completion. In the present application, the code completion based on syntax analysis can filter the inaccurate options recommended by the model in combination with the code semantics of the context, and can also adjust the content of the candidate items based on the context semantics, automatically fill in the variable parameters, and supplement the ending symbols. It can also provide context grammar type information to help the model improve the accuracy, and can also directly recommend available class names, variable names, etc. based on the current location information to accelerate model prediction. The present application is described in detail below in conjunction with various embodiments.
[0052] Example 1
[0053] According to an embodiment of the present invention, a code completion method embodiment is also provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0054] The method embodiment provided in the first embodiment of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Figure 1 FIG. 1 shows a hardware structure block diagram of a computer terminal (or mobile device) for implementing a code completion method. Figure 1 As shown, the computer terminal 10 (or mobile device 10) may include one or more (shown as 102a, 102b, ..., 102n in the figure) processors (the processor may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA), a memory 104 for storing data, and a transmission device 106 for communication functions. In addition, it may also include: a display, an input / output interface (i.e., indicating a BUS bus), a universal serial bus (USB) port (which may be included as one of the ports of the BUS bus), a network interface, a power supply and / or a camera. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0055] It should be noted that the one or more processors and / or other data processing circuits described above may generally be referred to herein as "data processing circuits". The data processing circuits may be embodied in whole or in part as software, hardware, firmware, or any other combination thereof. In addition, the data processing circuit may be a single independent processing module, or may be incorporated in whole or in part into any of the other components in the computer terminal 10 (or mobile device). As involved in the embodiments of the present application, the data processing circuit acts as a processor control (e.g., selection of a variable resistor terminal path connected to an interface).
[0056] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the code completion method in the embodiment of the present invention. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory 104, that is, the above-mentioned code completion method is realized. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the computer terminal 10 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0057] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the computer terminal 10. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0058] The display may be, for example, a touch screen liquid crystal display (LCD) that enables a user to interact with a user interface of the computer terminal 10 (or mobile device).
[0059] Figure 1 The hardware structure block diagram shown can be used not only as an exemplary block diagram of the above-mentioned computer terminal 10 (or mobile device), but also as an exemplary block diagram of the above-mentioned server. In an optional embodiment, Figure 2 FIG. 1 shows a block diagram of using a computer terminal 10 (or a mobile device) as a transmitting end. Figure 2 As shown, the computer terminal 10 (or mobile device) can be connected to one or more servers 110 via a data network / data center 108. In an optional embodiment, the above-mentioned computer terminal 10 (or mobile device) can be any mobile computing device, etc. The data network connection can be a local area network connection, a wide area network connection, an Internet connection, or other types of data network connections. The computer terminal 10 (or mobile device) can be executed to connect to a network service performed by a server or a group of servers. A network server is a network-based user service, such as a social network, cloud resources, email, online payment or other online applications.
[0060] Under the above operating environment, this application provides Figure 3 The code completion method shown. Figure 3 is a flowchart of an optional code completion method according to the first embodiment of the present invention. Figure 3 As shown, including:
[0061] Step S301, in response to a code completion instruction, using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes a plurality of candidate items for performing code completion at a location where code completion is required;
[0062] Step S303, obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required;
[0063] Step S305, based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set;
[0064] Step S307 , using the pre-built syntax tree and available variable list, filtering the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0065] In the above steps, after receiving the code completion instruction, the code completion instruction can be responded to, and a pre-trained completion model can be used to obtain a candidate set, and then an available variable list and an available code element list can be obtained. Based on the available code element list, multiple candidates in the candidate set are screened and processed to obtain a processed candidate set, and a pre-built syntax tree and an available variable list are used to filter the processed candidate set to obtain a target candidate set, wherein the target candidate in the target candidate set is used to complete the code at the position where code completion is required. In this embodiment, a syntax analysis method can be used to filter multiple candidates in the candidate set in combination with the obtained available variable list and the available code element list, and then the syntax tree can be used to filter the wrong candidates recommended by the model to improve the accuracy of the recommended code candidates, thereby solving the technical problem that the code recommended by the model in the related art does not conform to the context grammar, the completion accuracy is low, and the user's interest in use is reduced.
[0066] The embodiments of the present invention are described in detail below in combination with the above-mentioned implementation steps.
[0067] In this embodiment, a completion plug-in or intelligent coding plug-in can be set for the subject of code completion, and the completion plug-in is used as an example below. The completion plug-in can be pre-connected to the completion binary file, and the completion binary file obtains the completion candidate through the algorithm model prediction, and then matches and filters with the available class list to generate the completion candidate with the highest probability that meets the code context logic.
[0068] When using a completion plug-in, the user will pre-install the completion plug-in on the programming application, and trigger the code completion instruction through a peripheral device such as a mouse. The code completion instruction may carry the location where code completion is required. The location where code completion is required may be the location where the mouse cursor stops, the current location where the cursor stops, or a location customized by the user through the completion plug-in.
[0069] Step S301, in response to a code completion instruction, a pre-trained completion model is used to obtain a candidate item set, wherein the candidate item set includes a plurality of candidate items for performing code completion at a location where code completion is required.
[0070] In this embodiment, a deep learning model can be used as a completion model, and candidate items are predicted through the completion model. The following items (predicted codes starting from the position where code completion is required) with the highest probability based on the previous context (i.e., the code that has been entered) are output as candidate items. When improving the accuracy of code completion, it is also possible to start from the completion model level and improve the model complexity to provide more accurate candidate items through the completion model.
[0071] Step S303, obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required.
[0072] In this embodiment, it is necessary to obtain all available variables at the position where code completion is required and available code elements that meet the input prefix requirement. The following describes in detail how to obtain the available variables and the available code elements.
[0073] When obtaining available variables, optionally, the step of obtaining a list of available variables and a list of available code elements includes: obtaining a context code file at a location where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, obtaining all available variables at the location where code completion is required to obtain a list of available variables.
[0074] When obtaining available variables, a list of available variables for the locations where code completion is required is obtained based on syntax analysis, so as to delete the variables that do not conform to the context in the candidate items and replace them with placeholders. In addition, based on this list, repeated definition of variable names can be avoided. For repeated variable definitions, a simple suffix is added to form a new variable.
[0075] The syntax analysis in this embodiment is provided by the platform where the completion plug-in is located, and the file can be parsed and converted into a syntax tree through the platform.
[0076] When obtaining a list of available code elements, optionally, the step of obtaining a list of available variables and a list of available code elements includes: obtaining a context code file and syntax information of the context code file at a location where code completion is required; obtaining a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; matching the scenario code template based on the context code file and the syntax information; based on the scenario code template, determining all available code elements that meet the input prefix requirements at the location where code completion is required, and obtaining a list of available code elements.
[0077] By performing grammatical analysis on the context of the location where code completion is required and combining it with pre-defined scenarios, code element recommendations are made for various types of scenarios. The grammatical analysis here specifically refers to available class acquisition.
[0078] For the predefined scenario code templates, in this embodiment, the following four scenarios are illustrated as examples, but in actual use, other scenario types besides these four scenarios (such as try catch template filling, for loop automatic completion) are also within the protection scope of this application.
[0079] Scenario 1: The capital letter at the beginning of the code line is considered to be a class name. The code prefix with the first capital letter at the initial position in the line is most likely to be a class name. The available class name is searched based on the prefix entered by the user. For example, Adm obtains the available class Admin.
[0080] Scenario 2. Admin a = new Ad. The new scenario is most likely to create an instance of a class. The constructor of the corresponding class can be found based on the type of the previous variable. The new scenario finds the available classes Admin and Admin's implementation class based on the class corresponding to the previous variable.
[0081] Scenario 3.throw new For. The scenario of throw new is most likely to throw an exception and find the exception class available at the current location. The scenario of throwing the exception is usually followed by a subclass of the Exception class, for example, ForbiddenException.
[0082] Scenario 4. Method list of static class. For example, when . appears, if it is preceded by a class name, the public methods and fields of the static class are obtained; if it is preceded by a variable, the public methods and fields of the class corresponding to the variable are obtained. For example, StringUtils.is can predict methods such as isEmpty().
[0083] In this embodiment, code elements are directly recommended for various scenarios. In this way, the accuracy of the recommended code elements is significantly improved, and the model can directly use the results for completion and return, and can also filter candidates based on the recommended words. There are relatively few situations that meet these scenarios, but once they meet, they can be directly and accurately recommended and filtered.
[0084] Step S305 , based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set.
[0085] Optionally, based on the list of available code elements, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set, including: obtaining a context code file at a location where code completion is required; based on the context code file and the list of available code elements, searching for all candidate completion code classes that meet preset completion conditions to obtain an available class list; using the available class list to screen multiple candidate items in the candidate item set to obtain a candidate item set that meets the context rules at the location where code completion is required; and using the candidate item set that meets the context rules at the location where code completion is required as the processed candidate item set.
[0086] In this embodiment, after obtaining the context of the location where code completion is required / the location where code is input, two pieces of information can be analyzed in combination with the preceding token input by the user: first, the user wants to enter a class name; second, the user's class name begins with the preceding token. Based on the two pieces of information analyzed, the completion plug-in can find all candidate completion code classes that meet the preset completion conditions at the location where code completion is required, and obtain a list of available classes.
[0087] After obtaining the list of available classes, the candidate item set determined in advance by the completion model is then matched and screened through the list of available classes to generate the completion candidate items that are most likely to meet the code context logic, and obtain the processed candidate item set.
[0088] Another optional method, after screening multiple candidate items in the candidate item set to obtain a processed candidate item set, also includes: combining the candidate items in the processed candidate item set with the context code files of the locations where code completion is required to generate a code virtual file; and converting the code virtual file into a syntax tree according to preset conversion rules.
[0089] The candidate items in the processed candidate item set and the context code file are combined to generate a code virtual file. The completion plug-in generates the virtual file in order to utilize the syntax tree generation capability of the plug-in to obtain the desired syntax tree.
[0090] Optionally, after converting the code virtual file into a syntax tree according to preset conversion rules, it also includes: locating the first and last code elements of the code line at the position where code completion is required; based on the first and last code elements, querying the minimum common child node of the syntax tree of the code line at the position where code completion is required; generating a serializable syntax tree according to the node type of the minimum common child node of the syntax tree.
[0091] By locating the first and last code elements of the line where the completion content is located, finding the smallest common child node of the syntax tree, and then obtaining related information according to the syntax tree node type, a custom serializable syntax tree is generated.
[0092] In this embodiment, the purpose of searching for the smallest common child node of the syntax tree is to find the root node that covers all nodes of the code line corresponding to the completion position. Obtaining this subtree means that syntax tree nodes that are not related to the position where code completion is required can be trimmed, making the transmitted information more focused and faster.
[0093] The serializability in the above serializable syntax tree is to ensure that information can be transmitted without distortion. The native syntax tree does not support serializability. Once it is transmitted, it cannot be restored in the completion binary file. Serializability can be understood as filtering out meaningful information in the syntax tree nodes and generating its own format for easy transmission and parsing. The role of the syntax tree is to use the obtained syntax tree information to filter known bad cases in the completion binary file. For example, if there is only the foo method in class A corresponding to the a variable, then a method call such as a.bar() is illegal and will be filtered to ensure the accuracy of the completion candidates.
[0094] Optionally, after generating a serializable syntax tree according to the node type of the smallest common child node of the syntax tree, it also includes: parsing the element information and symbol information of each candidate item in the serializable syntax tree; based on the element information and symbol information, analyzing whether the elements of each candidate item are missing, or analyzing whether the symbols of the candidate item are missing; in the case that the elements of the candidate item are missing or the symbols are missing, completing the serializable syntax tree based on the element information of the known elements of the candidate item.
[0095] When the completion model generates several candidates, this solution will generate a syntax tree based on the PSI syntax service, and then intercept the sub-syntax tree corresponding to the line according to the coordinates of the completion position. The completion candidate may fail to parse the code elements in the candidate due to missing symbols or incomplete content. This application will guess the corresponding type based on the element content. If it is a specific syntax tree node, it will parse the type, return value, parameter type, and method field list, etc., to filter out incorrect candidates.
[0096] In this embodiment, the corresponding type is guessed based on the element content because there are many objective situations that will lead to incomplete syntax tree information, such as incomplete code, syntax tree acquisition interface timeout, code elements with the same name, etc. Each node of the syntax tree not only records the name of the element, but also the type it belongs to, the methods and fields it contains, etc. Due to these objective reasons, the completion plug-in may not be able to obtain the specific information of the element, such as the type. In this embodiment, the pre-acquired information (such as the list of available classes) is used to fuzzily guess which type the current syntax tree node belongs to.
[0097] Step S307 , using the pre-built syntax tree and available variable list, filtering the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0098] In this embodiment, the candidate items can be filtered based on the syntax tree, which is the core function of this solution. It means matching each candidate item to see if it meets the position requirements for code completion through the parent-child relationship between the nodes on the syntax tree. If not, the candidate item is filtered out.
[0099] An optional implementation manner, using an available variable list to filter the processed candidate item set to obtain a target candidate item set, comprising: using the available variable list to perform an associated position check on the variables referenced by the candidate items in the processed candidate item set, and filtering the candidate items with check errors; and / or, using the available variable list to check whether there are duplicate definitions of the variables referenced in the candidate items in the processed candidate item set, and filtering the candidate items to which the variables with duplicate definitions appear.
[0100] The list of available variables is used to filter the candidate items, or simply put, as a basis for filtering and correction. The above implementation scheme in this embodiment illustrates two filtering methods: 1. A variable is referenced in the completion candidate item, and the legality of the variable name in the current context is verified to make a filtering judgment. For example, a.foo(x, y), the x variable has not been defined in the current position and is illegal, so this candidate item will be filtered; 2. Correction of duplicate variable definitions. For example, the completion candidate item is A a=new A(); but the variable a has been defined above and is illegal here. The candidate item can be modified to Aa1=new A() to make the candidate item legal.
[0101] In this embodiment, after filtering the processed candidate item set to obtain the target candidate item set, it also includes: pushing each target candidate item of the target candidate item set; after receiving the selection instruction, displaying the selected target candidate item at the position where code completion is required.
[0102] In this embodiment, when displaying, each candidate item may be displayed in a drop-down list or title box for the user to select. For the completion item recommendation, the Template mechanism of IDEA may be used, and the completion result is a template rather than text, which can utilize the variable positioning and replacement of the Template mechanism.
[0103] Another optional method, after displaying the selected candidate item at the position where code completion is required, further includes: based on the context code file at the position where code completion is required and the grammatical information of the context code file, replacing the historical definition variable to be recommended with the continuation variable parameter at the position where code completion is required; or, positioning the cursor at the position where missing information exists in the selected candidate item; or, filling the tail of the selected candidate item with a preset end marker.
[0104] In this embodiment, after determining the candidate items to be displayed, online semantics can be used to perform variable replacement, cursor positioning, symbol completion and other operations based on the candidate items selected by the user. These operations are optimization operations after the user selects the completed candidate items. For example, variable replacement refers to the ability to replace variable parameters within a method based on contextual grammatical analysis, positioning the cursor to the part expected to be filled in by the user (such as missing method parameters of the candidate item), and the tail often needs to be completed with semicolons, braces and other symbols to ensure code completeness and improve development efficiency.
[0105] Optionally, the present embodiment may also improve the content of code completion in other ways, for example, based on the pattern learning in the above text, if there are consecutive statements of similar format in the above text, some variables may be replaced and automatically completed.
[0106] Through the above embodiments, we can optimize the inaccurate completion and grammatical errors caused by the deep learning model from each stage of code completion through syntax analysis, and improve the accuracy and accurate completion length of code completion. Not only can we adjust the content of candidate items based on context semantics, automatically fill in variable parameters, supplement the end symbol, and provide context grammar type information to help the model improve accuracy, but we can also directly recommend available class names, variable names, etc. based on current location information to accelerate model prediction, so that the accuracy of code completion of the intelligent coding plug-in is greatly improved.
[0107] The present application is schematically described below in conjunction with another specific code completion plug-in.
[0108] Figure 4 This is a process of another optional code completion method according to an embodiment of the present invention. Figure 1 ,like Figure 4 As shown, the code completion methods include:
[0109] 1. User triggers completion;
[0110] 2. Get all available variables at the current position and available code elements that match the input prefix;
[0111] 3. Call deep learning model to complete;
[0112] 4. Filter candidates based on syntax tree;
[0113] 5. Perform variable replacement, cursor positioning, and symbol completion based on online semantics.
[0114] To obtain all available variables at the current position, we use syntax analysis to obtain a list of variables that are actually available at the current position. This is used to delete variables that do not conform to the context in the candidate items and replace them with placeholders. In addition, based on this list, we can avoid repeated definition of variable names and simply add a suffix to repeated variable definitions to form new variables.
[0115] When obtaining available code elements that match the input prefix, we predefine several scenarios based on syntax analysis and make direct recommendations for code elements based on the scenarios. Such recommendations are fast and accurate. The model can directly use the results for completion and return, or filter candidates based on the recommended words. There are relatively few cases that meet these scenarios, but once they do, they can be directly and accurately recommended and filtered. The following are the four scenarios currently used:
[0116] 1. The code prefix with the first letter capitalized at the initial position in the line is most likely to use the class name, and the available class name is found based on the prefix entered by the user.
[0117] 2. The new scenario is most likely to create a new instance of a class, and the constructor of the corresponding class can be found based on the type of the preceding variable.
[0118] 3. The scenario of throw new is most likely to throw an exception and find the exception class available at the current location.
[0119] 4. When "." appears, if it is preceded by a class name, the public methods and fields of the static class are obtained; if it is preceded by a variable, the public methods and fields of the class corresponding to the variable are obtained.
[0120] The core function of this solution is to filter candidates based on the syntax tree. When the model generates several candidates, this solution will generate a syntax tree based on the PSI syntax service, and then intercept the sub-syntax tree corresponding to the line according to the coordinates of the completion position. The completion candidate may fail to parse the code elements in the candidate due to missing symbols or incomplete content. This application can guess the corresponding type based on the element content. If it is a specific syntax tree node, it will parse the type, return value, parameter type and method field list, etc. to filter incorrect candidates.
[0121] Variable replacement, cursor positioning, and symbol completion based on online semantics are optimized operations after the user selects a completion candidate. The variable parameters in the method can be replaced based on the contextual syntax analysis. The cursor is positioned to the part expected to be filled in by the user (such as missing method parameters of the candidate). The tail often needs to be completed with symbols such as semicolons and curly braces to ensure code completeness and improve development efficiency.
[0122] The following describes how to implement code completion syntax filtering in conjunction with a specific implementation method.
[0123] Figure 5 is a schematic diagram of an optional code completion syntax filtering mechanism according to an embodiment of the present invention, with a completion plug-in and a completion binary file as the execution body, and the two interact to complete the code completion operation, such as Figure 5 As shown, including:
[0124] After receiving the completion instruction, the completion plug-in is called, and the completion model is called through the completion binary file to obtain the completion candidates, and the completion items (which are the abbreviations of the completion candidates, and can also refer to the above-mentioned candidates) are preprocessed. At the same time, the completion plug-in will periodically cache the available class list and variable list.
[0125] The completion plug-in obtains the list of available variables, the list of available code elements and completion candidates at the cursor position (i.e., the position where code completion is required as mentioned above), and then generates a virtual file based on the context of the completion item and the cursor position, locates the first and last code elements of the line where the completion content is located, and finds the smallest common child node of the syntax tree; then, it extracts valuable information based on the syntax tree node type and generates a custom serializable syntax tree;
[0126] The completion plugin returns the generated syntax tree and completion items to the completion binary file (i.e. Figure 5 The completion item and the corresponding syntax tree are returned in the completion plug-in), and then the completion binary file can perform subsequent processing such as filtering out the non-grammatical content based on the syntax tree, and return the processed completion result to the completion plug-in;
[0127] After obtaining each candidate item in the completion result, the completion plug-in displays each completion item. If the user selects a candidate item through an external device such as a mouse or an application command, subsequent processing can be performed based on the selected candidate item to replace the cursor placeholder, position the cursor, automatically replace variables, and add a tail completion.
[0128] In this embodiment, various syntax analysis mechanisms are used to improve the accuracy of code completion, reduce the low accuracy brought by the pure deep learning model, and reduce the disadvantages of the user needing to roll back and modify the code after completion. Starting from each stage of code completion, the accuracy of code completion is improved, and scenario-based analysis of code elements is used to improve the speed and accuracy of code completion. At the same time, the post-processing mechanism after user selection is used to improve the completeness of completion and the user selection rate.
[0129] Example 2
[0130] The following is a code completion method described in conjunction with another embodiment. The code completion method can be applied to a cloud server or a cloud end, with the cloud server or a data cloud end as the implementation entity.
[0131] Figure 6 This is a process of another optional code completion method according to an embodiment of the present invention. Figure 2 , applied to cloud servers, such as Figure 6 As shown, the code completion method includes:
[0132] Step S602, receiving a code completion instruction initiated by the client;
[0133] Step S604, in response to the code completion instruction, using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at the position where code completion is required; obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; using a pre-constructed syntax tree and an available variable list, the processed candidate item set is filtered to obtain a target candidate item set;
[0134] Step S606: transmitting the candidate item set to the client, so as to display each candidate item in the candidate item set at a position where code completion is required.
[0135] In the above steps, after receiving the code completion instruction initiated by the client, the cloud server can respond to the code completion instruction, use the pre-trained completion model to obtain the candidate set, and then obtain the available variable list and the available code element list. Based on the available code element list, multiple candidates in the candidate set are screened and processed to obtain a processed candidate set, and the pre-built syntax tree and the available variable list are used to filter the processed candidate set to obtain the target candidate set, and the candidate set is transmitted to the client to display each candidate in the candidate set at the location where code completion is required. In this embodiment, the cloud server can use syntax analysis means, combined with the obtained available variable list and the available code element list, to screen multiple candidates in the candidate set, and then filter the wrong candidates recommended by the model through the syntax tree to improve the accuracy of the recommended code candidates, thereby solving the technical problem that the code recommended by the model in the related technology does not conform to the context grammar, the completion accuracy is low, and the user's interest in use is reduced.
[0136] In this embodiment, the code completion based on syntax analysis can combine the code semantics of the context to filter out the inaccurate options recommended by the model, and can also adjust the content of the candidate items based on the context semantics, automatically fill in the variable parameters, and supplement the ending symbols. It can also provide context syntax type information to help the model improve the accuracy, and can also directly recommend available class names, variable names, etc. based on the current location information to accelerate model prediction. This solution greatly improves the accuracy of code completion and greatly improves the accuracy of the code completion function provided by the cloud server.
[0137] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should know that the present invention is not limited by the described action sequence, because according to the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.
[0138] Through the description of the above implementation methods, those skilled in the art can clearly understand that the code completion method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, disk, CD), including a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0139] Example 3
[0140] According to an embodiment of the present invention, a code completion device for implementing the above code completion method is also provided. Figure 7 is a schematic diagram of an optional code completion device according to an embodiment of the present invention. Figure 7 As shown, the device includes: a first response unit 71, an acquisition unit 73, a screening unit 75, and a filtering unit 77, wherein:
[0141] A first response unit 71 is used to respond to a code completion instruction and obtain a candidate item set using a pre-trained completion model, wherein the candidate item set includes a plurality of candidate items for performing code completion at a position where code completion is required;
[0142] An acquisition unit 73 is used to acquire an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required;
[0143] A screening unit 75, configured to screen multiple candidate items in the candidate item set based on the available code element list to obtain a processed candidate item set;
[0144] The filtering unit 77 is used to filter the processed candidate item set using the pre-built syntax tree and available variable list to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to complete the code at the position where code completion is required.
[0145] The schematic diagram of the above-mentioned code completion device can respond to the code completion instruction through the first response unit 71 after receiving the code completion instruction, use the pre-trained completion model to obtain the candidate set, then obtain the available variable list and the available code element list through the acquisition unit 73, and filter the multiple candidates in the candidate set based on the available code element list through the screening unit 75 to obtain the processed candidate set, and filter the processed candidate set through the filtering unit 77 using the pre-built syntax tree and the available variable list to obtain the target candidate set, wherein the target candidate in the target candidate set is used to complete the code at the position where code completion is required. In this embodiment, a syntax analysis method can be used to filter the multiple candidates in the candidate set in combination with the obtained available variable list and the available code element list, and then the wrong candidate recommended by the syntax tree filtering model is used to improve the accuracy of the recommended code candidate, thereby solving the technical problem that the code recommended by the model in the related art does not conform to the context syntax, the completion accuracy is low, and the user's interest in use is reduced.
[0146] Optionally, the acquisition unit includes: a first acquisition module, used to acquire the context code file and syntax information of the context code file at the location where code completion is required; a second acquisition module, used to acquire all available variables at the location where code completion is required based on the context code file and syntax information, and obtain a list of available variables.
[0147] Optionally, the acquisition unit also includes: a third acquisition module, used to acquire the context code file and syntax information of the context code file at the location where code completion is required; acquiring a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; a first matching module, used to match the scenario code template based on the context code file and syntax information; and a first determination module, used to determine, based on the scenario code template, all available code elements that meet the input prefix requirements at the location where code completion is required, to obtain a list of available code elements.
[0148] Optionally, the screening unit includes: a fourth acquisition module, used to obtain the context code file at the location where code completion is required; a first search module, used to search for all candidate completion code classes that meet the preset completion conditions based on the context code file and the list of available code elements, and obtain an available class list; a first screening module, used to use the available class list to screen multiple candidate items in the candidate item set, and obtain a candidate item set that meets the context rules at the location where code completion is required; a second determination module, used to use the candidate item set that meets the context rules at the location where code completion is required as the processed candidate item set.
[0149] Optionally, the code completion device also includes: a combination unit, which is used to screen and process multiple candidate items in the candidate item set to obtain a processed candidate item set, and then combine the candidate items in the processed candidate item set with the context code files of the locations where code completion needs to be performed to generate a code virtual file; and a conversion unit, which is used to convert the code virtual file into a syntax tree according to preset conversion rules.
[0150] Optionally, the code completion device also includes: a first positioning unit, used to locate the first and last code elements of the code line belonging to the position where code completion is required after converting the code virtual file into a syntax tree according to a preset conversion rule; a query unit, used to query the minimum common child node of the syntax tree of the code line belonging to the position where code completion is required based on the first and last code elements; and a generation unit, used to generate a serializable syntax tree according to the node type of the minimum common child node of the syntax tree.
[0151] Optionally, the code completion device also includes: a parsing unit, which is used to parse the element information and symbol information of each candidate item in the serializable syntax tree after generating a serializable syntax tree according to the node type of the smallest common child node of the syntax tree; an analysis unit, which is used to analyze whether the elements of each candidate item are missing based on the element information and symbol information, or to analyze whether the symbols of the candidate items are missing; and a completion unit, which is used to complete the serializable syntax tree based on the element information of known elements of the candidate items when there are missing elements or missing symbols of the candidate items.
[0152] Optionally, the filtering unit includes: a first verification module, which uses an available variable list to perform associated position verification on variables referenced by candidate items in the processed candidate item set, and filters candidate items with verification errors; and / or, a second verification module, which uses an available variable list to verify whether there are duplicate definitions of variables referenced in candidate items in the processed candidate item set, and filters candidate items to which the duplicate defined variables belong.
[0153] Optionally, the code completion device also includes: a pushing unit, which is used to push each target candidate item of the target candidate item set after filtering the processed candidate item set to obtain the target candidate item set; and a display unit, which is used to display the selected target candidate item at the location where code completion is required after receiving the selection instruction.
[0154] Optionally, the code completion device also includes: a replacement unit, which is used to replace the historical definition variables to be recommended with continuation variable parameters that meet the requirements of the position where code completion is required, after the selected candidate item is displayed at the position where code completion is required, based on the context code file at the position where code completion is required and the grammatical information of the context code file; or, a positioning unit, which is used to position the mobile cursor to the position where missing information exists in the selected candidate item; or, a filling unit, which is used to fill the tail of the selected candidate item with a preset ending marker.
[0155] It should be noted that the first response unit 71, the acquisition unit 73, the screening unit 75, and the filtering unit 77 correspond to steps S301 to S307 in Example 1, and the four units and the corresponding steps implement the same examples and application scenarios, but are not limited to the contents disclosed in the above-mentioned Example 1. It should be noted that the above-mentioned units, as part of the device, can be run in the computer terminal 10 provided in Example 1.
[0156] The present invention is described in detail below in conjunction with another optional code completion device.
[0157] According to another aspect of an embodiment of the present invention, there is further provided a code completion device, which is applied to a cloud server and includes:
[0158] A receiving unit, used for receiving a code completion instruction initiated by a client;
[0159] The second response unit is used to respond to the code completion instruction, use a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at the position where code completion is required; obtain an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; use a pre-constructed syntax tree and an available variable list to filter the processed candidate item set to obtain a target candidate item set;
[0160] The transmission unit is used to transmit the candidate item set to the client, so as to display each candidate item in the candidate item set at a position where code completion is required.
[0161] The above-mentioned code completion device can respond to the code completion instruction through the second response unit after receiving the code completion instruction initiated by the client through the receiving unit, and use the pre-trained completion model to obtain the candidate set, and then obtain the available variable list and the available code element list, based on the available code element list, multiple candidates in the candidate set are screened and processed to obtain the processed candidate set, and the pre-built syntax tree and the available variable list are used to filter the processed candidate set to obtain the target candidate set, and the candidate set is transmitted to the client through the transmission unit to display each candidate in the candidate set at the location where code completion is required. In this embodiment, the cloud server can use syntax analysis means, combined with the obtained available variable list and the available code element list, to screen multiple candidates in the candidate set, and then filter the wrong candidates recommended by the syntax tree filtering model to improve the accuracy of the recommended code candidates, thereby solving the technical problem that the code recommended by the model in the related art does not conform to the context grammar, the completion accuracy is low, and the user's interest in use is reduced.
[0162] In this embodiment, the code completion based on syntax analysis can combine the code semantics of the context to filter out the inaccurate options recommended by the model, and can also adjust the content of the candidate items based on the context semantics, automatically fill in the variable parameters, and supplement the ending symbols. It can also provide context syntax type information to help the model improve the accuracy, and can also directly recommend available class names, variable names, etc. based on the current location information to accelerate model prediction. This solution greatly improves the accuracy of code completion and greatly improves the accuracy of the code completion function provided by the cloud server.
[0163] Example 4
[0164] The embodiment of the present invention can provide a computer terminal, which can be any computer terminal device in a computer terminal group. Optionally, in this embodiment, the computer terminal can also be replaced by a terminal device such as a mobile terminal.
[0165] Optionally, in this embodiment, the computer terminal may be located in at least one network device among a plurality of network devices of the computer network.
[0166] According to another aspect of an embodiment of the present invention, a terminal is also provided, comprising: a first device; a second device; a third device and a fourth device; a processor, wherein the processor runs a program, wherein when the program runs, the following processing steps are performed on the data output from the first device, the second device, the third device and the fourth device: a first step, in response to a code completion instruction, a pre-trained completion model is used to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; a second step, obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; a third step, based on the available code element list, screening multiple candidate items in the candidate item set to obtain a processed candidate item set; and a fourth step, using a pre-constructed syntax tree and an available variable list, filtering the processed candidate item set to obtain a target candidate item set, wherein the target candidate item in the target candidate item set is used to perform code completion at the position where code completion is required.
[0167] Optionally, Figure 8 is a structural block diagram of a computer terminal according to an embodiment of the present invention. Figure 8 As shown, the computer terminal A may include: one or more (only one is shown in the figure) processors 102, a memory 104, and a peripheral interface, and the peripheral interface is connected to a radio frequency module, an audio module, and a display screen.
[0168] Among them, the memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the code completion method and device in the embodiment of the present invention. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, the above-mentioned code completion method is realized. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the computer terminal A via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0169] The processor can call the information and application stored in the memory through the storage controller to execute the following steps: a first step, responding to the code completion instruction, using a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at the position where code completion is required; a second step, obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; a third step, based on the available code element list, screening multiple candidate items in the candidate item set to obtain a processed candidate item set; and a fourth step, using a pre-constructed syntax tree and an available variable list to filter the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
[0170] It can be understood by those skilled in the art that Figure 8 The structure shown is for illustration only, and the computer terminal may also be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a handheld computer, a mobile Internet device (Mobile Internet Devices, MID), a PAD, or other terminal devices. Figure 8 It does not limit the structure of the above electronic device. For example, the computer terminal A may also include Figure 8 More or fewer components (such as network interfaces, display devices, etc.) shown in, or having Figure 8 Different configurations shown.
[0171] A person of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the hardware related to the terminal device through a program, and the program can be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0172] Example 5
[0173] An embodiment of the present invention further provides a storage medium. Optionally, the storage medium includes a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute any one of the above code completion methods. In this embodiment, the storage medium can be used to store the program code executed by the code completion method provided in the above embodiment 1.
[0174] Optionally, in this embodiment, the above storage medium may be located in any computer terminal in a computer terminal group in a computer network, or in any mobile terminal in a mobile terminal group.
[0175] According to another aspect of an embodiment of the present invention, a processor is further provided, and the processor is used to run a program, wherein any one of the above code completion methods is executed when the program is running.
[0176] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0177] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0178] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0179] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0180] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0181] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0182] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A code completion method, characterized in that: include: In response to the code completion instruction, a pre-trained completion model is used to obtain a candidate item set, wherein the candidate item set includes a plurality of candidate items for performing code completion at a location where code completion is required; Obtaining an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to a position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; Based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; Using the pre-built syntax tree and the available variable list, the processed candidate item set is filtered to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to complete the code at the position where the code completion is required; The steps of obtaining the available variable list and the available code element list include: Obtaining a context code file at a location where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, obtaining all available variables at the location where code completion is required to obtain an available variable list; Obtaining pre-configured scenario code templates, wherein each of the scenario code templates is configured with an index relationship between a code input prefix and available code elements; Matching a scenario code template based on the context code file and the grammar information; Based on the scenario code template, all available code elements that meet the input prefix requirement at the position where code completion is required are determined to obtain the available code element list.
2. The method according to claim 1, characterized in that The step of screening multiple candidate items in the candidate item set based on the available code element list to obtain a processed candidate item set includes: Get the context code file where code completion is required; Based on the context code file and the available code element list, searching for all candidate completion code classes that meet the preset completion conditions to obtain an available class list; Using the available class list to screen multiple candidate items in the candidate item set, to obtain a candidate item set that meets the context rule at the position where code completion is required; The candidate item set that meets the context rule at the position where code completion is required is used as the processed candidate item set.
3. The method according to claim 1, characterized in that After screening multiple candidate items in the candidate item set to obtain a processed candidate item set, the method further includes: Combining the candidate items in the processed candidate item set with the context code file of the location where code completion is required to generate a code virtual file; According to preset conversion rules, the code virtual file is converted into a syntax tree.
4. The method according to claim 3, characterized in that After converting the code virtual file into a syntax tree according to a preset conversion rule, the method further includes: Locate the first and last code elements of the code line where code completion is required; Based on the first and last code elements, query the smallest common child node of the syntax tree of the code line at the position where code completion is required; A serializable syntax tree is generated according to the node type of the smallest common child node of the syntax tree.
5. The method according to claim 4, characterized in that After generating a serializable syntax tree according to the node type of the smallest common child node of the syntax tree, the method further includes: Parsing element information and symbol information of each candidate in the serializable syntax tree; Based on the element information and the symbol information, analyzing whether an element of each candidate item is missing, or analyzing whether a symbol of the candidate item is missing; In the case that an element of the candidate item is missing or a symbol is missing, the serializable syntax tree is completed based on element information of known elements of the candidate item.
6. A code completion method, characterized in that: Applied to cloud servers, including: Receive code completion instructions initiated by the client; In response to a code completion instruction, a pre-trained completion model is used to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at a position where code completion is required; an available variable list and an available code element list are obtained, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; a pre-constructed syntax tree and the available variable list are used to filter the processed candidate item set to obtain a target A set of candidate items; wherein the step of obtaining a list of available variables and a list of available code elements comprises: obtaining a context code file at a location where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, obtaining all available variables at the location where code completion is required, and obtaining a list of available variables; obtaining a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; matching the scenario code template based on the context code file and the syntax information; based on the scenario code template, determining all available code elements that meet the input prefix requirements at the location where code completion is required, and obtaining the list of available code elements; The candidate item set is transmitted to the client, so that each candidate item in the candidate item set is displayed at a position where code completion is required.
7. A code completion device, characterized in that: include: A first response unit, used to respond to a code completion instruction, and to obtain a candidate item set using a pre-trained completion model, wherein the candidate item set includes a plurality of candidate items for performing code completion at a position where code completion is required; An acquisition unit is used to acquire an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to a position where code completion is required, and the available code element list includes at least one available code element corresponding to a position where code completion is required, and the acquisition unit is further used to acquire a context code file at the position where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, acquire all available variables at the position where code completion is required to obtain an available variable list; acquire a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and an available code element; match the scenario code template based on the context code file and the syntax information; based on the scenario code template, determine all available code elements that meet the input prefix requirements at the position where code completion is required to obtain the available code element list; A screening unit, configured to screen a plurality of candidate items in the candidate item set based on the available code element list to obtain a processed candidate item set; A filtering unit is used to filter the processed candidate item set using a pre-built syntax tree and the available variable list to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to perform code completion at the position where code completion is required.
8. A code completion device, characterized in that: Applied to cloud servers, including: A receiving unit, used for receiving a code completion instruction initiated by a client; The second response unit is used to respond to the code completion instruction, use a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes multiple candidate items for performing code completion at the position where code completion is required; obtain an available variable list and an available code element list, wherein the available variable list includes at least one available variable corresponding to the position where code completion is required, and the available code element list includes at least one available code element corresponding to the position where code completion is required; based on the available code element list, multiple candidate items in the candidate item set are screened to obtain a processed candidate item set; and the processed candidate items are screened using a pre-constructed syntax tree and the available variable list. The set is filtered to obtain a target candidate set, wherein the second response unit is further used to obtain a context code file at a location where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, all available variables at the location where code completion is required are obtained to obtain a list of available variables; a pre-configured scenario code template is obtained, wherein each scenario code template is configured with an index relationship between a code input prefix and available code elements; based on the context code file and the syntax information, the scenario code template is matched; based on the scenario code template, all available code elements that meet the input prefix requirements at the location where code completion is required are determined to obtain the list of available code elements; The transmission unit is used to transmit the candidate item set to the client, so as to display each candidate item in the candidate item set at a position where code completion is required.
9. A storage medium, characterized in that: The storage medium includes a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute the code completion method according to any one of claims 1 to 6.
10. A processor, characterized in that: The processor is used to run a program, wherein the program executes the code completion method according to any one of claims 1 to 6 when running.
11. A terminal, characterized in that: include: first device; Second device; A third device and a fourth device; A processor, wherein the processor runs a program, wherein when the program runs, the following processing steps are performed on the data output from the first device, the second device, the third device, and the fourth device: The first step is to respond to the code completion instruction and use a pre-trained completion model to obtain a candidate item set, wherein the candidate item set includes a plurality of candidate items for performing code completion at a position where code completion is required; The second step is to obtain a list of available variables and a list of available code elements, wherein the list of available variables includes at least one available variable corresponding to the position where code completion is required, and the list of available code elements includes at least one available code element corresponding to the position where code completion is required, wherein obtaining the list of available variables and the list of available code elements includes: obtaining a context code file at the position where code completion is required and syntax information of the context code file; based on the context code file and the syntax information, obtaining all available variables at the position where code completion is required, and obtaining a list of available variables; obtaining a pre-configured scenario code template, wherein each scenario code template is configured with an index relationship between a code input prefix and an available code element; matching the scenario code template based on the context code file and the syntax information; based on the scenario code template, determining all available code elements that meet the input prefix requirements at the position where code completion is required, and obtaining the list of available code elements; The third step is to screen multiple candidate items in the candidate item set based on the available code element list to obtain a processed candidate item set; and The fourth step is to use the pre-constructed syntax tree and the available variable list to filter the processed candidate item set to obtain a target candidate item set, wherein the target candidate items in the target candidate item set are used to complete the code at the location where code completion is required.
