Code completion method and device

By building a code relationship diagram and using semantic association, the problem that traditional code completion methods rely on a single code file context is solved, and more accurate and intelligent code completion is achieved, improving the comprehensiveness and accuracy of code completion.

CN119201121APending Publication Date: 2024-12-27ALIPAY (HANGZHOU) INFORMATION TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411217808.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-30
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

Traditional code completion methods mainly rely on the internal context of the currently edited code file, resulting in limited accuracy and intelligence of code completion, ignoring the relevant information in the entire code base.

Method used

By building a code relationship diagram, code completion is performed based on multiple code files, using semantic associations between code entities, querying the code snippets of the target node and its neighbor node to determine the code context, and inputting it into the code model to generate the completion result.

Benefits of technology

It improves the comprehensiveness and accuracy of code completion, can better understand the code structure and intention, and improves the intelligence and accuracy of completion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201121A_ABST
    Figure CN119201121A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code completion method and device, and the method comprises the steps: obtaining a to-be-completed current code snippet in a current code file; a target node matched with the current code snippet is inquired in a code relation graph, the code relation graph is constructed according to a plurality of code files in a code library, and the code files comprise other code files different from the current code file. A single node in the code relation graph represents a code entity corresponding to a code snippet, and a connection edge between the nodes represents a semantic association between the code entities. And determining the code context of the current code snippet according to the first code snippet corresponding to the neighbor node of the target node. And inputting the current code snippet and the code context into a code large model, so that the code large model generates a completion result of the current code snippet based on the code context.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] One or more embodiments of the present specification relate to the field of computer technology, and more particularly, to a code completion method and device. Background Art

[0002] Code completion technology has always been one of the research hotspots in the field of software engineering. This technology greatly reduces the workload of programmers and improves development efficiency, quality and experience. Currently, all mainstream integrated development environments (IDEs) include code completion as a core function.

[0003] However, traditional code completion methods mainly rely on the context within the currently edited code file, which limits the accuracy and intelligence of code completion. Summary of the invention

[0004] One or more embodiments of this specification describe a code completion method and device, which can complete the code more accurately and comprehensively.

[0005] In a first aspect, a code completion method is provided, comprising:

[0006] Get the current code snippet to be completed in the current code file;

[0007] Searching for a target node matching the current code snippet in a code relationship graph; the code relationship graph is constructed based on multiple code files in a code library, the multiple code files include other code files different from the current code file; a single node in the code relationship graph represents a code entity corresponding to the code snippet, and the connecting edges between the nodes represent semantic associations between the code entities;

[0008] Determining a code context of a current code fragment according to a first code fragment corresponding to a neighbor node of the target node;

[0009] The current code snippet and the code context are input into the code model so that the code model generates a completion result of the current code snippet based on the code context.

[0010] In a second aspect, a code completion device is provided, comprising:

[0011] The acquisition unit is used to obtain the current code snippet to be completed in the current code file;

[0012] A query unit, configured to query a target node matching the current code snippet in a code relationship graph; the code relationship graph is constructed based on a plurality of code files in a code library, the plurality of code files including other code files different from the current code file; a single node in the code relationship graph represents a code entity corresponding to the code snippet, and a connection edge between nodes represents a semantic association between code entities;

[0013] A determining unit, configured to determine a code context of a current code fragment according to a first code fragment corresponding to a neighboring node of the target node;

[0014] The input unit is used to input the current code snippet and the code context into the code model so that it generates a completion result for the current code snippet based on the code context.

[0015] According to a third aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed in a computer, the computer is caused to execute the method of the first aspect.

[0016] In a fourth aspect, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method of the first aspect is implemented.

[0017] The code completion method and device provided in one or more embodiments of this specification can, on the one hand, perform code completion based on multiple code files, rather than being limited to the current code file, thereby greatly improving the comprehensiveness of code completion. On the other hand, code completion can also be performed based on the semantic association between code entities, which helps to better understand the code structure and intent, thereby improving the accuracy and intelligence of completion. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0019] Figure 1 A schematic diagram of an implementation scenario of an embodiment disclosed in this specification;

[0020] Figure 2 A schematic diagram of a code relationship diagram in an example is shown;

[0021] Figure 3 A flowchart of a code completion method according to an embodiment of the present specification is shown;

[0022] Figure 4 A flowchart of a code completion method according to another embodiment of the present specification is shown;

[0023] Figure 5 A schematic diagram showing first and second code snippets in one example;

[0024] Figure 6 A schematic diagram showing a training method of a classification model in one example;

[0025] Figure 7 A schematic diagram of a code completion method according to another embodiment of the present specification is shown;

[0026] Figure 8 A schematic diagram of a code completion device according to an embodiment of the present specification is shown. DETAILED DESCRIPTION

