Code processing method and device, equipment and medium
By performing static analysis and depth-first search on the contextual features of the code, combined with the evaluation model, the problem of low accuracy in prediction of existing AI code completion is solved, efficient and accurate automatic code completion is achieved, and development efficiency is improved.
Patent Information
- Application Number
- CN202410658502.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2020-11-02
- Filing Date
- 2021-10-11
- Publication Date
- 2024-08-23
AI Technical Summary
Existing AI-based code completion methods have low prediction accuracy, and users still need to manually complete or correct the code, resulting in low development efficiency.
By statically analyzing the contextual features of the code to be completed, using the context database to predict candidates, and combining depth-first search and evaluation models to improve prediction accuracy, automatic code completion is achieved.
It significantly improves the accuracy and efficiency of code completion, reduces the number of manual completions and corrections by users, reduces the requirements for computing power, does not rely on GPU resources, and avoids network transmission delays.
Smart Images

Figure CN118535141A
Abstract
Description
[0001] This application is a divisional application. The application number of the original application is 202180074635.4, and the original application date is October 11, 2021. The entire contents of the original application are incorporated into this application by reference. Technical Field
[0002] The present application relates to the field of software development technology, and in particular to a code processing method, apparatus, device, and computer-readable storage medium. Background Art
[0003] When developing software, many development tools, such as integrated development environments (IDEs), provide code completion functions. Code completion means that when a user enters a portion of code, such as a keyword or part of a function, the development tool can provide the user with at least one candidate to help the user complete the keyword or function. This can reduce the user's input operations and improve development efficiency.
[0004] With the progress of artificial intelligence (AI) technology, especially deep learning, in text generation, automatic code generation and completion through AI has become a hot research direction. However, the current prediction accuracy of the code to be completed based on AI is low. In many cases, users still need to complete the code manually, or manually correct the prediction result after accepting the prediction result of the code to be completed.
[0005] The industry urgently needs to provide a code processing method with high prediction accuracy to automatically complete the code and thus improve development efficiency. Summary of the invention
[0006] In view of this, the present application provides a code processing method, which predicts candidate items of the code to be completed by statically analyzing the code according to the contextual features of the code to be completed, thereby improving the prediction accuracy, thereby realizing automatic code completion and improving development efficiency. The present application also provides a device, an apparatus, a computer-readable storage medium, and a computer program product corresponding to the above method.
[0007] In a first aspect, the present application provides a code processing method. The method can be executed by a code processing system. The code processing system is provided with a user interface, such as a graphical user interface (GUI) or a command user interface (CUI).
[0008] The code processing system can receive the code input by the user through the user interface, and then determine the context features of the code to be completed based on the code input by the user. Among them, the context features refer to the features that can express the context in which the code is located, for example, including any one or more of the type of the base class, the class name of the base class, the prefix, the return type and the Boolean feature. Then the code processing system determines at least one candidate item of the code to be completed from the context database based on the context features of the code to be completed. Among them, the context database stores sample code and the context features of the sample code. The sample code may include any one or more of the identifiers such as class name, method name, function name, variable name or parameter name in an open source data set or a user's private data set. The code processing system presents the above at least one candidate item to the user through a user interface such as a GUI, thereby realizing code completion.
[0009] Since the candidates for the code to be completed are obtained by static analysis of the code, such as static syntax analysis and semantic analysis, the candidates for the code to be completed conform to the grammatical rules, and the candidates for the code to be completed are determined from the upper and lower feature databases based on the context features of the code to be completed, rather than predicted by models such as long short-term memory (LSTM), and have a higher probability of passing the compilation check. Therefore, the candidates predicted by this method have a higher accuracy. Code completion based on the above candidates can effectively reduce the number of times users manually complete the code, or effectively reduce the number of times users modify the completed code, greatly improving development efficiency and user experience.
[0010] In addition, this method does not require complex models, has low requirements on computing power, does not require graphics processor unit (GPU) resources, can be deployed locally, avoids phenomena such as freezes caused by network transmission delays, and improves user experience.
[0011] In some possible implementations, the code processing system may also obtain statistical information of at least one candidate item in the context database, and the statistical information may include the number of calls of the candidate item, such as the number of nested calls, the number of loop calls, etc. The statistical information may reflect the frequency of use of the candidate item, and the code processing system may filter at least one candidate item according to the statistical information, for example, filtering candidates with low frequency of use. Correspondingly, the code processing system may present the filtered candidate items to the user through a user interface such as a GUI, thereby providing the user with candidates with high frequency of use, so that the number of candidate items can be effectively reduced, and the recommendation of expired and deprecated application programming interfaces (APIs) can be avoided, thereby improving the prediction accuracy.
[0012] The code processing system can also sort the candidates according to the statistical information. Correspondingly, when providing the candidates, the code processing system can also display the candidates in the order of the sorting results. In this way, the user can quickly know the candidates that are ranked high and used frequently, which is convenient for the user to quickly select the above candidates and improve the efficiency of code completion.
[0013] In some possible implementations, the code processing system may also input the contextual features of at least one candidate and the code to be completed into the evaluation model to obtain the recommendation probability of at least one candidate. Correspondingly, the code processing system may further screen the candidate items according to the recommendation probability of at least one candidate, such as determining a target candidate among at least one candidate according to the recommendation probability of at least one candidate, and presenting the target candidate to the user through the user interface. This may further improve the prediction accuracy, and improve the accuracy and efficiency of code completion.
[0014] The evaluation model can be obtained by training the initial model with samples collected from an open source dataset or a user's private dataset. The initial model can be a simple model including 2 or more hidden layers. The hidden layer can be a fully connected layer (Dense layer), and the activation function of the hidden layer can be a hyperbolic function such as a hyperbolic tangent function TANH. The output layer includes a loss function, which can be a cross entry loss function (XENT) and the like.
[0015] The evaluation model trained by the above initial model does not need to consume GPU resources and can be deployed locally (for example, on a local computing device). This can reduce transmission delays, avoid freezes caused by long network transmission delays, and improve user experience.
[0016] In some possible implementations, the evaluation model can be implemented by a binary classification model. The binary classification model takes the candidate items and the contextual features of the candidate items as input and takes the recommended label as output. The binary classification model specifically matches the input candidate items and the contextual features of the candidate items with the existing identifier and the contextual features of the identifier to determine the recommended label. Among them, the recommended label can take the value of 0, 1, or true, false. When the recommended label is 0 or false, it indicates that the candidate item is not recommended. When the recommended label is 1 or true, it indicates that the candidate item is recommended.
[0017] The evaluation model can further filter the candidates according to the above-mentioned recommendation tags to improve the accuracy of the predicted candidates, thereby improving the accuracy of code completion. The evaluation model can also obtain statistical information of the candidates with the recommendation tag of 1 or true, and determine the recommendation probability of the candidates based on the statistical information, for example, determine the recommendation probability according to the score value. In this way, the code processing system can display the candidates recommended by the evaluation model according to the recommendation probability order.
[0018] In some possible implementations, when the candidate includes a function name, the code processing system may also fill in the parameters of the at least one candidate according to the code in the code file where the code input by the user is located (hereinafter referred to as the local code for ease of description). Correspondingly, the code processing system may present at least one candidate filled with the above parameters to the user through a user interface such as a GUI, thereby achieving multi-symbol completion.
[0019] Specifically, the code processing system can use a depth-first search algorithm to search for parameters corresponding to the function, for example, to search for parameters corresponding to the function from the local code, and then fill in the parameters of the candidate items based on the searched parameters. Furthermore, the code processing system can also fill in multiple sets of parameters for a candidate item to obtain multiple candidate items after filling in the parameters. The code processing system can sort the multiple candidate items after filling in the parameters according to information including the distance between the parameters and the code to be completed, filter the candidate items according to the sorting results, or display the candidate items in sequence. In this way, the candidate items that are close to the user's input intention can be displayed first, which is convenient for the user to quickly select and improve the efficiency of code completion.
[0020] It should be noted that in object-oriented computer languages, function names include method names. Therefore, the code processing system can fill in method parameters according to local codes, thereby achieving multi-symbol completion.
[0021] In some possible implementations, the code processing system may perform code analysis based on the context features of the code and determine at least one candidate item of the code to be completed from the context database. Specifically, the code processing system may search the context database using a depth first search (DFS) algorithm based on the context features of the code to be completed and determine at least one candidate item of the code to be completed.
[0022] The code processing system may search for candidate items that match the context features of the code to be completed through a depth-first search algorithm. For each candidate item, the code processing system may continue searching until a static function call is found.
[0023] Since this method searches for candidate items in the context feature library in combination with the context features of the code to be completed, and the sample code and its context features in the context feature library are extracted from code that conforms to grammatical rules and passes compilation checks, the candidate items obtained by this method conform to grammatical rules and have a greater probability of passing compilation checks.
[0024] Moreover, the method uses a depth-first search algorithm for matching, which can match all matching candidates in the context feature library. The sample code in the context database can also include uncommon identifiers, such as uncommon APIs. Based on this, even in a complex context environment (a context environment using uncommon APIs), the method can determine relatively accurate candidates from the context database and achieve high-precision code prediction.
[0025] In some possible implementations, the code input by the user includes the prefix of the code to be completed. For example, it is the prefix of the identifier to be completed. Based on this, the code processing system can also determine the completion condition according to the input code. The completion condition is specifically that the candidate items of the code to be completed include the above prefix. For an identifier with a length of N (N is a positive integer greater than 1), the prefix of the identifier can be any one of the first 1 code to the first N-1 code.
[0026] Wherein, when the code input by the user includes the prefix of the code to be completed, the code processing system can determine at least one candidate item matching the prefix of the code to be completed from the context database according to the context features of the code to be completed. In this way, the candidate items can be predicted more accurately and the prediction accuracy can be improved.
[0027] In some possible implementations, the context database includes at least one of a database constructed based on an open source dataset and a database constructed based on a private dataset of the user.
[0028] Taking the Java-based code completion scenario as an example, the code processing system can index the code in open source data sets such as GitHubcorpus, so as to identify identifiers such as class names, method names, function names, variable names, parameter names, operators, etc. in the code, and then determine the contextual features of each identifier, store the identifier and the contextual features of the identifier in the database, and thus obtain a context database.
[0029] The code processing system can also index the user's private data sets, such as the code in the code repository provided by the user, to identify identifiers such as class names, method names, function names, variable names, parameter names, operators, etc. in the code, and then determine the contextual features of each identifier, and obtain a context database based on the identifier and its contextual features.
[0030] Specifically, the code processing system can construct context databases based on the open source data set and the user's private data set respectively, and use the context database constructed based on the open source data set and the context database constructed based on the user's private data set to determine candidates for the code to be completed, thereby improving prediction accuracy.
[0031] In some possible implementations, the code to be completed includes code in a class method (also referred to as a class method), and the code input by the user includes a return type. The code processing system can predict candidates for the code to be completed in the class method based on the return type, and complete the class method based on the candidate.
[0032] In some possible implementations, a class method can be called in different environments. Correspondingly, the context of a class method can be different. Based on this, the code processing system can determine the role of each method call according to the context characteristics of the method call. For example, for the getitem() method, it can be determined that the role of the method call includes get accessor (or read accessor), for the add() method, it can be determined that the role of the method call includes adder, and for the removeitem() method, it can be determined that the role of the method call includes remover.
[0033] When the code processing system is training the evaluation model, it can also improve the accuracy of the evaluation model by adding features such as the role of the method call. In this way, the evaluation model can determine the recommendation probability of the candidate item in combination with the role of the method call, so that the candidate items recommended by the evaluation model are more in line with the user's intention, thereby achieving higher completion accuracy.
[0034] In some possible implementations, considering that there may be relationships between variables in the code, such as producer-consumer relationships, in most scenarios, circular references between variables are illegal. For example, variable A is a consumer of variable B, and variable B is also a consumer of variable A, which is usually illegal. Therefore, the code processing system can also track the data flow, filter out candidates with circular references from at least one candidate, and present the filtered candidates to the user through a user interface such as a GUI. This can avoid circular references and improve completion accuracy.
[0035] In a second aspect, the present application provides a code processing device. The code processing device comprises:
[0036] An interface unit, used for receiving a code input by a user through a user interface;
[0037] A feature extraction unit, used to determine context features of the code to be completed according to the code input by the user;
[0038] an analyzing unit, configured to determine at least one candidate item of the code to be completed from a context database according to the context feature of the code to be completed, wherein the context database stores sample codes and the context features of the sample codes;
[0039] The interface unit is further used to present the at least one candidate item to the user through the user interface.
[0040] In some possible implementations, the analysis unit is further configured to:
[0041] Obtaining statistical information of the at least one candidate item in the context database;
[0042] filtering the at least one candidate item according to the statistical information;
[0043] The interface unit is specifically used for:
[0044] The filtered candidate items are presented to the user through the user interface.
[0045] In some possible implementations, the device further includes:
[0046] an evaluation unit, configured to input the context features of the at least one candidate item and the code to be completed into an evaluation model, obtain a recommendation probability of the at least one candidate item, and determine a target candidate item among the at least one candidate item according to the recommendation probability of the at least one candidate item;
[0047] The interface unit is specifically used for:
[0048] The target candidates are presented to the user through the user interface.
[0049] In some possible implementations, the device further includes:
[0050] A parameter filling unit, used to fill in the parameters of the at least one candidate item according to the code in the file where the code input by the user is located when the candidate item includes a function name;
[0051] The interface unit is specifically used for:
[0052] The at least one candidate item populated with the parameters is presented to the user via the user interface.
[0053] In some possible implementations, the analysis unit is specifically configured to:
[0054] According to the context features of the code to be completed, a context database is searched using a depth-first search algorithm to determine at least one candidate item of the code to be completed.
[0055] In some possible implementations, the code input by the user includes a prefix of the code to be completed;
[0056] The analysis unit is specifically used for:
[0057] At least one candidate item matching a prefix of the code to be completed is determined from a context database according to the context feature of the code to be completed.
[0058] In some possible implementations, the context database includes at least one of a database constructed based on an open source dataset and a database constructed based on a private dataset of the user.
[0059] In some possible implementations, the code to be completed includes code in a class method, and the code input by the user includes a return type.
[0060] In some possible implementations, the device further includes:
[0061] An evaluation unit is used to determine a role of a method call corresponding to the code to be completed according to context features of the code to be completed, wherein the role is used to assist in determining a recommendation probability of a candidate item of the code to be completed.
[0062] In some possible implementations, the analysis unit is further configured to:
[0063] Filtering candidate items having circular references from the at least one candidate item;
[0064] The interface unit is specifically used for:
[0065] The filtered candidate items are presented to the user through the user interface.
[0066] In a third aspect, the present application provides a device, comprising a processor and a memory. The processor and the memory communicate with each other. The processor is used to execute instructions stored in the memory so that the device performs the method in the first aspect or any implementation of the first aspect.
[0067] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, wherein the instructions instruct a device to execute the method described in the first aspect or any one of the implementations of the first aspect.
[0068] In a fifth aspect, the present application provides a computer program product comprising instructions, which, when executed on a device, enables the device to execute the method described in the first aspect or any one of the implementations of the first aspect.
[0069] Based on the implementations provided in the above aspects, this application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS
[0070] In order to more clearly illustrate the technical method of the embodiments of the present application, the drawings required for use in the embodiments are briefly introduced below.
[0071] Figure 1 A system architecture diagram of a code processing system provided in an embodiment of the present application;
[0072] Figure 2 A schematic diagram of an interface for displaying at least one candidate item provided in an embodiment of the present application;
[0073] Figure 3A A schematic diagram of the architecture of a code processing system provided in an embodiment of the present application;
[0074] Figure 3B A schematic diagram of the architecture of a code processing system provided in an embodiment of the present application;
[0075] Figure 4 A flowchart of a code processing method provided in an embodiment of the present application;
[0076] Figure 5 A schematic diagram of an interface for displaying at least one candidate item provided in an embodiment of the present application;
[0077] Figure 6 A schematic diagram of an interface before and after completing a code snippet provided in an embodiment of the present application;
[0078] Figure 7 A schematic diagram of the effect of code completion provided in an embodiment of the present application;
[0079] Figure 8 A schematic diagram of the effect of code completion provided in an embodiment of the present application;
[0080] Fig. 9 A schematic diagram of the structure of a code processing device provided in an embodiment of the present application;
[0081] Fig.10 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0082] The terms "first" and "second" in the embodiments of the present application are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.
[0083] First, some technical terms involved in the embodiments of the present application are introduced.
[0084] In the field of software development, source code refers to code files written by developers using development tools such as code editors and computer languages supported by integrated development environments (IDEs), such as C, Java, Python, etc.
[0085] Source code (also called source program) includes a series of human-readable computer language instructions. The computer language instructions in the source code can be compiled by a compiler into binary instructions executable by a computer. The computer executes the binary instructions to achieve the corresponding functions. Among them, the binary instructions executable by the computer can also be called target code. The code can include source code and target code.
[0086] An identifier (token) is the smallest compilation unit in the source code compilation process. Identifiers can include any one or more of keywords, class names, method names, function names, variable names, parameter names, and operators. Among them, keywords refer to words with special meanings specified in computer languages, such as break for breakpoints, return for returns, and so on. A class (class) refers to a data structure in an object-oriented computer language that describes the common properties and methods of the created objects. The class name is the name of the class. The method name is the name of the method described by the class. For example, the class name can be DocumentBuilder, and the method name can be newDocument.
[0087] A function is an executable code block used to implement a certain function. Since methods are related to objects and classes and rely on objects for calling, methods can also be regarded as a special function in object-oriented computer languages. Function name is the name of the function, such as count, print, etc. Functions support passing in some parameters and processing the parameters. Furthermore, functions can also return some data, that is, functions can also include return values. Parameter name is the name of the parameter. The parameters used when defining the function name and function body are called formal parameters, or formal parameters for short. Formal parameters are virtual variables that do not occupy memory. The parameters when a function is called are called actual parameters, or actual parameters for short. Actual parameters are variables that occupy memory. A variable is a data structure that stores variable data, which can be a variable value, such as a function value, or a variable text, such as text typed by a user. Variable name refers to the name of the variable.
[0088] Code completion refers to predicting at least one identifier that a user (e.g., a developer) intends to input based on the code (e.g., source code) that the user (e.g., a developer) has already entered, and providing input suggestions to the user based on the prediction results, so that the user can directly complete the code based on the input suggestions. Code completion can reduce the number of times a user types characters, reduce spelling errors, and eliminate the need for users to spend time memorizing unfamiliar class names, method names, etc., which can improve development efficiency.
[0089] Code completion can be divided into single token completion and multi-token completion. Among them, single token completion refers to predicting a single identifier and then completing it according to the prediction result. In some embodiments, single token completion may include completing class names, method names, function names, variable names or parameter names. Multi-token completion refers to predicting multiple identifiers and then completing them according to the prediction result. In some embodiments, multi-token completion may include completing multiple of class names, method names and parameter names, or completing code snippets including multiple identifiers. Among them, a code snippet is a small section of source code, which may include some functional statements, such as class declarations, function declarations, or code blocks with start and end identifiers. A typical application of multi-token completion is to complete class methods (also referred to as class methods), especially to complete class methods with return types.
[0090] With the breakthrough progress made by artificial intelligence (AI), especially deep learning, in the task of natural language text generation, the industry has proposed a technical solution to process computer language text (specifically code) through AI to achieve automatic code completion. For example, a deep neural network based on a long short-term memory network (LSTM) is constructed, which is a time recursive neural network suitable for processing and predicting sequences with time order. The code input by the user is then serialized to obtain an input sequence, and then the input sequence is input into the above-mentioned deep neural network to predict the candidate for the next identifier, thereby achieving single symbol completion. Furthermore, it is also possible to select candidates with higher probability, merge the candidate with the input code and then serialize it to generate a new input sequence, and input the input sequence into the above-mentioned deep neural network to predict the candidate for the next identifier, thereby achieving multi-symbol completion.
[0091] The key to the above method lies in code modeling. Currently, code modeling mainly includes sequence modeling and abstract syntax tree (AST) modeling. Sequence modeling is achieved by performing lexical analysis on the code and obtaining the token stream. However, this method is prone to produce grammatically uncompliant code. AST modeling is achieved by performing lexical analysis and grammatical analysis on the code. Even if the code generated based on this method conforms to the grammatical rules, there is a high probability that it cannot be compiled. In other words, the accuracy of the completion code predicted by the above method is not high and it is difficult to meet user needs.
[0092] In view of this, the present application provides a code processing method. The method can be executed by a code processing system. Specifically, the code processing system receives the code input by the user through a user interface such as a graphical user interface (GUI) or a command user interface (CUI), and then determines the context features of the code to be completed according to the code input by the user, and then determines at least one candidate item of the code to be completed from a context database according to the context features of the code to be completed, wherein the context database stores sample codes and context features of the sample codes, and the code processing system presents the above at least one candidate item to the user through a user interface such as a GUI, thereby realizing code completion.
[0093] Since the candidates for the code to be completed are obtained by static analysis of the code, such as static syntax analysis and semantic analysis, the candidates for the code to be completed conform to the grammatical rules, and the candidates for the code to be completed are determined from the upper and lower feature databases based on the context features of the code to be completed, rather than predicted by models such as LSTM, and have a higher probability of passing the compilation check. Therefore, the candidates predicted by this method have a higher accuracy. Code completion based on the above candidates can effectively reduce the number of times users manually complete the code, or effectively reduce the number of times users modify the completed code, greatly improving development efficiency and user experience.
[0094] In addition, this method does not require complex models, has low requirements on computing power, does not require graphics processor unit (GPU) resources, can be deployed locally, avoids phenomena such as freezes caused by network transmission delays, and improves user experience.
[0095] The sample codes in the above-mentioned context database may include unfamiliar application programming interfaces (APIs). Based on this, even in a complex context environment (a context environment using unfamiliar APIs), the method can determine more accurate candidates from the context database and achieve high-precision code prediction.
[0096] Furthermore, the code processing system can also obtain statistical information of at least one candidate item in the context database, and then filter at least one candidate item according to the statistical information. On the one hand, the prediction accuracy can be further improved, and on the other hand, outdated or deprecated candidate items, such as outdated or deprecated APIs, can be avoided from being recommended.
[0097] The code processing system can not only predict identifiers such as method names and function names, but also predict parameters of methods and functions, that is, the code processing system can perform single-symbol prediction or multi-symbol prediction. Since the code processing system predicts candidates from the context database through static analysis, both single-symbol prediction and multi-symbol prediction have high accuracy. In addition, the code processing system inputs the candidate filled with parameters into the evaluation model to obtain the recommendation probability of the candidate, and then makes accurate recommendations based on the recommendation probability to further improve the accuracy of code completion.
[0098] It should be noted that the code processing method provided in the embodiment of the present application can be provided to users in the form of a plug-in. A plug-in is a program written in accordance with a certain standard application program interface, which runs on a platform specified by the program (may support multiple platforms at the same time), and cannot run independently from the specified platform.
[0099] Specifically, the service provider of the development tool or a third party may publish a plug-in for the development tool, such as an IDE or a code editor, to enhance the function of the development tool. For ease of description, this application uses the development tool as an IDE for example.
[0100] See also Figure 1 The system architecture diagram of the code processing system shown in Figure 1 As shown, the code processing system 100 includes an IDE 102 and a completion subsystem 104 located at the back end. The IDE 102 includes an IDE core 1022 and an IDE plug-in 1024 installed in the IDE 102. The completion subsystem 104 includes a code analysis module 1042 and a context database 1044. Optionally, the completion subsystem 104 may also include any one or more of a parameter filling module 1046, an evaluation module 1048, and an indexing module 1049.
[0101] Specifically, the IDE kernel 1022 is used to provide native functions of the IDE 102, such as code prompts, code spelling detection, etc., and the IDE plug-in 1024 is used to interact with the completion subsystem 104 to implement enhanced functions, such as intelligent code completion. The interaction process is described in detail below.
[0102] In some embodiments, the IDE plug-in 1024 can receive the code input by the user through the user interface, obtain the position of the input cursor in the code, wherein the position of the input cursor is the position of the code to be completed, thereby determining the contextual features of the code to be completed, such as the type of the base class (such as public, private, protected, etc.), the class name of the base class, the prefix, the return type, and any one or more of the Boolean features. The Boolean feature may include at least one of the following features:
[0103] private boolean is_in_direct_new;
[0104] private boolean is_in_binary_op;
[0105] private boolean is_in_variable_name;
[0106] private boolean inClassDeclarationName;
[0107] private boolean is_in_interface;
[0108] private boolean is_in_for_declaration.
[0109] The IDE plug-in 1024 may send the contextual features of the code to be completed to the completion subsystem 104, and the completion subsystem 104 performs static analysis based on the contextual features of the code to be completed, generates at least one candidate item of the code to be completed, and returns the at least one candidate item to the IDE plug-in 1024. The IDE 102 may present at least one candidate item of the code to be completed to the user, so that the user can select a candidate item for code completion.
[0110] The context database 1044 of the completion subsystem 104 stores sample codes and context features of the sample codes. The sample codes may be identifiers such as class names, method names, function names, variable names, or parameter names. It should be noted that the sample codes may be single identifiers or multiple identifiers. The context features of the sample codes are specifically features extracted based on the context of the sample codes, such as variable types, object types, return types, etc. The code analysis module 1042 of the completion subsystem 104 may determine at least one candidate item of the code to be completed from the context database 1044 according to the context features of the code to be completed.
[0111] For ease of understanding, the present embodiment also provides a schematic diagram of an interface in which IDE 102 presents candidate items to a user. Figure 2 As shown, the code editing interface 200 presents the code input by the user, specifically shown as 202 in the figure. It should be noted that, Figure 2 202 shows a snippet of the code entered by the user, specifically:
[0112] public static void basicString(){
[0113] Document doc=
[0114] Some code snippets may be included before or after the code snippet. Figure 2”…” is used for schematic illustration. After the IDE plug-in 1024 obtains the position of the input cursor, specifically “=”, the IDE plug-in 1024 can determine the context of the code to be completed based on the position, and then extract the context features of the code to be completed. In this example, the context features of the code to be completed may include a return type of Document type. The IDE plug-in 1024 sends the context features to the completion subsystem 104, and the code analysis module 1042 determines at least one candidate item of the code to be completed from the context database based on the context features. Optionally, the code analysis module 1042 can also obtain statistical information of the candidate items in the context database 1044, and filter at least one candidate item based on the statistical information. Further, the code analysis module 1042 can also transmit the candidate items (for example, the filtered candidate items) to the parameter filling module 1046 and the evaluation module 1048 for subsequent processing, such as parameter filling and recommendation probability evaluation. IDE102 can obtain the above-mentioned candidate items, such as the candidate items filtered by the code analysis module 1042, the candidate items filled with parameters by the parameter filling module 1046, or the candidate items determined by the evaluation module 1048 according to the recommendation probability, and display the above-mentioned candidate items, as shown in FIG. Figure 2 As shown in 204.
[0115] It should be noted that the IDE kernel 1022 can also directly generate at least one candidate item based on the code input by the user through text completion technology. In some embodiments, the completion method provided by the IDE kernel 1022 itself, such as the text completion method, can be compatible with the code completion method provided by the example of this application. When the text completion method provided by the IDE kernel 1022 is used together with the code processing method provided by the embodiment of this application, IDE102 can display the candidate items generated by the text completion method and the candidate items generated by the code processing method of the embodiment of this application together, as shown in the figure. Figure 2 As shown in 204 and 206.
[0116] When displaying candidate items generated by different methods, IDE102 can also distinguish them by different display modes. For example, IDE102 can add the identification of the method used by the candidate item before the candidate item. Figure 2 As shown in , IDE102 may use circles to mark candidate items generated by the IDE kernel 1022 through the text completion method, and use plus signs with boxes to mark candidate items generated by the code processing method of the embodiment of the present application. For another example, IDE102 may also use different colors or different fonts to distinguish candidate items generated by different methods.
[0117] It should be noted that IDE 102 may display the candidate items in order according to the probability of each candidate item, so that the user can quickly learn the candidate items with higher probability and select the candidate items with higher probability for code completion.
[0118] In some possible implementations, the code analysis module 1042 can determine more candidate items from the code feature library according to the context features of the code to be completed. Considering that some candidate items have a low probability of use, the code analysis module 1042 can also filter at least one candidate item according to the statistical information of the candidate item in the context database 1044, such as the number of calls callCount, the number of nested calls nestedCount, etc., and filter out the candidate items with a low probability.
[0119] Furthermore, when the candidate is a function name (in an object-oriented computer language, the function name can be a method name), the parameter filling module 1046 can also fill in parameters for the candidate, such as the filtered candidate, thereby achieving multi-symbol completion and avoiding the user from manually entering the function parameters. Specifically, the parameter filling module 1046 can determine the appropriate parameters by searching the local code (specifically the code in the code file where the user-input code is located) and fill the parameters into the candidate. Then the parameter filling module 1046 inputs the candidate after filling in the parameters into the evaluation module 1048. The evaluation module 1048 can evaluate the candidate after filling in the parameters through the evaluation model to determine the recommendation probability of the candidate after filling in the parameters.
[0120] The evaluation module 1048 can send the candidate items after each parameter is filled and their recommendation probabilities to the IDE plug-in 1024. Correspondingly, the IDE102 can display the top N candidate items according to the recommendation probability, or display the candidate items with a recommendation probability greater than a preset probability. It should be noted that the evaluation module 1048 can also filter the candidate items after the parameters are filled according to the recommendation probability, for example, filter the top N candidate items, or filter the candidate items with a recommendation probability greater than a preset probability, and then return the filtered candidate items to the IDE plug-in 1024, and the IDE displays the filtered candidate items, or displays the filtered candidate items and their recommendation probabilities.
[0121] The indexing module 1049 can also index local codes, such as codes in the user's code warehouse, obtain sample codes and context features of the sample codes according to the indexing results, and store them in the context database 1044. Correspondingly, the candidate items determined by the code analysis module 1042 from the context database 1044 according to the context features of the code to be completed can include local code calls, such as local variable names. The indexing module 1049 enriches the context database by indexing the codes in the user's code warehouse, so that the intelligent recommendation results are more accurate when performing intelligent code completion.
[0122] In some possible implementations, the service provider of the development tool may also natively embed the relevant code of the code processing method provided in the embodiment of the present application into the kernel when developing the above-mentioned development tool, thereby eliminating the operation of installing the plug-in and avoiding the security risks introduced by the plug-in installation.
[0123] In the embodiment of the present application, the various parts of the code processing system 100 can be centrally deployed in a local computing device (a user device under direct user control, such as a user terminal such as a laptop, desktop, smart phone, etc.) or a cloud computing cluster (including at least one cloud computing device, such as a cloud server, etc.). Of course, the various parts of the code processing system 100 can also be distributedly deployed in a cloud computing cluster. The deployment method of the code processing system 100 is described in detail below.
[0124] In some possible implementations, IDE102 and completion subsystem 104 can be deployed on a local computing device, such as a terminal device such as a personal computer (PC). Since the code processing system 100 in the embodiment of the present application consumes less computing power when performing code analysis, and the evaluation model can be implemented through a simple network (for example, including 2 hidden layers), the requirements for the computing performance of the terminal device are relatively low, and a lightweight terminal device can meet business needs. In addition, the completion subsystem 104 is deployed on a local computing device together with IDE102, which can reduce the interaction time between IDE102 and the completion subsystem 104, avoid network transmission delays causing the completion function to freeze, and affect the user experience.
[0125] In some other possible implementations, the IDE 102 and the completion subsystem 104 may be deployed in a cloud computing cluster, that is, the IDE 102 is a cloud IDE, and the IDE 102 and the completion subsystem 104 are provided to users in the form of cloud services.
[0126] The cloud service provider can integrate the intelligent code completion service provided by the completion subsystem 104 and the code development environment service provided by the cloud IDE into one cloud service for users to use, or can separately provide cloud IDE and code intelligent completion services for users to use. In some cases, the cloud service provider can use the code intelligent completion service as a value-added service of the cloud IDE. After the user purchases or leases the value-added service, the cloud service provider will combine it with the cloud IDE and provide it to the user for use.
[0127] See also Figure 3A The schematic diagram of the code processing system 100 is shown in FIG. Figure 3A As shown, IDE102 and completion subsystem 104 are provided by a cloud service provider, and IDE102 and completion subsystem 104 can be deployed in the same cloud computing cluster. In other possible implementations of the present application embodiment, IDE102 and completion subsystem 104 can also be provided by different cloud service providers and deployed in different cloud computing clusters.
[0128] See next Figure 3B In the schematic diagram of the code processing system 100 shown, the IDE 102 can be deployed on a local computing device, and the completion subsystem 104 can be deployed on a cloud computing cluster. When a user processes code through the IDE 102 deployed on a local computing device, if code completion is triggered, the IDE 102 calls the completion subsystem 104 in the cloud computing cluster to obtain at least one candidate item of the code to be completed. Among them, users who use the completion service can pre-register for the cloud service, so that cloud service traffic can be achieved.
[0129] Moreover, the evaluation model deployed in the cloud computing cluster can be based on the user's private data set, such as a customized model trained based on a data set built based on a code repository provided by the user. This model is more in line with the user environment and has a better recommendation effect.
[0130] Above FIG. 3A to FIG. 3B These are only some specific examples of how the code processing system 100 is deployed in the embodiments of the present application. In other possible implementations of the embodiments of the present application, the code processing system 100 may be deployed in other ways, such as IDE 102 being deployed in the cloud and completion subsystem 104 being deployed in a local computing device, and the embodiments of the present application are not limited to this.
[0131] Next, from the perspective of the code processing system 100, the code processing method provided in the embodiment of the present application is introduced in detail.
[0132] See also Figure 4 The flowchart of the code processing method shown in FIG. 1 comprises:
[0133] S402: The code processing system 100 receives a code input by a user through a user interface.
[0134] The code processing system 100 (e.g., IDE 102) can receive the code typed by the user through a physical keyboard through a user interface (e.g., GUI or CUI). Of course, the code processing system 100 can also receive the code typed by the user through a virtual keyboard in a touch-controlled manner through a user interface.
[0135] In some possible implementations, the code processing system 100 may also select a code file through a user interface to receive the code in the code file, wherein the code in the code file may include code previously written by the current user, or may include code previously written by other users.
[0136] The code input by the user may be a code written in a single computer language, such as a code written in C language, a code written in Java language, or a code written in Python language. In some embodiments, the code input by the user may also be a code written in multiple computer languages in a mixed programming manner, such as a code written in C language or embedded assembly language.
[0137] S404: The code processing system 100 determines context features of the code to be completed according to the code input by the user.
[0138] The code processing system 100 (e.g., the IDE plug-in 1024 in the IDE 102) can capture the position of the input cursor. When the code completion function is triggered, the position of the input cursor is the completion position. It should be noted that the completion position can be the end position of the input line or the middle position. Of course, in some embodiments, the completion position can also be the beginning position of the input line.
[0139] In computer language, the context of code can be understood as the context or environment in which the code is located. Features of context refer to features that can express the context in which the code is located, such as any one or more of the base class type, base class name, prefix, return type, and Boolean features.
[0140] Taking the input code including "Document doc=" as an example, the completion position of the code to be completed is the position after "=", which is specifically the end of the input. The context features of the code to be completed may include the return value type being the Document type, and the Boolean type is_in_API being true. Among them, is_in_API being true indicates that the current completion is a class method completion.
[0141] The completion type can be single symbol completion or multiple symbol completion. The code processing system can determine the completion type as single symbol completion or multiple symbol completion according to the pre-settings, or can determine the completion behavior as single symbol completion or multiple symbol completion according to the completion type set by the user when triggering the code completion function.
[0142] There are many ways to trigger the completion function. For example, the code processing system 100 can detect that the time when the user stops inputting reaches a preset time, and then determine to trigger the code completion function. For another example, the code processing system 100 can set a trigger condition, such as double-clicking the right button of the mouse, single-clicking a shortcut key (such as the tab key). When the code processing system 100 detects that the trigger condition is met, such as detecting that the user double-clicks the right button of the mouse or single-clicks the shortcut key, the code completion function is triggered. Furthermore, the user can also set the completion type to single-symbol completion or multi-symbol completion when triggering the code completion function.
[0143] In some possible implementations, the input code may include a prefix of the code to be completed, for example, a prefix of the identifier to be completed. Based on this, the code processing system 100 may also determine a completion condition based on the input code. The completion condition is specifically that the candidate items of the code to be completed include the above prefix. For an identifier with a length of N (N is a positive integer greater than 1), the prefix of the identifier may be any one of the first 1-digit code to the first N-1-digit code.
[0144] S406: The code processing system 100 determines at least one candidate item of the code to be completed from the context database according to the context feature of the code to be completed.
[0145] The context database stores sample codes and context features of the sample codes. The sample codes may include any one or more of identifiers such as class names, method names, function names, variable names, or parameter names. The sample codes may come from an open source dataset or a user's private dataset.
[0146] Specifically, taking the Java-based code completion scenario as an example, the code processing system 100 can index the code in open source data sets such as the GitHub corpus, thereby identifying identifiers such as class names, method names, function names, variable names, parameter names, operators, etc. in the code, and then determine the contextual features of each identifier, store the identifiers and the contextual features of the identifiers in the database, and thus obtain a context database.
[0147] In some possible implementations, the code processing system 100 may also index user-private data sets, such as code in a code repository provided by the user, to identify identifiers such as class names, method names, function names, variable names, parameter names, operators, and so on in the code, and then determine the contextual features of each identifier, and obtain a context database based on the identifier and its contextual features.
[0148] Taking accuracy into consideration, the code processing system 100 may also construct context databases based on the open source dataset and the user's private dataset, for example, construct a first context database and a second context database, wherein the first context database is used to store identifiers and context features of the identifiers in the open source dataset, and the second context database is used to store identifiers and context features of the identifiers in the user's private dataset.
[0149] After constructing the context database, the code processing system 100 (eg, the code analysis module 1042 in the completion subsystem 104) may perform code analysis according to the context features of the code, and determine at least one candidate item of the code to be completed from the context database.
[0150] Specifically, the code processing system 100 (eg, the code analysis module 1042 in the completion subsystem 104) searches the context database using a depth first search (DFS) algorithm according to the context features of the code to be completed, and determines at least one candidate item of the code to be completed.
[0151] The depth-first search algorithm is an algorithm for traversing a tree (such as an abstract syntax tree of a code) or a graph. Taking traversing a tree as an example, the nodes of the tree are traversed along the depth of the tree, and the branches of the tree are searched as deeply as possible. When all the edges where the node v is located have been explored, the search will trace back to the starting node of the edge where the node v was found. This process continues until all nodes reachable from the source node have been found. If there are still undiscovered nodes, one of them is selected as the source node and the above process is repeated. The whole process is repeated until all nodes have been visited.
[0152] The code processing system 100 may search for candidate items that match the context features of the code to be completed through a depth-first search algorithm. For each candidate item, the code processing system 100 may continue searching until a static function call is found.
[0153] Taking "Document doc=" as an example, the return value type is Document type, and the code processing system 100 can search for function calls or API calls with a return value type of Document type, such as newDocument() or parse(). For newDocument, the code processing system 100 continues to perform a deep search to determine that newDocument is called by DocumentBuilder, and then searches for calls that return DocumentBuilder, such as newDocumentBuilder(). Then the code processing system 100 continues to perform a deep search based on newDocumentBuilder(). Specifically, the code processing system 100 determines that DocumentBuilder is called by DocumentBuilderFactory, and the code processing system 100 searches and returns DocumentBuilderFactory.newInstance.
[0154] Since DocumentBuilderFactory.newInstance is a static function call, which can be directly called on the right side of the equal sign, the code processing system 100 can stop the deep search for newDocument and generate candidate items:
[0155] DocumnetBuilderFactory.newInstance().newdDocumentBuilder().newDocument().
[0156] The deep search process for parse() can refer to the deep search process for newDocument(), based on which the candidate items can be generated: DocumnetBuilderFactory.newInstance().newdDocumentBuilder().parse().
[0157] Furthermore, when the code input by the user includes the prefix of the code to be completed, the code processing system 100 (for example, the code analysis module 1042) can determine at least one candidate item that matches the prefix of the code to be completed from the searched candidates. Taking "Document doc = Doc" as an example, the prefix includes Doc, and the code processing system 100 can filter the candidates whose prefix includes Doc from the searched return values of the candidate items of the Document type, and filter out the candidates whose prefix does not include Doc, such as DOMUtils.getOwnerDocument(new IIOMetadataNode("")).
[0158] In some possible implementations, the code processing system 100 may also determine an evaluation index value of at least one candidate item, and then filter at least one candidate item according to the evaluation index value, or sort the evaluation index values.
[0159] The evaluation index value may be a score value determined based on the statistical information of the candidate in the context database 1044, or a probability value of the recommendation probability determined based on the evaluation model. The statistical information may include usage information. For example, when the candidate includes a class name, the statistical information may include class usage information; for another example, when the candidate includes a method name, the statistical information may include method usage information.
[0160] The class usage information may include any one or more of the following information:
[0161] public int nestedCount;
[0162] public int extendsCount;
[0163] public int fieldCount;
[0164] public int assignCount;
[0165] public int ifCount;
[0166] public int finallyCount;
[0167] public int localVariableCount;
[0168] public int parameterCount;
[0169] public int newCount;
[0170] public int callBaseCount;
[0171] public int totalCount;
[0172] public int localCount;
[0173] public int samePackage.
[0174] Similarly, the method usage information may specifically include any one or more of the following information:
[0175] public int callCount;
[0176] public int constructorCount;
[0177] public int methodCount;
[0178] public int repeatCount;
[0179] public int firstCount;
[0180] public int boolCount;
[0181] public int finallyCount;
[0182] public int nestedCount;
[0183] public int rightAssignSide;
[0184] public int inReturn.
[0185] The code processing system 100 (e.g., the code analysis module 1042 in the completion subsystem 104) can determine the score of the candidate item based on the above usage information of the candidate item. Specifically, the code processing system 100 can assign weights to different usage information respectively, and then determine the score of the candidate item through weighted operations (e.g., weighted summation, weighted average).
[0186] The code processing system 100 can filter at least one candidate item according to the score value. For example, the code processing system 100 can filter out candidates with scores lower than a preset value, or candidates with scores ranked lower (such as after the top 10), so as to avoid recommending expired or deprecated APIs, etc., and improve accuracy. Furthermore, the code processing system 100 can also sort at least one candidate item according to the score value, so as to display the candidate items in order of high and low scores.
[0187] In some possible implementations, when the candidate items include function names, the code processing system 100 may also fill in parameters for at least one candidate item. Specifically, the code processing system 100 may use a depth-first search algorithm to search for parameters corresponding to the function, for example, to search for parameters corresponding to the function from the local code, and then fill in the parameters for the candidate items based on the searched parameters.
[0188] When filling parameters, the code processing system 100 (for example, the parameter filling module 1046 of the completion subsystem 104) can fill multiple sets of parameters for one candidate item to obtain multiple candidate items after filling the parameters. Figure 5 As shown, the code processing system 100 can sort the candidate items after filling in the multiple parameters according to information including the distance between the parameters and the code to be completed. For example, the code processing system 100 can display the candidate item 502 filled with the parameter with a closer distance in a front position.
[0189] In some possible implementations, when the candidate items include function names, the code processing system 100 may also provide the user with candidate items filled with parameters and candidate items without filling in parameters, so that when the candidate items filled with parameters do not meet the user's needs, the user can select the above-mentioned candidate items without filling in parameters and manually enter the parameters to avoid unnecessary correction operations.
[0190] After filling the parameters, the code processing system 100 can be used not only to complete the entire line of code, but also to further complete the entire code snippet. The essence of completing the code snippet is multi-symbol completion, and its specific implementation method can refer to the specific implementation method of completing the entire line of code.
[0191] Figure 6 It also uses the interface diagram to complete the entire code snippet. Figure 6 (A) is a schematic diagram of the interface before completing the code snippet. Figure 6 As shown in (A), the code entered by the user includes:
[0192] public static void documentBuilderMethod(){
[0193] String path=" / path / to / file";
[0194] Document doc=
[0195] Based on the above code, the code processing system 100 can identify the local variables path and file, and based on the document type, determine at least one candidate item including:
[0196] DocumnetBuilderFactory.newInstance().newdDocumentBuilder().parse()
[0197] Next, the code processing system 100 identifies file as a parameter of the candidate item, and needs to create file before this, and path is used as a parameter of file. In addition, the code processing system 100 identifies the parser configuration exception ParserConfigurationException, and completes the try catch statement in the code snippet according to the exception, such as Figure 6 As shown in (B), the bold and italic codes are the completed codes.
[0198] In some possible implementations, the code processing system 100 may also input at least one candidate (e.g., a candidate filled with parameters) and context features of the code to be completed into the evaluation model to obtain the recommendation probability of at least one candidate. Correspondingly, the code processing system 100 may determine a target candidate among at least one candidate based on the recommendation probability of at least one candidate. The target candidate is a candidate whose recommendation probability meets a preset condition, such as a candidate whose recommendation probability is greater than a preset probability value, or a candidate whose recommendation probability ranks high (e.g., the top N, where N is a positive integer).
[0199] Among them, the evaluation model can be obtained by training the initial model with samples collected from an open source data set or a user's private data set. Specifically, the code processing system 100 can construct an initial model, which can be a model including 2 or more hidden layers. In some embodiments, the initial model may include an input layer, two hidden layers, and an output layer. Among them, the hidden layer can be a fully connected layer (Dense layer), and the activation function of the hidden layer can be a hyperbolic function such as a hyperbolic tangent function TANH. The output layer includes a loss function, which can be a cross entropy loss function (cross entry, XENT) and the like.
[0200] Then, the code processing system 100 can input samples (including identifiers and contextual features of identifiers) collected from open source data sets or user private data sets into the initial model for training to iteratively update the parameters of the initial model. When the loss function of the model meets the training end condition, such as when the loss function tends to converge or is less than a preset value, the training can be stopped. The trained model can be used as an evaluation model to evaluate the probability that the candidate items for filling parameters are correct. Among them, the probability that the candidate items for filling parameters are correct can be used as the recommendation probability of the candidate items.
[0201] Among them, the evaluation model can also be implemented by a binary classification model. The binary classification model takes candidate items (for example, candidate items filled with parameters) and contextual features of the candidate items as input, and takes recommended labels as output. The binary classification model specifically matches the input candidate items and the contextual features of the candidate items with the existing identifier and the contextual features of the identifier to determine the recommended label. Among them, the recommended label can take values of 0, 1, or true or false. When the recommended label is 0 or false, it indicates that the candidate item is not recommended. When the recommended label is 1 or true, it indicates that the candidate item is recommended.
[0202] The code processing system 100 can further filter the candidate items according to the above-mentioned recommendation tags to improve the accuracy of predicting the candidate items, thereby improving the accuracy of code completion. The evaluation model can also obtain statistical information of the candidate items with the recommendation tag of 1, and determine the recommendation probability of the candidate items based on the statistical information, for example, determining the recommendation probability according to the score value.
[0203] In some possible implementations, a class method can be called in different environments. Correspondingly, the context of a class method can be different. Based on this, the code processing system 100 can determine the role of each method call according to the context characteristics of the method call. For example, for the getitem() method, it can be determined that the role of the method call includes get accessor (or read accessor), for the add() method, it can be determined that the role of the method call includes adder, and for the removeitem() method, it can be determined that the role of the method call includes remover.
[0204] When the code processing system 100 is training the evaluation model, it can also improve the accuracy of the evaluation model by adding features such as the role of the method call. In this way, the evaluation model can determine the recommendation probability of the candidate item in combination with the role of the method call, so that the candidate item recommended by the evaluation model is more in line with the user's intention, thereby achieving higher completion accuracy.
[0205] Taking into account the relationships between variables in the code, such as producer-consumer relationships, in most scenarios, circular references between variables are illegal. For example, variable A is the consumer of variable B, and variable B is also the consumer of variable A, which is usually illegal. Therefore, the code processing system 100 can also track the data flow to avoid circular references and improve completion accuracy.
[0206] S408: The code processing system 100 presents at least one candidate item to the user through the user interface.
[0207] Specifically, the code processing system 100 can present to the user, through a user interface such as a GUI, at least one candidate item determined by the code processing system 100 from the context database according to the context features of the code to be completed. In some possible implementations, the code processing system 100 also filters the above-mentioned at least one candidate item according to statistical information, and the code processing system 100 can present to the user, through a user interface such as a GUI, the candidate items filtered by the code processing system 100 according to the statistical information. Among them, the code processing system 100 can also sort the candidate items according to the statistical information, and then display the candidate items in sequence. For example, the code processing system 100 determines the scoring value of the candidate items according to the statistical information, and displays the candidate items in order of the scoring value.
[0208] In some possible implementations, the candidate items include function names, and the code processing system 100 also fills in parameters for the candidate items according to the local code. The code processing system 100 can present at least one candidate item after the code processing system 100 fills in the parameters to the user through a user interface such as a GUI.
[0209] In some possible implementations, the code processing system 100 also inputs the candidate items into the evaluation model for evaluation, obtains the recommendation probability of the candidate items, and determines the target candidate item among the at least one candidate item according to the recommendation probability of at least one candidate item, and the code processing system 100 can present the target candidate item to the user through a user interface such as a GUI. When the code processing system 100 displays the target candidate items, the candidate items can be displayed in the order of the recommendation probability.
[0210] Among them, the code to be completed is the code in the class method. The code processing system 100 also determines the recommendation probability of the candidate according to the role of the method call corresponding to the code to be completed, and then determines the target candidate. The code processing system 100 can also display the target candidate determined based on the role of the method call.
[0211] The following is a specific example. Figure 7 The effect diagram of code completion is shown in Figure 7 As shown, the user completes the method getFullName(). Specifically, after the user enters "ret", the code processing system 100 can determine the contextual features of "ret", such as the return type is string, and name and LastName are both string types.
[0212] When the evaluation model adopted by the code processing system 100 is not combined with the role of the method call for evaluation, such as Figure 7As shown in (A), the code processing system 100 can determine that the probability of returning LastName is higher than the probability of returning name based on the distance between the variable and the code to be completed. The code processing system 100 preferentially recommends returning LastName and then recommends returning name.
[0213] When the evaluation model adopted by the code processing system 100 is also combined with the role of method call for evaluation, such as Figure 7 As shown in (B), the code processing system 100 can determine that the roles of the method call include get accessor. The evaluation model of the code processing system 100 can determine whether LastName, name, and builder have the corresponding functions of getaccessor in order from near to far. Obviously, LastName and name do not have the corresponding functions, but builder has the corresponding functions. Therefore, the code processing system 100 gives priority to returning builder. Considering that the return type is string, toString is called through builder to return string type. At this time, the code processing system recommends returning builder.toString first.
[0214] Although Figure 7 The recommendation result shown in (A), such as return LastName, is syntactically correct and can be compiled, but it still has some differences from the user's intention. Figure 7 The recommendation result shown in (B) is closer to the user's true intention, further improving the accuracy. Figure 7 The method corresponding to (B) can predict unknown patterns and has good generalization performance.
[0215] In some possible implementations, the code processing system 100 further filters candidates with circular references from at least one candidate, and the code processing system 100 may present the candidate items after filtering the candidates with circular references to the user through a user interface such as a GUI.
[0216] The following is a specific example. Figure 8 The effect diagram of code completion shown in the figure first creates a new frame, then creates a new panel, and then adds the panel to the frame through the add method. Then the user enters panel.a to trigger code completion with a prefix. If data flow tracking is not performed, such as Figure 8 As shown in (A), the code processing system 100 returns add(frame) based on the distance priority, and a circular reference is formed between frame and panel. If data flow tracing is performed, such as Figure 8As shown in (B), the code processing system 100 can filter the candidate add(frame) of the circular reference and give priority to returning add(label). It can be seen that by tracking the data flow, the accuracy of code completion can be higher, and the user experience of code completion can be improved.
[0217] Furthermore, after presenting the candidate items (e.g., the candidate items filled with parameters) to the user, the code processing system 100 may also receive the candidate items selected by the user, and update the context database according to the candidate items and the context features. In some embodiments, the code processing system 100 may also update the data set used for training the model or testing the model according to the candidate items selected by the user and the context features of the candidate items.
[0218] It should be noted that the above embodiment is mainly described in detail by taking class method completion as an example. When completing a class name, a variable name, or a method without a return type, the context features of the code to be completed can be directly input into a pre-trained completion model to obtain candidate items. The completion model can specifically be a completion model based on statistical information.
[0219] Combination of the above Figures 1 to 8 The code processing method provided in the embodiment of the present application is introduced in detail. Next, the code processing system, code processing device and computing equipment for implementing the code processing function provided in the embodiment of the present application are introduced in conjunction with the accompanying drawings.
[0220] See also Figure 1 , the embodiment of the present application provides a code processing system 100, which is used to execute steps S402 to S408 in the aforementioned method embodiment, and the system optionally executes the optional methods in the aforementioned steps. The system includes IDE 102 and a completion subsystem 104. The composition of IDE 102 and the completion subsystem 104 and the functions of each component are described in the above related content, which will not be repeated here.
[0221] like Fig. 9 As shown, the embodiment of the present application further provides a code processing device 900, which is used to execute the aforementioned code processing method. The code processing device 900 may include the aforementioned Figure 1 The IDE plug-in 1024 in the system architecture described above and some or all modules in the aforementioned completion subsystem 104. The functional division of the code processing device 900 can be the same as the aforementioned Figure 1For example, the code processing device 900 includes an IDE plug-in 1024 and a completion subsystem 104, and the completion subsystem 104 further includes a code analysis module 1042 and a context database 1044. Optionally, the completion subsystem 104 may also include a parameter filling module 1046, an evaluation module 1048, and an index module 1049. The code processing device 900 may also have other functional unit division methods. The embodiment of the present application does not limit the division of the functional units in the device 900. The following exemplary division is provided:
[0222] The code processing device 900 includes an interface unit 902 , a feature extraction unit 904 and an analysis unit 906 .
[0223] The interface unit 902 is used to receive a code input by a user through a user interface;
[0224] A feature extraction unit 904 is used to determine the context feature of the code to be completed according to the code input by the user;
[0225] An analyzing unit 906 is configured to determine at least one candidate item of the code to be completed from a context database according to the context feature of the code to be completed, wherein the context database stores sample codes and the context features of the sample codes;
[0226] The interface unit 902 is further configured to present the at least one candidate item to the user through the user interface.
[0227] In some possible implementations, the analyzing unit 906 is further configured to:
[0228] Obtaining statistical information of the at least one candidate item in the context database;
[0229] filtering the at least one candidate item according to the statistical information;
[0230] The interface unit is specifically used for:
[0231] The filtered candidate items are presented to the user through the user interface.
[0232] In some possible implementations, the apparatus 900 further includes:
[0233] an evaluation unit, configured to input the context features of the at least one candidate item and the code to be completed into an evaluation model, obtain a recommendation probability of the at least one candidate item, and determine a target candidate item among the at least one candidate item according to the recommendation probability of the at least one candidate item;
[0234] The interface unit 902 is specifically used for:
[0235] The target candidates are presented to the user through the user interface.
[0236] In some possible implementations, the apparatus 900 further includes:
[0237] A parameter filling unit, used for filling the parameters of at least one candidate item according to the code in the code file where the code input by the user is located when the candidate item includes a function name;
[0238] The interface unit is specifically used for:
[0239] The at least one candidate item populated with the parameters is presented to the user via the user interface.
[0240] In some possible implementations, the analyzing unit 906 is specifically configured to:
[0241] According to the context features of the code to be completed, a context database is searched using a depth-first search algorithm to determine at least one candidate item of the code to be completed.
[0242] In some possible implementations, the code input by the user includes a prefix of the code to be completed;
[0243] The analysis unit 906 is specifically used for:
[0244] At least one candidate item matching a prefix of the code to be completed is determined from a context database according to the context feature of the code to be completed.
[0245] In some possible implementations, the context database includes at least one of a database constructed based on an open source dataset and a database constructed based on a private dataset of the user.
[0246] In some possible implementations, the code to be completed includes code in a class method, and the code input by the user includes a return type.
[0247] In some possible implementations, the apparatus 900 further includes:
[0248] An evaluation unit is used to determine a role of a method call corresponding to the code to be completed according to context features of the code to be completed, wherein the role is used to assist in determining a recommendation probability of a candidate item of the code to be completed.
[0249] In some possible implementations, the analyzing unit 906 is further configured to:
[0250] Filtering candidate items having circular references from the at least one candidate item;
[0251] The interface unit 902 is specifically used for:
[0252] The filtered candidate items are presented to the user through the user interface.
[0253] The code processing device 900 according to the embodiment of the present application may correspond to executing the method described in the embodiment of the present application, and the above and other operations and / or functions of each module / unit of the code processing device 900 are respectively to implement Figure 4 For the sake of brevity, the corresponding processes of each method in the illustrated embodiment are not described in detail here.
[0254] The above-mentioned code processing device 900 can be implemented by a computing device. Fig.10 A computing device is provided, such as Fig.10 As shown, the computing device 1000 can be specifically used to implement the above Fig. 9 The functions of the code processing device 900 in the illustrated embodiment.
[0255] The computing device 1000 includes a bus 1001, a processor 1002, a display 1003, and a memory 1004. The processor 1002, the memory 1004, and the display 1003 communicate with each other via the bus 1001.
[0256] The bus 1001 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.10 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0257] The processor 1002 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0258] The display 1003 is an input / output (I / O) device. The device can display electronic files such as code files on the screen for users to view. Depending on the manufacturing material, the display 1003 can be divided into a liquid crystal display (LCD), an organic light emitting diode (OLED) display, etc. Specifically, the display 1003 can display the code input by the user through the GUI, present the candidate items of the code to be completed to the user through the GUI, etc.
[0259] The memory 1004 may include a volatile memory, such as a random access memory (RAM). The memory 1004 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard drive (HDD), or a solid state drive (SSD).
[0260] The memory 1004 stores executable program codes, and the processor 1002 executes the executable program codes to execute the aforementioned code processing method. Specifically, the processor 1002 executes the aforementioned program codes to control the display 1003 to receive the code input by the user through a user interface such as a GUI, and then controls the display 1003 to transmit the code input by the user to the processor 1002 through the bus 1001. The processor 1002 can determine the context features of the code to be completed according to the code input by the user, and then determine at least one candidate item of the code to be completed from the context database according to the context features of the code to be completed, and then control the display 1003 to present at least one candidate item to the user through a user interface such as a GUI.
[0261] In some possible implementations, the processor 1002 may also control other interfaces to receive the code input by the user. Among them, the other interface may be a microphone, etc. Specifically, the microphone may receive the code input in the form of voice.
[0262] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by a computing device or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions that instruct the computing device to execute the above-mentioned code processing method applied to the code processing device.
[0263] The embodiment of the present application also provides a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computing device, the process or function described in the embodiment of the present application is generated in whole or in part.
[0264] The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website, computer or data center to another website, computer or data center via wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means.
[0265] The computer program product may be a software installation package, which may be downloaded and executed on a computing device when any of the aforementioned code processing methods is required.
[0266] The descriptions of the processes or structures corresponding to the above-mentioned figures have different emphases. For parts that are not described in detail in a certain process or structure, please refer to the relevant descriptions of other processes or structures.
Claims
1. A code processing method, characterized in that: The method comprises: Receiving a code input by a user through a user interface of an integrated development environment, wherein the code includes a prefix of a code to be completed; Determine one or more first candidate items that match the prefix of the code to be completed from a database including the code, wherein the first candidate items include a plurality of identifiers, the plurality of identifiers of the first candidate items include the prefix of the code to be completed, and the identifier includes one of the following: a class name, a function name, a variable name, and an operator; A first candidate item with parameters and other candidate items are presented through the user interface of the integrated development environment, wherein the parameters in the first candidate item are parameters corresponding to the function name in the first candidate item, and the other candidate items include a second candidate item with a single identifier, and the single identifier of the second candidate item includes a prefix of the code to be completed.
2. The method according to claim 1, characterized in that The method comprises: According to the code, the parameters of the function in the first candidate are filled.
3. The method according to claim 1 or 2, characterized in that: The determining, from a database including the code, one or more first candidate items that match the prefix of the code to be completed comprises: According to the context of the code to be completed in the code, one or more first candidate items matching the prefix of the code to be completed are determined from a database including the code.
4. The method according to claim 3, characterized in that The determining, according to the context of the code to be completed, from a database including the code, one or more first candidate items that match a prefix of the code to be completed, comprises: According to the context feature of the code to be completed, one or more first candidate items matching the prefix of the code to be completed are determined from the database, wherein the context feature is a feature expressing the context in which the code to be completed is located.
5. The method according to claim 4, characterized in that The context features include one or more of the following: The type of the base class, the class name of the base class, the prefix, the return type, and the Boolean characteristics.
6. The method according to any one of claims 1 to 5, characterized in that: The other candidates include the first candidate without parameters.
7. The method according to any one of claims 1 to 6, characterized in that: The database further includes a database constructed based on a private data set of the user; or, The database also includes a database constructed based on an open source dataset and a database constructed based on a private dataset of the user.
8. The method according to any one of claims 1 to 7, characterized in that: The presenting, through the user interface, the first candidate item and other candidate items including the parameter comprises: According to the probability of each of the first candidate items and the probability of each of the other candidate items, each of the first candidate items and each of the other candidate items are presented in sequence on the user interface.
9. The method according to any one of claims 1 to 8, characterized in that: The code is a code written in one computer language or a code written in a mixture of multiple computer languages.
10. The method according to any one of claims 1 to 9, characterized in that: The first candidate item and the second candidate item are distinguished on the user interface by different marks or different colors or different fonts.
11. A code processing device, characterized in that: The device comprises: An interface unit, used to receive a code input by a user through a user interface of an integrated development environment, wherein the code includes a prefix of a code to be completed; an analyzing unit, configured to determine, from a database including the code, one or more first candidate items that match a prefix of the code to be completed, wherein the first candidate items include a plurality of identifiers, the plurality of identifiers of the first candidate items include the prefix of the code to be completed, and the identifier includes one of the following: a class name, a function name, a variable name, and an operator; The interface unit is also used to present a first candidate item with parameters and other candidate items through the user interface of the integrated development environment, wherein the parameters in the first candidate item are parameters corresponding to the function name in the first candidate item, and the other candidate items include a second candidate item with a single identifier, and the single identifier of the second candidate item includes a prefix of the code to be completed.
12. The device according to claim 11, characterized in that The device comprises: A parameter filling unit is used to fill the parameters of the function in the first candidate according to the code.
13. The device according to claim 11 or 12, characterized in that The analyzing unit is used to determine one or more first candidate items matching a prefix of the code to be completed from a database including the code according to the context of the code to be completed in the code.
14. The device according to any one of claims 11 to 13, characterized in that The analyzing unit is used to determine one or more first candidate items matching the prefix of the code to be completed from the database according to the context feature of the code to be completed, wherein the context feature is a feature expressing the context in which the code to be completed is located.
15. The device according to claim 14, characterized in that The context features include one or more of the following: The type of the base class, the class name of the base class, the prefix, the return type, and the Boolean characteristics.
16. The device according to any one of claims 11 to 15, characterized in that The other candidates include the first candidate without parameters.
17. The device according to any one of claims 11 to 16, characterized in that The database further includes a database constructed based on a private data set of the user; or, The database also includes a database constructed based on an open source dataset and a database constructed based on a private dataset of the user.
18. The device according to any one of claims 11 to 17, characterized in that The interface unit is specifically used for: According to the probability of each of the first candidate items and the probability of each of the other candidate items, each of the first candidate items and each of the other candidate items are presented in sequence on the user interface.
19. The device according to any one of claims 11 to 18, characterized in that The code is a code written in one computer language or a code written in a mixture of multiple computer languages.
20. The device according to any one of claims 11 to 19, characterized in that The first candidate item and the second candidate item are distinguished on the user interface by different marks or different colors or different fonts.
21. A computing device, characterized in that The device comprises a processor and a memory; The processor is configured to execute instructions stored in the memory so that the computing device performs the method according to any one of claims 1 to 10.
22. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes instructions; when a computing device executes the instructions, the computing device performs the method according to any one of claims 1 to 10.
Citation Information
Patent Citations
A code completion method and device for a third-party library based on ace expansion
CN109901829A
Code completion method and device, storage medium and electronic device
CN110502227A
Code completion method and device, computing equipment and storage medium
CN110673836A
Code completion
US20200097261A1
Cited By
Method for supporting code completion during oversized file editing
CN118708170A
Code completion method and device, electronic equipment and storage medium
CN119149000A
Code editing method and system based on low-code development platform
CN119336310A
A code editing method and system based on a low-code development platform
CN119336310B
