Code completion method and apparatus
By constructing a code relationship graph and utilizing a large code model, based on semantic association and content similarity, the limitations of accuracy and intelligence in traditional code completion methods are solved, achieving more comprehensive and accurate code completion.
Patent Information
- Application Number
- PCT/CN2025/108426
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-08-30
- Filing Date
- 2025-07-14
- Publication Date
- 2026-03-05
AI Technical Summary
Traditional code completion methods rely primarily on the context within the currently edited code file, which limits the accuracy and intelligence of code completion and fails to fully utilize relevant information from the entire codebase.
By constructing a code relationship graph, reflecting the semantic relationships between code entities based on multiple code files, querying the neighboring nodes of the target node to determine the code context, and combining the large code model to generate completion results.
It improves the comprehensiveness and accuracy of code completion, enabling a better understanding of code structure and intent, and enhancing the intelligence and accuracy of completion.
Smart Images

Figure CN2025108426_05032026_PF_FP_ABST
Abstract
Description
Code completion methods and devices
[0001] This application claims priority to Chinese patent application filed on August 30, 2024, with application number 202411217808.5 and entitled "Code Completion Method and Apparatus", the entire contents of which are incorporated herein by reference. Technical Field
[0002] This specification relates to one or more embodiments in the field of computer technology, and more particularly to a code completion method and apparatus. Background Technology
[0003] Code completion technology has always been a research hotspot 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 auto-completion as a core function.
[0004] However, traditional code completion methods rely heavily on the context within the currently edited code file, which limits the accuracy and intelligence of code completion. Summary of the Invention
[0005] This specification describes a code completion method and apparatus through one or more embodiments, which can complete code more accurately and comprehensively.
[0006] Firstly, a code completion method is provided, including:
[0007] Get the current code segment in the current code file that needs to be completed;
[0008] The code relationship graph is used to query target nodes that match the current code snippet; the code relationship graph is constructed based on multiple code files in the code library, 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 the connecting edges between nodes represent semantic associations between code entities;
[0009] The code context of the current code segment is determined based on the first code segment corresponding to the neighboring nodes of the target node;
[0010] Input the current code snippet and the code context into the large code model, so that it generates the completion result of the current code snippet based on the code context.
[0011] Secondly, a code completion device is provided, comprising:
[0012] The acquisition unit is used to acquire the current code segment in the current code file that needs to be completed;
[0013] A query unit is used to query a target node in a code relationship graph that matches the current code fragment; the code relationship graph is constructed based on multiple code files in a code library, 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 fragment, and the connecting edges between nodes represent semantic associations between code entities;
[0014] The determining unit is used to determine the code context of the current code segment based on the first code segment corresponding to the neighboring nodes of the target node;
[0015] The input unit is used to input the current code fragment and the code context into the code big model, so that it generates the completion result of the current code fragment based on the code context.
[0016] Thirdly, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed in a computer, causes the computer to perform the method of the first aspect.
[0017] Fourthly, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and the processor executes the executable code to implement the method of the first aspect.
[0018] The code completion method and apparatus 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, it can also perform code completion based on the semantic relationships between code entities, which helps to better understand the code structure and intent, thereby improving the accuracy and intelligence of completion. Attached Figure Description
[0019] To more clearly illustrate the technical solutions of the embodiments in this specification, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this specification. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 is a schematic diagram of an implementation scenario of an embodiment disclosed in this specification;
[0021] Figure 2 shows a schematic diagram of the code relationships in one example;
[0022] Figure 3 shows a flowchart of a code completion method according to an embodiment of this specification;
[0023] Figure 4 shows a flowchart of a code completion method according to another embodiment of this specification;
[0024] Figure 5 shows schematic diagrams of the first and second code snippets in one example;
[0025] Figure 6 illustrates a schematic diagram of the training method for a classification model in one example;
[0026] Figure 7 shows a schematic diagram of a code completion method according to yet another embodiment of this specification;
[0027] Figure 8 shows a schematic diagram of a code completion device according to one embodiment of this specification. Detailed Implementation
[0028] The solution provided in this specification will now be described with reference to the accompanying drawings.
[0029] As mentioned earlier, traditional techniques primarily rely on the context within the currently edited code file for code completion, which ignores relevant information that may exist throughout the entire codebase. For example, the definitions of certain functions or classes may be in other code files. While some improvements attempt to utilize cross-file context, these solutions typically only consider relevant files or code segments, ignoring other information that might be useful for completion.
[0030] Therefore, in the embodiments of this specification, an improved scheme is proposed, namely, code completion based on a code relationship diagram. Since the code relationship diagram is constructed based on multiple code files and can reflect the semantic relationship between code entities, this scheme can greatly improve the comprehensiveness and accuracy of code completion.
[0031] Figure 1 is a schematic diagram of an implementation scenario of an embodiment disclosed in this specification. In Figure 1, the current code segment to be completed is first obtained, and based on the current code segment, the code relationship graph is queried to determine the target node v that matches it. Then, based on the code segments corresponding to the neighboring nodes u of the target node v, the code context of the current code segment is determined. Finally, the current code segment and the code context are input into the large code model to obtain the completion result of the current code segment.
[0032] The following section will explain the construction process of the above code relationship diagram.
[0033] In this solution, a code relationship graph can be constructed based on multiple code files in the code repository, including the current code file and other code files different from the current code file.
[0034] Specifically, for any code file, a syntax parsing tool can first be used to parse it into an Abstract Syntax Tree (AST). This AST describes the syntactic structure of the code statements, where each node represents a structure, such as an expression, statement, or declaration. Then, important code entities, such as modules, classes, methods, or variables, can be extracted from this AST. Finally, the semantic relationships between the extracted code entities are analyzed, and the code entities are represented as nodes, while the semantic relationships between them are represented as edges, thus constructing a code relationship graph.
[0035] The semantic associations mentioned above include, but are not limited to, the following:
[0036] The construction relationship between methods and class instances: that is, a method directly constructs or initializes an instance of the class;
[0037] Import relationship between two modules: that is, one module is imported by another module;
[0038] Base class relationship between two classes: that is, one class is the superclass of the other class;
[0039] Overriding relationship between subclass methods and superclass methods: that is, a method in a subclass overrides a method in its superclass.
[0040] The calling relationship between two methods: that is, one method calls another method;
[0041] Instantiation relationship between methods and classes: that is, a method creates an instance of a specific class;
[0042] The usage relationship between two classes: that is, if one class has a field of another class type, it means that one class uses the other class as an attribute.
[0043] It should be noted that in this solution, for each code entity, the corresponding entity attributes can also be associated and stored.
[0044] For example, for a module, the corresponding entity attributes may include: file path, module name, module signature, and code content; for a class, the corresponding entity attributes may include: file path, class name, class signature, code content, start line number, and end line number; for a method, the corresponding entity attributes may include: file path, method name, method signature, method body, start line number, and end line number; for a variable, the corresponding entity attributes may include: file path, variable name, variable signature, start line number, and end line number.
[0045] The aforementioned entity signature (including module signatures, etc.) refers to a code fragment that contains only key information from the code content of the corresponding code entity. For example, a module signature or class signature contains only variable definitions and / or method declarations from the corresponding code content, a method signature contains only method declarations from the corresponding code content, and a variable signature contains only variable definitions from the corresponding code content.
[0046] Furthermore, for the connecting edges between code entities, corresponding edge attributes can also be stored. These edge attributes can include, for example, the starting line number of the code statement corresponding to the semantic association represented by the connecting edge. For instance, the starting line number of the code statement corresponding to the call relationship between two methods.
[0047] Figure 2 shows a code relationship diagram in an example. In Figure 2, the code relationship diagram includes multiple nodes and connecting edges between nodes. Each node represents a code entity such as a module, class, method, and variable. Each connecting edge reflects the construction relationship, import relationship, base class relationship, overriding relationship, calling relationship, instantiation relationship, and usage relationship between code entities.
[0048] Figure 3 shows a flowchart of a code completion method according to an embodiment of this specification. This method can be executed by any device, apparatus, platform, or cluster of devices with computing and processing capabilities. As shown in Figure 3, the method may include the following steps:
[0049] Step S302: Obtain the current code segment to be completed in the current code file.
[0050] In one embodiment, the line of code where the cursor is located in the current code file can be directly used as the current code segment to be completed.
[0051] In another embodiment, the current code segment to be completed can be the N lines of code in the current code file starting from and including the line where the cursor is located. In the following text, the line where the cursor is located is also referred to as the completion position.
[0052] Step S304: Query the target node that matches the current code fragment in the code relationship graph.
[0053] Specifically, the target node can be queried in the code relationship graph based on the target position of the current code snippet. Here, the target position at least indicates the file path of the current code file to which the current code snippet belongs in the file system. Furthermore, the target position can also indicate the target line number of the location to be completed, i.e., the line number of the code line where the cursor is located.
[0054] When the target location indicates both the file path and the target line number, querying the target node in the code relationship graph can include identifying several candidate nodes that meet the position matching criteria. These criteria include that the candidate node belongs to the current code file and that the line number range of its corresponding code segment contains the target line number of the location to be completed. The candidate node with the smallest corresponding line number range is then selected as the target node. Here, the line number range consists of a start line number and an end line number.
[0055] It should be understood that the candidate nodes specified by the above location matching conditions belong 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 earlier, each code entity in the code relationship graph is associated with and stored as a file path. Therefore, candidate nodes can be selected by matching the file path in the target location with the file paths corresponding to each code entity in the code relationship graph.
[0056] In addition, the location matching condition also requires that the line number range corresponding to the candidate node must contain the target line number. Since the entity attributes corresponding to the module do not include the line number range, the above candidate nodes only include entity categories such as class, method and variable.
[0057] Finally, typically, classes are contained within modules, methods are contained within classes, and variables are contained within methods. Thus, the line number ranges corresponding to these four items are arranged from largest to smallest. The above selection of the candidate node with the smallest line number range is actually selecting the innermost code entity, such as a method.
[0058] Step S306: Determine the code context of the current code segment based on the first code segment corresponding to the neighboring nodes of the target node.
[0059] In one embodiment, the neighboring nodes mentioned above can satisfy the following condition: the starting row number of the connection edge between the target node and the neighboring node is less than the target row number of the position to be filled.
[0060] It should be noted that the starting line number of the connecting edge is limited to be less than the target line number. This is actually to find nodes that are semantically related to the target node before the position to be completed. It should be understood that when completing the current code segment based on the code segment corresponding to the found node, the success rate is relatively high.
[0061] Of course, in practice, the above conditions may also include: the neighboring node belongs to another code file, etc., which this specification does not limit. Specifically, when the current code segment contains N lines of code, including the line where the cursor is located, since the current code segment already contains the context of the current code file, it can be limited that the neighboring node belongs to another code file, thereby improving the effectiveness of the code context.
[0062] Furthermore, the first code fragment corresponding to the aforementioned neighbor node can refer to the entity signature of the represented code entity, such as a method signature. Since entity signatures typically contain only some key information, the complexity of the code context can be reduced when determining the aforementioned code context based on it, thereby helping to improve code completion efficiency.
[0063] Specifically, if there are multiple neighboring nodes, and the sum of the lengths of their respective first code segments is less than a predetermined threshold, then each first code segment can be directly used as the code context. If the sum of the lengths of the first code segments is not less than the predetermined threshold, then the final code segment can be selected as the code context by calculating the relevance score between each first code segment and the current code segment. The calculation method for the relevance score will be explained later.
[0064] Step S308: Input the current code snippet and code context into the large code model, so that it generates the completion result of the current code snippet based on the code context.
[0065] Specifically, a suggestion text can be constructed based on the current code segment and code context. This suggestion text indicates how to generate the completion result for the current code segment based on the code context. Inputting the suggestion text into a large model yields the completion result described above.
[0066] In summary, the code completion method provided in 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 accuracy of code completion. On the other hand, it can also perform code completion based on semantic relationships between code entities, which helps to better understand the code structure and intent, thus improving the accuracy and intelligence of completion. Furthermore, this solution performs code completion based on semantic relationships, rather than relying on the similarity of the content itself, representing a novel approach to code completion.
[0067] The above is a code completion scheme based on code relationship graphs provided in the embodiments of this specification. It is a scheme for completing code from the dimension of semantic relevance. In practice, it is also possible to combine code fragments that are similar to the content of the current code segment to determine the final code context, that is, to complete the code from the dimension of content similarity. The similar code fragments can provide useful hints and guidance, thereby further improving the accuracy of code completion when performing code completion from the above two dimensions. The following is a detailed explanation.
[0068] Figure 4 shows a flowchart of a code completion method according to another embodiment of this specification. This method can be executed by any device, apparatus, platform, or cluster of devices with computing and processing capabilities. As shown in Figure 4, the method may include the following steps:
[0069] Step S402: Obtain the current code segment to be completed in the current code file.
[0070] Step S404: Query the target node that matches the current code fragment in the code relationship graph, and obtain the first code fragment corresponding to the neighbor node of the target node.
[0071] For a detailed description of steps S402-404, please refer to steps S302-S306. This specification will not repeat them here.
[0072] Step S406: Based on the current code fragment, select similar code fragments from the various code fragments obtained from other code files in the code library.
[0073] A single code snippet consists of several lines of code.
[0074] In one embodiment, in the code segments obtained from the division of the same code file, the lines of code contained in two adjacent code segments overlap.
[0075] For example, suppose code file 1 is divided into 3 code segments, where code segment 1 contains code lines 1-5, code segment 2 contains code lines 3-7, and code segment 3 contains code lines 5-9.
[0076] In one embodiment, selecting similar code segments for the current code segment includes selecting similar code segments from various code segments based on the current code segment using an information retrieval algorithm.
[0077] The information retrieval algorithms mentioned here can be, for example, the BM25 algorithm, the Term Frequency-Inverse Document Frequency (TF-IDF) algorithm, etc.
[0078] In another embodiment, the above-mentioned selection of similar code segments for the current code segment includes calculating the edit distance between the current code segment and each other code segment, and selecting similar code segments from each other code segment based on the edit distance.
[0079] The edit distance mentioned above refers to the minimum number of edit operations required to transform one code snippet into another; the greater the distance, the more different they are. The edit operations may include replacing one character with another, inserting a character, or deleting a character.
[0080] It should be understood that the smaller the edit distance between two code snippets, the more similar the two code snippets are.
[0081] Specifically, the code snippets can be sorted from smallest to largest edit distance, and then the top n (e.g., 5) code snippets can be selected from the sorted code snippets as the similar code snippets mentioned above.
[0082] In another embodiment, the above-mentioned selection of similar code segments of the current code segment includes: using a representation model to determine the representation vectors corresponding to the current code segment and each code segment respectively; calculating the similarity between the current code segment and each code segment based on the representation vectors; and selecting similar code segments from each code segment based on the similarity.
[0083] The aforementioned representation models can be, for example, the BERT model, the Transformer encoder, etc.
[0084] Specifically, the code snippets can be sorted from highest to lowest similarity, and then the top n (e.g., 5) code snippets can be selected from the sorted snippets as the similar code snippets mentioned above.
[0085] Step S408: Obtain the second code segment that follows the similar code segment.
[0086] When there are multiple similar code snippets, this can be used to obtain a second code snippet that follows each similar code snippet.
[0087] Specifically, the start and end line numbers of similar code snippets can be superimposed with a predetermined offset to obtain updated start and end line numbers. From the code snippets belonging to the same code file as the similar code snippet, the code snippet containing the updated start and end line numbers is selected as the second code snippet.
[0088] For example, suppose code segment 1 obtained from the above code file 1 is a similar code segment to the current code segment, with corresponding start and end line numbers of 1 and 5 respectively. Assume the predetermined offset is 2, and the updated start and end line numbers are 3 and 7 respectively. Then the above second code segment is code segment 2. However, if we assume the predetermined offset is 4, and the updated start and end line numbers are 5 and 9 respectively, then the above second code segment is code segment 3.
[0089] It should be noted that the reason for obtaining the second code segment that follows the similar code segment is that only the offset code segment may contain the content that needs to be completed in the current code segment.
[0090] In practice, steps S402-S404 and steps S406-S408 can be executed in parallel, or steps S406-S408 can be executed first, followed by steps S402-S404. This specification does not limit this.
[0091] Step S410: Determine the code context based on the first code snippet and the second code snippet.
[0092] It should be understood that in practice, there are usually multiple first and second code snippets.
[0093] Figure 5 illustrates the first and second code snippets in an example. In Figure 5, the top left corner represents the current code file; "res = self.service." is the line where the cursor is located, i.e., the position to be completed. The current code snippet consists of five lines preceding this line. The top right corner contains three first code snippets: se1-se3. se1 is the class signature, containing only variable definitions; se2 is the class signature, containing both variable definitions and method declarations; and se3 is the method signature, containing only method declarations. The bottom corner contains two second code snippets: si1-si2.
[0094] Specifically, the code context can be determined from the first code segment and the second code segment under target constraints. These target constraints include that the length of the code context is less than a predetermined threshold, and that the model prediction performance based on the code context is optimal.
[0095] In one embodiment, determining the code context specifically includes: using a first code segment and a second code segment as candidate code segments, calculating a relevance score between the current code segment and each candidate code segment. This relevance score indicates the usefulness of the corresponding candidate code segment, where usefulness can be understood as whether it helps in completing the current code segment. Based on this relevance score, the code context is determined from each candidate code segment.
[0096] The aforementioned relevance score can be calculated using a representation model. Specifically, the current code segment and each candidate code segment can be input into the representation model to obtain their respective feature vectors. Then, based on each feature vector, the similarity between the current code segment and each candidate code segment can be calculated, and this similarity can be used as the aforementioned relevance score.
[0097] In practice, a pre-trained classification model can also be used to calculate the aforementioned relevance score. Specifically, for any first candidate code segment, the current code segment and the first candidate code segment are concatenated and input into a pre-trained classification model. The relevance score between the current code segment and the first candidate code segment is obtained through the output of the classification model. The training process of the classification model will be explained later.
[0098] Similarly, a relevance score can be obtained between the current code snippet and each candidate code snippet.
[0099] After calculating the relevance scores, the candidate code segments can be sorted from highest to lowest relevance score. The top n candidate code segments are then selected as the code context, where the total length of these n candidate code segments is less than a predetermined threshold, thus ensuring that the total length of the code context is also less than the predetermined threshold.
[0100] It should be understood that since the relevance score mentioned above can indicate the usefulness of a code snippet, the most relevant code snippets can be selected based on this score, thereby reducing unnecessary calculations and improving the real-time performance of code completion. Furthermore, when the total length of the code context is less than a predetermined threshold, it helps to improve inference speed, thus ensuring that real-time and accurate code completion suggestions can be provided in actual development.
[0101] In summary, by determining the code context based on the aforementioned objective constraints, this approach can maintain high completion performance while controlling the complexity of reasoning.
[0102] Furthermore, this scheme uses the top n candidate code snippets as code contexts. The actual deletion 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.
[0103] Step S412: Input the current code snippet and code context into the large code model, so that it generates the completion result of the current code snippet based on the code context.
[0104] Taking the current code snippet shown in Figure 5 as an example, the completion result generated by the large code model can be, for example, "validate_user(user.uid,user.token)".
[0105] The training process of the above classification model is explained below.
[0106] Figure 6 illustrates a training method for a classification model in one example. In Figure 6, sample code snippets and their corresponding contexts are obtained. The target content within the sample code snippets is masked to obtain the target code snippets. The target code snippets are concatenated with each sample context and input into the large code model to obtain predicted content corresponding to each sample context. For any predicted content, if it matches the target content, the corresponding sample context and sample code snippet are designated as a positive sample. If the predicted content does not match the target content, the corresponding sample context and sample code snippet are designated as a negative sample; this process yields multiple positive and multiple negative samples. The classification model is then trained based on these multiple positive and multiple negative samples.
[0107] More specifically, the classification loss can be calculated using the cross-entropy loss function based on the relevance scores and positive / negative labels predicted by the classification model for each positive / negative sample. Then, backpropagation can be used to calculate the update gradient corresponding to the parameters of the classification model, and the parameters of the classification model can be updated based on this update gradient to obtain the trained classification model.
[0108] Figure 7 illustrates a code completion method according to another embodiment of this specification. In Figure 7, for the current code segment in the current code file, three code segments can be selected from the code library based on content similarity, shown by rectangles; and three code segments can be retrieved from the code relationship graph based on semantic relevance, shown by parallelograms. Next, for these six code segments, their relevance scores with the current code segment can be calculated, and based on these relevance scores, three code segments can be selected. Finally, a hint text can be constructed based on these three code segments and the current code segment, and the hint text can be input into the code big model to obtain the completion result of the current code segment.
[0109] In summary, the code completion method provided in this specification can determine the code context by combining code fragments selected from two dimensions: content similarity and semantic relevance. This expands the scope of the code context, which helps improve the accuracy of code completion and user satisfaction. Furthermore, when selecting the final code fragments used to constitute the code context based on the aforementioned target constraints, this solution can simplify 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, thus not only improving the accuracy of completion but also ensuring response speed, meeting the high-efficiency requirements of actual development.
[0110] Corresponding to the code completion method described above, one embodiment of this specification also provides a code completion device, as shown in FIG8, which may include:
[0111] Unit 802 is used to retrieve the current code segment to be completed in the current code file.
[0112] Query unit 804 is used to query a target node in a code relationship graph that matches the current code fragment. This code relationship graph is constructed based on multiple code files in a code repository, 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 fragment, and the connecting edges between nodes represent semantic relationships between code entities.
[0113] The determination unit 806 is used to determine the code context of the current code segment based on the first code segment corresponding to the neighboring nodes of the target node.
[0114] Input unit 808 is used to input the current code fragment and code context into the code big model, so that it generates the completion result of the current code fragment based on the code context.
[0115] Individual code entities belong to one of the following entity categories: module, class, method, and variable.
[0116] In addition, the aforementioned semantic relationships include at least one of the following: import relationship between two modules, base class relationship between two classes, overriding relationship between subclass method and superclass method, calling relationship between two methods, instantiation relationship between method and class, and usage relationship between two classes.
[0117] In one embodiment, the query unit 804 is specifically used for:
[0118] Several candidate nodes that meet the position matching conditions are identified from the code relationship graph. The position matching conditions include that the candidate node belongs to the current code file and the line number range of the corresponding code segment contains the target line number of the position to be filled.
[0119] Select the candidate node with the smallest corresponding row number interval from a number of candidate nodes as the target node.
[0120] In one embodiment, the neighboring node belongs to another code file, and the starting line number of the connection edge between the target node and the neighboring node is less than the target line number of the position to be filled.
[0121] In one embodiment, when the entity category of the code entity is a module or a class, the code fragment corresponding to the code entity only includes variable definitions and / or method declarations;
[0122] When the entity type of a code entity is a method, the code snippet corresponding to the code entity only includes the method declaration;
[0123] When the entity category of a code entity is a variable, the code snippet corresponding to the code entity only includes the variable definition.
[0124] In one embodiment, the device further includes:
[0125] The selection unit 810 is used to select similar code segments from the various code segments obtained from other code files in the code library based on the current code segment.
[0126] The acquisition unit 802 is also used to acquire a second code segment that follows a similar code segment;
[0127] The determination unit 806 is specifically used for:
[0128] Determine the code context based on the first and second code snippets.
[0129] In a more specific embodiment, the selection unit 810 is specifically used for:
[0130] Based on the current code snippet, an information retrieval algorithm is used to select similar code snippets from various code snippets; or,
[0131] By calculating the edit distance between the current code snippet and other code snippets, similar code snippets are selected from the existing code snippets; or,
[0132] Using a representation model, the representation vectors corresponding to the current code segment and each other code segment are determined. Based on the representation vectors, the similarity between the current code segment and each other code segment is calculated. Based on the similarity, similar code segments are selected from each other code segment.
[0133] In one embodiment, the acquisition unit 802 is specifically used for:
[0134] The start and end line numbers of similar code snippets are respectively superimposed with a predetermined offset to obtain the updated start line number and the updated end line number;
[0135] From the code snippets that belong to the same code file as the similar code snippets, select the code snippet that contains the start line number and the end line number of the update, and use it as the second code snippet.
[0136] In one embodiment, the determining unit 806 includes:
[0137] Determine submodule 8062, which is used to select the first code segment and the second code segment as candidate code segments;
[0138] The calculation submodule 8064 is used to calculate the relevance score between the current code segment and each candidate code segment. The relevance score indicates the usefulness of the corresponding candidate code segment.
[0139] The determination submodule 8062 is also used to determine the code context from each candidate code snippet based on the relevance score.
[0140] Specifically, the calculation submodule 8064 is used for:
[0141] For any first candidate code segment, the current code segment and the first candidate code segment are concatenated and input into a pre-trained classification model. The relevance score between the current code segment and the first candidate code segment is obtained through the output of the classification model.
[0142] In one embodiment, the device further includes: a masking unit 812 and a training unit 814;
[0143] The acquisition unit 802 is also used to acquire sample code snippets and their corresponding sample contexts;
[0144] The masking unit 812 is used to mask the target content in the sample code fragment to obtain the target code fragment;
[0145] Input unit 808 is also used to input the target code fragment and sample context into the code big model to obtain the predicted content;
[0146] The determining unit 806 is used to determine the sample context and sample code segment as positive samples if the predicted content is consistent with the target content; and to determine the sample context and sample code segment as negative samples if the predicted content is inconsistent with the target content.
[0147] Training unit 814 is used to train a classification model based on positive and negative samples.
[0148] In one embodiment, the determining submodule 8062 is specifically used for:
[0149] The candidate code snippets are sorted from highest to lowest based on their relevance scores.
[0150] Select the top n candidate code segments from the sorted candidate code segments as the code context, where the total length of the n candidate code segments is less than a predetermined threshold.
[0151] The functions of each functional unit of the apparatus in the above embodiments of this specification can be implemented through the steps of the above method embodiments. Therefore, the specific working process of the apparatus provided in one embodiment of this specification will not be repeated here.
[0152] The code completion device provided in one embodiment of this specification can complete the code more accurately and comprehensively.
[0153] According to another embodiment, a computer-readable storage medium is also provided, on which a computer program is stored, which, when executed in a computer, causes the computer to perform the method described in conjunction with FIG3 or FIG4.
[0154] According to another embodiment, a computing device is also provided, including a memory and a processor, wherein executable code is stored in the memory, and the processor, when executing the executable code, implements the method described in conjunction with FIG3 or FIG4.
[0155] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the medium or device embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0156] The foregoing has described specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than that shown in the embodiments and may still achieve the desired result. Furthermore, the processes depicted in the drawings do not necessarily require the specific or sequential order shown to achieve the desired result. In some embodiments, multitasking and parallel processing are possible or may be advantageous.
[0157] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of this specification. It should be understood that the above description is only a specific embodiment of this specification and is not intended to limit the scope of protection of this specification. Any modifications, equivalent substitutions, improvements, etc., made on the basis of the technical solution of this specification should be included within the scope of protection of this specification.
Claims
A code completion method, including: Get the current code segment in the current code file that needs to be completed; Search the code relationship graph for the target node that matches the current code snippet; The code relationship diagram is constructed based on multiple code files in the code repository, and the multiple code files include other code files that are different from the current code file; In the code relationship graph, a single node represents a code entity corresponding to a code fragment, and the connecting edges between nodes represent the semantic associations between code entities. The code context of the current code segment is determined based on the first code segment corresponding to the neighboring nodes of the target node; Input the current code snippet and the code context into the large code model, so that it generates the completion result of the current code snippet based on the code context. According to the method of claim 1, wherein, A single code entity belongs to one of the following entity categories: module, class, method, and variable. The method according to claim 2, wherein, The semantic associations include at least one of the following: import relationship between two modules, base class relationship between two classes, overriding relationship between subclass methods and superclass methods, calling relationship between two methods, instantiation relationship between methods and classes, and usage relationship between two classes. According to the method of claim 1, wherein, The step of querying the target node that matches the current code fragment in the code relationship graph includes: Several candidate nodes that meet the position matching conditions are determined from the code relationship graph; the position matching conditions include that the candidate node belongs to the current code file and the line number range of the corresponding code segment contains the target line number of the position to be filled. The candidate node with the smallest corresponding row number interval is selected from the candidate nodes as the target node. According to the method of claim 1, wherein, The neighboring node belongs to another code file, and the starting line number of the connection edge between the target node and the neighboring node is less than the target line number of the position to be filled. According to the method of claim 1, wherein, When the entity category of the code entity is a module or a class, the code fragment corresponding to the code entity only includes variable definitions and / or method declarations; When the entity category of the code entity is a method, the code fragment corresponding to the code entity only includes the method declaration; When the entity category of the code entity is a variable, the code fragment corresponding to the code entity only includes the variable definition. The method according to claim 1 further includes: Based on the current code snippet, select similar code snippets from the various code snippets obtained from other code files in the code library; Obtain a second code segment that follows the similar code segment; The process of determining the code context of the current code segment includes: The code context is determined based on the first code snippet and the second code snippet. The method according to claim 7, wherein, The step of selecting similar code segments to the current code segment includes: Based on the current code fragment, an information retrieval algorithm is used to select the similar code fragments from the various code fragments; or, The similar code snippets are selected from the various code snippets by calculating the edit distance between the current code snippet and each of the other code snippets; or, Using a representation model, the representation vectors corresponding to the current code segment and each other code segment are determined. Based on the representation vectors, the similarity between the current code segment and each other code segment is calculated. Based on the similarity, the similar code segment is selected from each other code segment. The method according to claim 7, wherein, The step of obtaining a second code segment that follows the similar code segment includes: The start and end line numbers of the similar code segments are respectively superimposed with a predetermined offset to obtain the updated start line number and the updated end line number; From the code segments belonging to the same code file as the similar code segments, select the code segment containing the update start line number and update end line number as the second code segment. The method according to claim 7, wherein, Determining the code context based on the first code fragment and the second code fragment includes: The first code snippet and the second code snippet are selected as candidate code snippets; Calculate the relevance score between the current code snippet and each candidate code snippet. This relevance score indicates the usefulness of the corresponding candidate code snippet. Based on the relevance score, the code context is determined from each candidate code segment. The method according to claim 10, wherein, The calculation of the relevance score between the current code segment and each candidate code segment includes: For any first candidate code segment, the current code segment and the first candidate code segment are concatenated and input into a pre-trained classification model. The relevance score between the current code segment and the first candidate code segment is obtained through the output of the classification model. The method according to claim 11, wherein, The classification model is trained through the following steps: Obtain the sample code snippet and its corresponding sample context; The target content in the sample code fragment is masked to obtain the target code fragment; The target code fragment and the sample context are input into the code model to obtain the predicted content. If the predicted content matches the target content, the sample context and the sample code segment are determined as positive samples; if the predicted content does not match 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 and negative samples. The method according to claim 10, wherein, Determining the code context from each candidate code fragment includes: The candidate code segments are sorted from largest to smallest according to their relevance scores; The top 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. A code completion device, comprising: The acquisition unit is used to acquire the current code segment in the current code file that needs to be completed; A query unit is used to query a target node in a code relationship graph that matches the current code fragment; the code relationship graph is constructed based on multiple code files in a code library, including other code files that are different from the current code file. In the code relationship graph, a single node represents a code entity corresponding to a code fragment, and the connecting edges between nodes represent the semantic associations between code entities. The determining unit is used to determine the code context of the current code segment based on the first code segment corresponding to the neighboring nodes of the target node; The input unit is used to input the current code fragment and the code context into the code big model, so that it generates the completion result of the current code fragment based on the code context. The apparatus according to claim 14, wherein, The query unit is specifically used for: Several candidate nodes that meet the position matching conditions are determined from the code relationship graph; the position matching conditions include that the candidate node belongs to the current code file and the line number range of the corresponding code segment contains the target line number of the position to be filled. The candidate node with the smallest corresponding row number interval is selected from the candidate nodes as the target node. The apparatus according to claim 14, wherein, The neighboring node belongs to another code file, and the starting line number of the connection edge between the target node and the neighboring node is less than the target line number of the position to be filled. The apparatus according to claim 14 further includes: The selection unit is used to select similar code segments from the various code segments obtained from other code files in the code library based on the current code segment. The acquisition unit is further configured to acquire a second code segment that follows the similar code segment; The determining unit is specifically used for: The code context is determined based on the first code snippet and the second code snippet. The apparatus according to claim 17, wherein, The selection unit is specifically used for: Based on the current code fragment, an information retrieval algorithm is used to select the similar code fragments from the various code fragments; or, By calculating the edit distance between the current code snippet and each of the other code snippets, the similar code snippets are selected from the other code snippets; or, Using a representation model, the representation vectors corresponding to the current code segment and each other code segment are determined. Based on the representation vectors, the similarity between the current code segment and each other code segment is calculated. Based on the similarity, the similar code segment is selected from each other code segment. The apparatus according to claim 17, wherein, The acquisition unit is specifically used for: The start and end line numbers of the similar code segments are respectively superimposed with a predetermined offset to obtain the updated start line number and the updated end line number; From the code segments belonging to the same code file as the similar code segments, select the code segment containing the update start line number and update end line number as the second code segment. The apparatus according to claim 17, wherein, The determining unit includes: A submodule is defined to select the first code segment and the second code segment as candidate code segments. The calculation submodule is used to calculate the relevance score between the current code snippet and each candidate code snippet. This relevance score indicates the usefulness of the corresponding candidate code snippet. The determining submodule is further configured to determine the code context from each candidate code segment based on the relevance score. The apparatus according to claim 20, wherein, The calculation submodule is specifically used for: For any first candidate code segment, the current code segment and the first candidate code segment are concatenated and input into a pre-trained classification model. The relevance score between the current code segment and the first candidate code segment is obtained through the output of the classification model. The apparatus according to claim 21 further includes: Masking unit and training unit; The acquisition unit is also used to acquire sample code fragments and corresponding sample contexts; The masking unit is used to mask the target content in the sample code fragment to obtain the target code fragment; The input unit is also used to input the target code fragment and the sample context into the code big model to obtain the predicted content; The determining 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. The apparatus according to claim 20, wherein, The determination submodule is specifically used for: The candidate code segments are sorted from largest to smallest according to their relevance scores; The top 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. A computer-readable storage medium having a computer program stored thereon, wherein, When the computer program is executed in a computer, it causes the computer to perform the method according to any one of claims 1-13. A computing device includes a memory and a processor, wherein, The memory stores executable code, and when the processor executes the executable code, it implements the method of any one of claims 1-13.
Citation Information
Patent Citations
Code completion method, device and equipment
CN115291854A
Model training method, related method, device, equipment and storage medium
CN118012781A
Code completion method and device
CN119201121A
Automating Identification of Code Snippets for Library Suggestion Models
US20190079754A1
Code completion
US20200097261A1