[0027] The solution provided in this specification is described below in conjunction with the accompanying drawings.

[0028] As mentioned above, in traditional techniques, code completion mainly relies on the context within the currently edited code file, which ignores relevant information that may exist in the entire code base. For example, the definition of some functions or classes may be in other code files. Although some improvement schemes try to utilize cross-file context, these schemes usually only consider some related files or code segments, and ignore other information that may be useful for completion.

[0029] To this end, in an embodiment of the present specification, an improved solution is proposed, namely, code completion based on a code relationship graph. Since the code relationship graph is constructed based on multiple code files and can reflect the semantic relationship between code entities, this solution can greatly improve the comprehensiveness and accuracy of code completion.

[0030] Figure 1 A schematic diagram of an implementation scenario of an embodiment disclosed in this specification. Figure 1 In the method, the current code snippet to be completed is first obtained, and based on the current code snippet, the code relationship graph is queried to determine the target node v that matches it. Then, based on the code snippet corresponding to the neighbor node u of the target node v, the code context of the current code snippet is determined. Finally, the current code snippet and the code context are input into the code big model to obtain the completion result of the current code snippet.

[0031] The following first describes the construction process of the above code relationship diagram.

[0032] In this solution, a code relationship graph may be constructed based on multiple code files in a code library, where the multiple code files include a current code file and other code files different from the current code file.

[0033] Specifically, for any code file, you can first use a syntax parsing tool to parse it into an Abstract Syntax Tree (AST), which is used to describe the grammatical structure of code statements, where each node represents a structure, such as an expression, statement, or declaration. Then, you can extract important code entities from the abstract syntax tree, such as modules, classes, methods, or variables. Finally, analyze the semantic associations between the extracted code entities, represent the code entities as nodes, and represent the semantic associations between code entities as connecting edges to construct a code relationship graph.

[0034] The above semantic associations include but are not limited to the following:

[0035] The construction relationship between methods and class instances: that is, a method directly constructs or initializes an instance of a class;

[0036] The import relationship between two modules: one module is imported by another module;

[0037] A base class relationship between two classes: that is, one class is the superclass of another class;

[0038] The overriding relationship between the subclass method and the superclass method: that is, the method in the subclass overrides the method in its superclass;

[0039] The calling relationship between two methods: one method calls another method;

[0040] The instantiation relationship between methods and classes: that is, a method creates an instance of a specific class;

[0041] A usage relationship between two classes: that is, one class has a field of the type of another class, meaning that one class uses the other class as an attribute.

[0042] It should be noted that in this solution, for each code entity, the corresponding entity attributes can also be associated and stored.

[0043] For example, for modules, the corresponding entity attributes may include: file path, module name, module signature, and code content, etc.; for classes, the corresponding entity attributes may include: file path, class name, class signature, code content, starting line number and ending line number, etc.; for methods, the corresponding entity attributes may include: file path, method name, method signature, method body, starting line number and ending line number, etc.; for variables, the corresponding entity attributes may include: file path, variable name, variable signature, starting line number and ending line number, etc.

[0044] The above entity signature (including module signature, etc.) refers to a code fragment that only contains key information in the code content of the corresponding code entity. For example, a module signature or class signature only contains variable definitions and / or method declarations in the corresponding code content, a method signature only contains method declarations in the corresponding code content, and a variable signature only contains variable definitions in the corresponding code content.

[0045] In addition, for the connection edges between code entities, corresponding edge attributes may also be associated and stored. The edge attributes may include the starting line number of the code statement corresponding to the semantic association represented by the connection edge, etc. For example, the starting line number of the code statement corresponding to the calling relationship between two methods.

[0046] Figure 2 A schematic diagram of a code relationship diagram in one example is shown. Figure 2 In the code relationship diagram, there are multiple nodes and connecting edges between the nodes. Each node represents a code entity such as a module, a class, a method, and a variable. Each connecting edge reflects the construction relationship, import relationship, base class relationship, override relationship, call relationship, instantiation relationship, and usage relationship between the code entities.

[0047] Figure 3 The flowchart of the code completion method according to one embodiment of the present specification is shown, and the method can be executed by any device, equipment, platform, or device cluster with computing and processing capabilities. Figure 3 As shown, the method may include the following steps:

[0048] Step S302, obtaining the current code snippet to be completed in the current code file.

[0049] In one embodiment, the code line where the cursor is located in the current code file can be directly used as the current code snippet to be completed.

[0050] In another embodiment, the N code lines starting from and including the code line where the cursor is located in the current code file can also be used as the current code fragment to be completed. In the following, the code line where the cursor is located is also referred to as the position to be completed.

[0051] Step S304: search the code relationship graph for a target node that matches the current code snippet.

[0052] Specifically, the target node can be queried in the code relationship graph based on the target position of the current code snippet. The target position here at least indicates the file path corresponding to the current code file to which the current code snippet belongs in the file system. In addition, the target position can also indicate the target line number of the position to be completed, that is, the line number of the code line where the cursor is located.

[0053] In the case where the target position indicates both the file path and the target line number, querying the target node in the code relationship graph may include determining from the code relationship graph several candidate nodes that meet the position matching condition, the position matching condition including that the candidate node belongs to the current code file, and the line number interval of the corresponding code snippet contains the target line number of the position to be completed. Select the candidate node with the smallest corresponding line number interval from the several candidate nodes as the target node. The line number interval here is composed of a starting line number and an ending line number.

[0054] It should be understood that the candidate node defined by the above position matching condition belongs to the current code file, which actually requires that the file path corresponding to the candidate node is the same as the file path in the target location. As mentioned above, each code entity in the code relationship graph is associated with a stored file path, so the candidate node can be selected by matching the file path in the target location with each file path corresponding to each code entity in the code relationship graph.

[0055] In addition, the position matching condition also stipulates that the line number interval corresponding to the candidate node must include the target line number. Since the entity attribute corresponding to the module does not include the line number interval, the above-mentioned candidate nodes only include the entity categories of class, method and variable.

[0056] Finally, usually, classes are contained in modules, methods are contained in classes, and variables are contained in methods. Therefore, the line number intervals corresponding to modules, classes, methods, and variables are from large to small. The above selection of the candidate node with the smallest line number interval is actually selecting the innermost code entity, such as a method.

[0057] Step S306: Determine the code context of the current code fragment according to the first code fragment corresponding to the neighbor node of the target node.

[0058] In one embodiment, the neighboring nodes mentioned above may satisfy the following condition: the starting row number corresponding to the connecting edge between the target node and the neighboring node is smaller than the target row number of the position to be completed.

[0059] It should be noted that the limitation here that the starting line number corresponding to the connecting edge is smaller than the target line number is actually to find a node that is semantically associated with the target node before the position to be completed. It should be understood that when the code fragment corresponding to the found node is used to complete the current code fragment, the success rate is higher.

[0060] Of course, in practice, the above conditions may also include: the neighbor node belongs to other code files, etc., which is not limited in this specification. Specifically, when the current code snippet contains N code lines including the line where the cursor is located, since the current code snippet already contains the context of the current code file, it can be limited that the neighbor node belongs to other code files to improve the effectiveness of the code context.

[0061] In addition, the first code snippet corresponding to the neighbor node may refer to the entity signature of the represented code entity, such as a method signature, etc. Since the entity signature usually only contains some key information, when determining the code context based on it, the complexity of the code context can be reduced, thereby helping to improve the efficiency of code completion.

[0062] Specifically, if there are multiple neighbor nodes, and the sum of the lengths of the first code snippets corresponding to each of them is less than a predetermined threshold, then the first code snippets can be directly used as the code context. If the sum of the lengths of the first code snippets is not less than the predetermined threshold, then the correlation scores between the first code snippets and the current code snippet can be calculated to select the final code snippet from the first code snippets as the code context. The calculation method of the correlation score will be described later.

[0063] Step S308, input the current code snippet and the code context into the code macro model, so that it generates a completion result for the current code snippet based on the code context.

[0064] Specifically, a prompt text may be constructed based on the current code segment and the code context, the prompt text indicating the completion result of the current code segment generated based on the code context. The prompt text is input into the large model to obtain the above completion result.

[0065] In summary, the code completion method provided in the embodiments of this specification, on the one hand, can perform code completion based on multiple code files, rather than being limited to the current code file, thereby greatly improving the accuracy of code completion. On the other hand, code completion can also be performed based on the semantic associations between code entities, which helps to better understand the code structure and intent, thereby improving the accuracy and intelligence of completion. In addition, this solution performs code completion based on semantic associations, rather than relying on the similarity of the content itself, which is a new idea for code completion.

[0066] The above is a code completion solution based on a code relationship graph provided in an embodiment of this specification. It is a solution for completing code from the dimension of semantic relevance. In practice, the final code context can also be determined by combining code snippets that are similar to the content of the current code segment itself, that is, the code is completed from the dimension of content similarity. The similar code snippets can provide useful hints and guidance, so that the accuracy of code completion can be further improved when completing code from the above two dimensions, which is described in detail below.

[0067] Figure 4A flowchart of a code completion method according to another embodiment of the present specification is shown, and the method can be executed by any device, equipment, platform, or device cluster with computing and processing capabilities. Figure 4 As shown, the method may include the following steps:

[0068] Step S402, obtaining the current code snippet to be completed in the current code file.

[0069] Step S404: search the code relationship graph for a target node that matches the current code snippet, and obtain a first code snippet corresponding to a neighboring node of the target node.

[0070] The detailed description of steps S402 to 404 can be found in steps S302 to S306 , which will not be repeated in this specification.

[0071] Step S406: Based on the current code snippet, a similar code snippet to the current code snippet is selected from various code snippets obtained by dividing other code files in the code library.

[0072] A single code snippet consists of several lines of code.

[0073] In one embodiment, in each code segment obtained by dividing the same code file, code lines contained in two adjacent code segments overlap with each other.

[0074] For example, assume that code file 1 is divided into three code snippets, wherein code snippet 1 includes code line 1 to code line 5, code snippet 2 includes code line 3 to code number 7, and code snippet 3 includes code line 5 to code line 9.

[0075] In one embodiment, selecting similar code snippets to the current code snippet includes selecting similar code snippets from various code snippets using an information retrieval algorithm based on the current code snippet.

[0076] The information retrieval algorithm here may be, for example, a BM25 algorithm, a term frequency-inverse document frequency (TF-IDF) algorithm, or the like.

[0077] In another embodiment, selecting similar code snippets to the current code snippet includes calculating an edit distance between the current code snippet and each code snippet, and selecting similar code snippets from each code snippet according to the edit distance.

[0078] The above-mentioned edit distance refers to the minimum number of edit operations required to convert two code snippets from one to another, and the larger the distance, the more different they are. The edit operation may include replacing one character with another character, inserting a character, and deleting a character.

[0079] It should be understood that the smaller the edit distance between two code snippets is, the more similar the two code snippets are.

[0080] Specifically, the code snippets may be sorted from small to large according to the edit distance, and then the first n (for example, 5) code snippets are selected from the sorted code snippets as the similar code snippets.

[0081] In another embodiment, the above-mentioned selecting similar code snippets to the current code snippet includes using a representation model to determine the representation vectors corresponding to the current code snippet and each code snippet, calculating the similarity between the current code snippet and each code snippet based on the representation vector, and selecting similar code snippets from each code snippet based on the similarity.

[0082] The above-mentioned representation model may be, for example, a Bert model, a Transformer encoder, etc.

[0083] Specifically, the code snippets may be sorted in descending order according to similarity, and then the first n (for example, 5) code snippets are selected from the sorted code snippets as the similar code snippets.

[0084] Step S408: obtaining a second code snippet following the similar code snippet.

[0085] When there are multiple similar code snippets, the second code snippet following each similar code snippet may be obtained.

[0086] Specifically, the start line number and the end line number of the similar code snippet can be respectively superimposed with a predetermined offset to obtain an updated start line number and an updated end line number. From the code snippets belonging to the same code file as the similar code snippet, a code snippet containing the updated start line number and the updated end line number is selected as the second code snippet.

[0087] For example, assuming that the code snippet 1 obtained by dividing the code file 1 is a similar code snippet to the current code snippet, that is, the corresponding starting line number and ending line number are 1 and 5 respectively, and assuming that the predetermined offset is 2, so that the updated starting line number and the updated ending line number are 3 and 7 respectively, then the second code snippet is code snippet 2. If the predetermined offset is 4, so that the updated starting line number and the updated ending line number are 5 and 9 respectively, then the second code snippet is code snippet 3.

[0088] It should be noted that the reason why the second code snippet following the similar code snippet is obtained is that only the offset code snippet may contain the content that needs to be completed in the current code snippet.

[0089] In practice, the above steps S402 to S404 and steps S406 to S408 may also be executed in parallel, or steps S406 to S408 may be executed first and then steps S402 to S404, which is not limited in this specification.

[0090] Step S410: determining a code context according to the first code snippet and the second code snippet.

[0091] It should be understood that, in practice, the number of the first code snippet and the second code snippet is usually multiple.

[0092] Figure 5 A schematic diagram of first and second code snippets in one example is shown. Figure 5 In the figure, the upper left corner is the current code file, "res=se l f.service." is the line where the cursor is located, that is, the position to be completed, and the 5 lines forward from this line form the current code snippet. The upper right corner is the 3 first code snippets: se1-se3, where se1 is the class signature, which only contains variable definitions; se2 is the class signature, which contains variable definitions and method declarations; se3 is the method signature, which only contains method declarations. At the bottom are the 2 second code snippets: si 1-si 2.

[0093] Specifically, the code context may be determined from the first code snippet and the second code snippet under a target constraint condition, wherein the target constraint condition includes that the length of the code context is less than a predetermined threshold and the model prediction performance based on the code context is optimal.

[0094] In one embodiment, the above-mentioned determination of the code context specifically includes: taking the first code snippet and the second code snippet as candidate code snippets, calculating the relevance score between the current code snippet and each candidate code snippet, the relevance score indicating the usefulness of the corresponding candidate code snippet, where the usefulness can be understood as whether it is helpful to complete the current code snippet. According to the relevance score, the code context is determined from each candidate code snippet.

[0095] The above correlation score can be calculated using the representation model. Specifically, the current code snippet and each candidate code snippet can be input into the representation model respectively to obtain the corresponding feature vectors, and then the similarity between the current code snippet and each candidate code snippet is calculated based on each feature vector, and the similarity is used as the above correlation score.

[0096] In practice, a pre-trained classification model can also be used to calculate the above relevance score. Specifically, for any first candidate code snippet, the current code snippet and the first candidate code snippet are concatenated and input into the pre-trained classification model, and the relevance score between the current code snippet and the first candidate code snippet is obtained through the output of the classification model. The training process of the classification model will be described later.

[0097] Similarly, the relevance scores between the current code snippet and each candidate code snippet can be obtained.

[0098] After calculating the above relevance score, the candidate code snippets can be sorted from large to small according to the relevance score. The first n candidate code snippets are selected from the sorted candidate code snippets as the code context, wherein the total length of the n candidate code snippets is less than a predetermined threshold, so that the total length of the code context is less than the predetermined threshold.

[0099] It should be understood that since the above relevance score can indicate the usefulness of the code snippet, the most relevant code snippet can be screened for the current code snippet based on the relevance score, thereby reducing unnecessary calculations and improving the real-time nature of completion. In addition, when the total length of the code context is less than a predetermined threshold, it helps to improve the reasoning speed, thereby ensuring that real-time and accurate code completion suggestions can be provided in actual development.

[0100] In summary, when determining the code context based on the above target constraints, this solution can control the complexity of reasoning while maintaining high completion performance.

[0101] In addition, this scheme uses the top n candidate code snippets as code contexts, and actually adopts a greedy strategy, which can ensure that the optimal code context can still be efficiently calculated and selected when there are a large number of candidate code snippets.

[0102] Step S412, input the current code snippet and the code context into the code macro model, so that it generates a completion result for the current code snippet based on the code context.

[0103] by Figure 5 Taking the current code snippet shown as an example, the completion result generated by the code model may be, for example: “validate_user(user.uid,user.token)”.

[0104] The training process of the above classification model is explained below.

[0105] Figure 6 A schematic diagram showing a method for training a classification model in one example. Figure 6, obtain a sample code snippet and several corresponding sample contexts. Mask the target content in the sample code snippet to obtain a target code snippet. Splice the target code snippet with each sample context respectively and input it into the code model to obtain each predicted content corresponding to each sample context. For any predicted content, if the predicted content is consistent with the target content, the corresponding sample context and sample code snippet are determined as positive samples. If the predicted content is inconsistent with the target content, the corresponding sample context and sample code snippet are determined as negative samples; in this way, multiple positive samples and multiple negative samples can be obtained. Based on multiple positive samples and multiple negative samples, a classification model is trained.

[0106] More specifically, the classification loss can be calculated based on the correlation score predicted by the classification model for each positive / negative sample and the positive / negative example label, using the cross entropy loss function. Afterwards, the back propagation method can be used to calculate the update gradient corresponding to the parameters of the classification model, and then the parameters of the classification model are updated based on the update gradient to obtain the trained classification model.

[0107] Figure 7 A schematic diagram of a code completion method according to yet another embodiment of the present specification is shown. Figure 7 In the example, for the current code snippet in the current code file, three code snippets can be selected from the code library based on content similarity, shown as rectangles; and three code snippets can be queried from the code relationship graph based on semantic relevance, shown as parallelograms. Next, the correlation scores of the six code snippets with the current code snippet can be calculated, and three code snippets can be selected based on the correlation scores. Finally, a prompt text can be constructed based on the three code snippets and the current code snippet, and the prompt text can be input into the code model to obtain the completion result of the current code snippet.

[0108] In summary, the code completion method provided in the embodiments of this specification can combine code snippets screened from the two dimensions of content similarity and semantic relevance to determine the code context, thereby expanding the scope of the code context, which helps to improve the accuracy of code completion and user satisfaction. In addition, when this solution is based on the above-mentioned target constraints to screen the final code snippets used to form the code context, it can streamline and optimize the context information. In other words, this solution can ensure that the most relevant and optimal code context is selected within a limited length, which not only improves the accuracy of completion, but also ensures the response speed, which meets the efficiency requirements of actual development.

[0109] Corresponding to the above code completion method, an embodiment of this specification also provides a code completion device, such as Figure 8 As shown, the device may include:

[0110] The acquisition unit 802 is used to acquire the current code snippet to be completed in the current code file.

[0111] The query unit 804 is used to query the target node matching the current code snippet in the code relationship graph, where the code relationship graph is constructed based on multiple code files in the code library, where the multiple code files include other code files different from the current code file. A single node in the code relationship graph represents a code entity corresponding to the code snippet, and the connection edges between the nodes represent the semantic associations between the code entities.

[0112] The determining unit 806 is configured to determine a code context of the current code fragment according to the first code fragment corresponding to the neighboring node of the target node.

[0113] The input unit 808 is used to input the current code snippet and the code context into the code model so that it generates a completion result for the current code snippet based on the code context.

[0114] A single code entity belongs to one of the following entity categories: module, class, method, and variable.

[0115] In addition, the above semantic association includes at least one of the following: an import relationship between two modules, a base class relationship between two classes, an override relationship between a subclass method and a superclass method, a call relationship between two methods, an instantiation relationship between a method and a class, and a usage relationship between two classes.

[0116] In one embodiment, the query unit 804 is specifically configured to:

[0117] Determine a number of candidate nodes that meet a position matching condition from the code relationship graph, where the position matching condition includes that the candidate node belongs to the current code file, and the line number interval of the corresponding code snippet contains the target line number of the position to be completed;

[0118] Select the candidate node with the smallest corresponding row number interval from several candidate nodes as the target node.

[0119] In one embodiment, the neighbor node belongs to other code files, and the starting line number corresponding to the connecting edge between the target node and the neighbor node is smaller than the target line number of the position to be completed.

[0120] In one embodiment, when the entity category of the code entity is a module or a class, the code snippet corresponding to the code entity only includes variable definitions and / or method declarations;

[0121] In the case where the entity category of the code entity is a method, the code snippet corresponding to the code entity includes only the method declaration;

[0122] When the entity category of the code entity is a variable, the code snippet corresponding to the code entity only includes the variable definition.

[0123] In one embodiment, the apparatus further comprises:

[0124] A selection unit 810 is used to select a similar code snippet to the current code snippet from various code snippets obtained by dividing other code files in the code library based on the current code snippet;

[0125] The acquisition unit 802 is further used to acquire a second code fragment following the similar code fragment;

[0126] The determining unit 806 is specifically configured to:

[0127] A code context is determined based on the first code snippet and the second code snippet.

[0128] In a more specific embodiment, the selection unit 810 is specifically used to:

[0129] Based on the current code snippet, use information retrieval algorithm to select similar code snippets from each code snippet; or,

[0130] By calculating the edit distance between the current code snippet and each code snippet, similar code snippets are selected from each code snippet; or,

[0131] The representation model is used to determine the representation vectors corresponding to the current code snippet and each code snippet, the similarity between the current code snippet and each code snippet is calculated based on the representation vector, and similar code snippets are selected from each code snippet based on the similarity.

[0132] In one embodiment, the acquisition unit 802 is specifically used to:

[0133] Adding a predetermined offset to the start line number and the end line number of the similar code fragment respectively to obtain an updated start line number and an updated end line number;

[0134] From the code snippets that belong to the same code file as the similar code snippet, a code snippet that includes an update start line number and an update end line number is selected as the second code snippet.

[0135] In one embodiment, the determining unit 806 includes:

[0136] A determination submodule 8062 is configured to use the first code segment and the second code segment as candidate code segments;

[0137] A calculation submodule 8064 is used to calculate a relevance score between the current code snippet and each candidate code snippet, where the relevance score indicates the usefulness of the corresponding candidate code snippet;

[0138] The determination submodule 8062 is further configured to determine a code context from each candidate code snippet according to the relevance score.

[0139] The calculation submodule 8064 is specifically used for:

[0140] For any first candidate code snippet, the current code snippet and the first candidate code snippet are concatenated and input into a pre-trained classification model, and a correlation score between the current code snippet and the first candidate code snippet is obtained through the output of the classification model.

[0141] In one embodiment, the apparatus further includes: a masking unit 812 and a training unit 814;

[0142] The acquisition unit 802 is further used to acquire the sample code snippet and the corresponding sample context;

[0143] A masking unit 812 is used to mask the target content in the sample code fragment to obtain a target code fragment;

[0144] The input unit 808 is also used to input the target code snippet and the sample context into the code macro model to obtain the predicted content;

[0145] A determination unit 806 is configured to determine the sample context and the sample code segment as a positive sample if the predicted content is consistent with the target content; and determine the sample context and the sample code segment as a negative sample if the predicted content is inconsistent with the target content;

[0146] The training unit 814 is used to train the classification model based on the positive samples and the negative samples.

[0147] In one embodiment, the determination submodule 8062 is specifically used for:

[0148] Sort the candidate code snippets according to their relevance scores from large to small;

[0149] The first n candidate code snippets are selected from the sorted candidate code snippets as code contexts, wherein the total length of the n candidate code snippets is less than a predetermined threshold.

[0150] The functions of the functional units of the device in the above-mentioned embodiment of this specification can be implemented through the steps of the above-mentioned method embodiment. Therefore, the specific working process of the device provided by one embodiment of this specification will not be repeated here.

[0151] A code completion device provided in an embodiment of the present specification can complete the code more accurately and comprehensively.

[0152] According to another embodiment, there is also provided a computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute a combination of Figure 3 or Figure 4 The method described.

[0153] According to another embodiment of the present invention, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the Figure 3 or Figure 4 The method described.

[0154] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the medium or device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0155] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0156] The specific implementation methods described above further illustrate the purpose, technical solutions and beneficial effects of this specification. It should be understood that the above description is only the specific implementation method of this specification and is not intended to limit the protection scope of this specification. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solutions of this specification should be included in the protection scope of this specification.

Claims

1. A code completion method, comprising: Get the current code snippet to be completed in the current code file; Search the code relationship graph for the target node that matches the current code snippet; The code relationship graph is constructed according to a plurality of code files in a code library, wherein the plurality of code files include other code files different from the current code file; A single node in the code relationship graph represents a code entity corresponding to a code snippet, and the connecting edges between the nodes represent the semantic associations between the code entities; Determining a code context of a current code fragment according to a first code fragment corresponding to a neighbor node of the target node; The current code snippet and the code context are input into the code model so that the code model generates a completion result of the current code snippet based on the code context.

2. The method according to claim 1, wherein: Individual code entities belong to one of the following entity categories: module, class, method, and variable.

3. The method according to claim 2, wherein: The semantic association includes at least one of the following: an import relationship between two modules, a base class relationship between two classes, an override relationship between a subclass method and a superclass method, a call relationship between two methods, an instantiation relationship between a method and a class, and a usage relationship between two classes.

4. The method according to claim 1, wherein: The step of searching the code relationship graph for a target node that matches the current code snippet includes: Determine a plurality of candidate nodes satisfying a position matching condition from the code relationship graph; the position matching condition includes that the candidate node belongs to the current code file, and the line number interval of the corresponding code snippet contains the target line number of the position to be completed; A candidate node with the smallest corresponding row number interval is selected from the plurality of candidate nodes as the target node.

5. The method according to claim 1, wherein: The neighbor node belongs to other code files, and the starting line number corresponding to the connecting edge between the target node and the neighbor node is smaller than the target line number of the position to be completed.

6. The method according to claim 1, wherein: In the case where the entity category of the code entity is a module or a class, the code snippet corresponding to the code entity only includes variable definitions and / or method declarations; In the case where the entity category of the code entity is a method, the code snippet corresponding to the code entity only includes a method declaration; In the case that the entity category of the code entity is a variable, the code snippet corresponding to the code entity only includes variable definition.

7. The method according to claim 1, further comprising: Based on the current code snippet, select a similar code snippet to the current code snippet from various code snippets obtained by dividing other code files in the code library; Obtaining a second code snippet following the similar code snippet; Determining the code context of the current code snippet includes: The code context is determined according to the first code snippet and the second code snippet.

8. The method according to claim 7, wherein: The selecting of similar code snippets to the current code snippet includes: Based on the current code snippet, an information retrieval algorithm is used to select the similar code snippet from the various code snippets; or, Selecting the similar code snippet from the code snippets by calculating the edit distance between the current code snippet and the code snippets; or, The representation model is used to determine the representation vectors corresponding to the current code snippet and each code snippet, and the similarity between the current code snippet and each code snippet is calculated based on the representation vector. Based on the similarity, the similar code snippet is selected from the code snippets.

9. The method according to claim 7, wherein: The obtaining of a second code snippet following the similar code snippet comprises: Adding a predetermined offset to the start line number and the end line number of the similar code fragment to obtain an updated start line number and an updated end line number; From the code snippets that belong to the same code file as the similar code snippet, select a code snippet that includes the update start line number and the update end line number as the second code snippet.

10. The method according to claim 7, wherein: The determining the code context according to the first code snippet and the second code snippet includes: taking the first code snippet and the second code snippet as candidate code snippets; Calculating a relevance score between the current code snippet and each candidate code snippet, the relevance score indicating the usefulness of the corresponding candidate code snippet; The code context is determined from the candidate code snippets according to the relevance score.

11. The method according to claim 10, wherein: The step of calculating the relevance score between the current code snippet and each candidate code snippet includes: For any first candidate code snippet, the current code snippet and the first candidate code snippet are concatenated and input into a pre-trained classification model, and a correlation score between the current code snippet and the first candidate code snippet is obtained through the output of the classification model.

12. The method according to claim 11, wherein: The classification model is trained by the following steps: Get sample code snippets and corresponding sample contexts; Masking the target content in the sample code fragment to obtain a target code fragment; Inputting the target code snippet and the sample context into the code macro model to obtain predicted content; If the predicted content is consistent with the target content, the sample context and the sample code segment are determined as positive samples; if the predicted content is inconsistent with the target content, the sample context and the sample code segment are determined as negative samples; The classification model is trained based on the positive samples and the negative samples.

13. The method according to claim 10, wherein: Determining the code context from the candidate code snippets includes: Sorting the candidate code snippets according to the relevance scores from large to small; The first n candidate code segments are selected from the sorted candidate code segments as the code context, wherein the total length of the n candidate code segments is less than a predetermined threshold.

14. A code completion device, comprising: The acquisition unit is used to obtain the current code snippet to be completed in the current code file; A query unit, configured to query a target node matching the current code snippet in a code relationship graph; the code relationship graph is constructed based on a plurality of code files in a code library, the plurality of code files including other code files different from the current code file; A single node in the code relationship graph represents a code entity corresponding to a code snippet, and the connecting edges between the nodes represent the semantic associations between the code entities; A determining unit, configured to determine a code context of a current code fragment according to a first code fragment corresponding to a neighboring node of the target node; The input unit is used to input the current code snippet and the code context into the code model so that it generates a completion result for the current code snippet based on the code context.

15. The device according to claim 14, wherein: The query unit is specifically used for: Determine a plurality of candidate nodes satisfying a position matching condition from the code relationship graph; the position matching condition includes that the candidate node belongs to the current code file, and the line number interval of the corresponding code snippet contains the target line number of the position to be completed; A candidate node with the smallest corresponding row number interval is selected from the plurality of candidate nodes as the target node.

16. The device according to claim 14, wherein: The neighbor node belongs to other code files, and the starting line number corresponding to the connecting edge between the target node and the neighbor node is smaller than the target line number of the position to be completed.

17. The apparatus according to claim 14, further comprising: A selection unit is used to select a similar code snippet to the current code snippet from various code snippets obtained by dividing other code files in the code library based on the current code snippet; The acquisition unit is further used to acquire a second code fragment following the similar code fragment; The determining unit is specifically used for: The code context is determined according to the first code snippet and the second code snippet.

18. The device according to claim 17, wherein: The selection unit is specifically used for: Based on the current code snippet, an information retrieval algorithm is used to select the similar code snippet from the various code snippets; or, Selecting the similar code snippet from the code snippets by calculating the edit distance between the current code snippet and the code snippets; or, The representation model is used to determine the representation vectors corresponding to the current code snippet and each code snippet, and the similarity between the current code snippet and each code snippet is calculated based on the representation vector. Based on the similarity, the similar code snippet is selected from the code snippets.

19. The device according to claim 17, wherein: The acquisition unit is specifically used for: Adding a predetermined offset to the start line number and the end line number of the similar code fragment to obtain an updated start line number and an updated end line number; From the code snippets that belong to the same code file as the similar code snippet, select a code snippet that includes the update start line number and the update end line number as the second code snippet.

20. The device according to claim 17, wherein: The determining unit comprises: Determine a submodule, configured to use the first code snippet and the second code snippet as candidate code snippets; A calculation submodule, used to calculate a relevance score between the current code snippet and each candidate code snippet, the relevance score indicating the usefulness of the corresponding candidate code snippet; The determination submodule is further configured to determine the code context from the candidate code snippets according to the relevance score.

21. The device according to claim 20, wherein: The computing submodule is specifically used for: For any first candidate code snippet, the current code snippet and the first candidate code snippet are concatenated and input into a pre-trained classification model, and a correlation score between the current code snippet and the first candidate code snippet is obtained through the output of the classification model.

22. The apparatus according to claim 21, further comprising: masking units and training units; The acquisition unit is further used to acquire the sample code snippet and the corresponding sample context; The masking unit is used to mask the target content in the sample code fragment to obtain a target code fragment; The input unit is further used to input the target code snippet and the sample context into the code macro model to obtain predicted content; The determination unit is configured to determine the sample context and the sample code segment as positive samples if the predicted content is consistent with the target content; and to determine the sample context and the sample code segment as negative samples if the predicted content is inconsistent with the target content; The training unit is used to train the classification model based on the positive samples and the negative samples.

23. The device according to claim 20, wherein: The determination submodule is specifically used for: Sorting the candidate code snippets according to the relevance scores from large to small; The first n candidate code segments are selected from the sorted candidate code segments as the code context, wherein the total length of the n candidate code segments is less than a predetermined threshold.

24. A computer-readable storage medium having a computer program stored thereon, wherein: When the computer program is executed in a computer, the computer is caused to execute the method according to any one of claims 1 to 13.

25. A computing device comprising a memory and a processor, wherein: The memory stores executable codes, and when the processor executes the executable codes, the method according to any one of claims 1 to 13 is implemented.

Citation Information

Cited By

  • Method and device for determining calling relation of generated code and storage medium

    CN119917075A

  • Code completion method and apparatus

    WO2026045722A1