Code completion method, related device, equipment and storage medium
By obtaining and utilizing project structure information, determining code semantics and structure information, and generating code to be completed, the problem of low code completion efficiency and accuracy in software development is solved, and more efficient and accurate code completion is achieved.
Patent Information
- Application Number
- CN202411072795.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-06
- Publication Date
- 2024-12-31
AI Technical Summary
In software development, conventional code completion methods are difficult to make full use of the dispersed information and dependencies in projects, resulting in low efficiency and accuracy.
By obtaining the project structure information of the project to be completed, determining the code semantic information and code structure information at the location to be completed, using this information to generate the code to be completed, and combining the information of the same file and different files to complete the code.
It improves the efficiency and accuracy of code completion, displays the code units and their relationships in a structured manner, quickly locates the locations to be completed, enriches the data sources, and improves the accuracy and efficiency of code completion.
Smart Images

Figure CN119225705A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of computer software development, and in particular to a code completion method and related devices, equipment and storage media. Background Art
[0002] In actual software development scenarios, code files in a project are usually interrelated, including shared tools, configurations, and cross-API calls caused by modularization. These dependencies constitute an important source of information for code completion tasks.
[0003] However, in conventional code completion tasks, code completion is usually performed based on contextual information in the same file, and it is difficult to fully utilize useful information and dependencies scattered in different files in the project, resulting in low efficiency and accuracy of code completion.
[0004] Therefore, how to improve the efficiency and accuracy of code completion has become an urgent problem to be solved. Summary of the invention
[0005] The main technical problem solved by the present application is to provide a code completion method and related devices, equipment and storage media, which can improve the efficiency and accuracy of code completion.
[0006] In order to solve the above technical problems, the first aspect of the present application provides a code completion method, which includes: obtaining project structure information of a project to be completed, wherein the project structure information includes code units of each file in the project to be completed and the relationship between each code unit, and the project to be completed is provided with a to-be-completed position of the code to be completed; based on the project structure information, determining code semantic information and code structure information of the position to be completed, wherein the code semantic information includes code of at least one code unit that semantically matches the position to be completed, and the code structure information includes target information of at least one code unit that has a target relationship with the position to be completed; using the code semantic information and code structure information of the position to be completed, generating the to-be-completed code of the position to be completed.
[0007] In order to solve the above technical problems, the second aspect of the present application provides a code completion device, which includes an acquisition module, a determination module and a generation module; the acquisition module is used to obtain project structure information of the project to be completed, wherein the project structure information includes code units of each file in the project to be completed and the relationship between each code unit, and the project to be completed is provided with a to-be-completed position of the code to be completed; the determination module is used to determine the code semantic information and code structure information of the position to be completed based on the project structure information, wherein the code semantic information includes the code of at least one code unit that semantically matches the position to be completed, and the code structure information includes target information of at least one code unit that has a target relationship with the position to be completed; the generation module is used to generate the to-be-completed code of the position to be completed by using the code semantic information and code structure information of the position to be completed.
[0008] In order to solve the above technical problem, the third aspect of the present application provides an electronic device, which includes a memory and a processor coupled to each other, and the processor is used to execute program instructions stored in the memory to implement the code completion method in the above first aspect.
[0009] In order to solve the above technical problem, the fourth aspect of the present application provides a computer-readable storage medium on which program instructions are stored. When the program instructions are executed by a processor, the code completion method in the above first aspect is implemented.
[0010] The above scheme obtains the project structure information of the project to be completed, and determines the code semantic information and code structure information of the position to be completed based on the project structure information, and then uses the code semantic information and code structure information of the position to be completed to generate the code to be completed at the position to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, the project to be completed is provided with the position to be completed of the code to be completed, the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed. On the one hand, by obtaining the project structure information, it is possible to combine the information in the same file and between different files with the position to be completed, so as to complete the code through a rich data source, which helps to improve the accuracy of code completion; on the other hand, the project structure information displays the code units of each file and the relationship between each code unit in a structured manner, which can quickly locate the code unit where the position to be completed is located, and then obtain the code semantic information and code structure information of the position to be completed, which helps to improve the efficiency of code completion. Therefore, the efficiency and accuracy of code completion can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] Figure 1 This is a flow chart of an embodiment of a method for completing code in this application;
[0012] Figure 2 Project structure for this application Figure 1 A schematic structural diagram of an embodiment;
[0013] Figure 3 This is a schematic diagram of a framework of an embodiment of a code completion device of the present application;
[0014] Figure 4 A schematic diagram of a framework of an embodiment of an electronic device of the present application;
[0015] Figure 5 A schematic diagram of a framework of an embodiment of a computer-readable storage medium of the present application. DETAILED DESCRIPTION
[0016] The scheme of the embodiment of the present application is described in detail below in conjunction with the drawings of the specification.
[0017] In the following description, for the purpose of explanation rather than limitation, specific details such as specific system structures, interfaces, and technologies are provided to facilitate a thorough understanding of the present application.
[0018] The term "multiple" herein means two or more than two. In addition, the term "at least one" herein means any combination of at least two of any one or more of a plurality of, for example, including at least one of A, B, and C, and may mean including any one or more elements selected from the set consisting of A, B, and C.
[0019] See also Figure 1 , Figure 1 This is a flowchart of an embodiment of the code completion method of this application, specifically, it includes the following steps:
[0020] S11: Acquire project structure information of the project to be completed, wherein the project structure information includes code units of each file in the project to be completed and the relationship between each code unit. The project to be completed is provided with a to-be-completed position of the code to be completed.
[0021] In the embodiment of the present disclosure, the project to be completed includes multiple files, each file includes multiple code units, and in the project to be completed, the position to be completed of the code to be completed can be identified by a placeholder. Exemplarily, the placeholder can be "-" or "()".
[0022] In an implementation scenario, the code units of each file in the project to be completed include at least one first code unit that can realize a specific function, and a second code unit, the second code unit includes the remaining code in the file except the first code unit, the first code unit includes at least one of a function and a class, and the second code unit includes an import statement. Among them, the import statement is used to characterize the introduction relationship between the current code unit and other code units, and the existence of the import statement in the second code unit indicates that the current file introduces code units in other files. Exemplarily, the import statement can be in the format of "from+file name+import+first code unit name". In the above method, on the basis that the code units of each file include at least one first code unit that can realize a specific function, and a second code unit, the project structure information is obtained, the granularity of the project structure information can be refined, and the accuracy of code completion can be improved.
[0023] In a specific implementation scenario, the first code unit may include the name, parameters, and code definition of a function or class.
[0024] In a specific implementation scenario, when the first code unit includes a class and a function, the position to be completed can be located outside the class and the function, or inside the function, or inside the class. The position to be completed is not specifically limited here.
[0025] In a specific implementation scenario, the second code unit also includes non-functional code, such as log information, statistical information, etc.
[0026] In an implementation scenario, the relationship between code units includes at least one of the following: import relationship, call relationship, inclusion relationship, encapsulation relationship, and class hierarchy relationship. The relationship between the above code units provides rich reference information for the code completion process, which helps to further improve the accuracy of code completion.
[0027] In a specific implementation scenario, the code units of each file include the aforementioned first code unit and the second code unit, the import relationship exists between the second code unit and the import module (such as a specific file) identified by the import statement in the second code unit, the calling relationship, the inclusion relationship, the encapsulation relationship and the class hierarchy relationship exist between each first code unit, and the encapsulation relationship exists between the first code unit and the second code unit in the same file. And in the case where the first code unit includes a class and a function, the calling relationship exists between the functions, the inclusion relationship exists between the function and the class, the encapsulation relationship exists between the function and the second code unit in the same file, and between the class and the second code unit in the same file, and the class hierarchy relationship exists between the classes.
[0028] In an implementation scenario, in order to obtain the project structure information of the project to be completed, a project structure diagram representing the relationship between the code units of each file in the project to be completed can be established, that is, the project structure information is embodied in the form of a project structure diagram, wherein the project structure diagram includes multiple nodes, each node represents a code unit, and the lines between the nodes represent the relationship between the code units corresponding to the nodes. Of course, the node is only one form used to represent the project structure diagram, and the code unit can also be represented by a module, a table or a specific graph, and the lines between the nodes can be in the form of arrows or line segments, etc., and the specific structure of the project structure diagram is not limited here. The above method, through the project structure diagram composed of nodes and the lines between the nodes, to represent the project structure information, can show the relationship between the code units of each file in the project to be completed as clearly and intuitively as possible, thereby improving the efficiency of code completion for the project to be completed.
[0029] In a specific implementation scenario, the process of establishing a project structure diagram may include: parsing each file in the project to be completed separately to obtain the abstract syntax tree corresponding to each file, and based on the relationship between the code units of each file, connecting the nodes of the abstract syntax tree corresponding to each file, thereby obtaining a project structure diagram, wherein the abstract syntax tree includes nodes corresponding to each code unit in the file, and the connection between the nodes corresponding to the file indicates that there is a relationship between the code units corresponding to the nodes, and the abstract syntax tree can be obtained by parsing each file using the tree-sitter tool. The specific process of parsing the file by the tree-sitter tool can refer to the relevant technology, which will not be repeated here. As a possible example, in the python language, the PyCG (coarse-grained) tool can also be used to assist in generating a project structure diagram. The above method, through the abstract syntax tree corresponding to each file, extracts the relevant nodes constituting the project structure diagram, and connects the nodes of the abstract syntax tree corresponding to each file, thereby obtaining a project structure diagram, that is, parsing each file in the project to be completed into an abstract syntax tree, compared with directly constructing a project structure diagram based on each file, it can improve the construction efficiency of the project structure diagram.
[0030] Exemplarily, file 1 and file 2 belong to the project to be completed, the code units of file 1 and file 2 respectively include at least one first code unit capable of realizing a specific function, and a second code unit, the second code unit includes the remaining code in the file except the first code unit, the first code unit includes a function and a class, the second code unit includes an import statement, and the relationship between the code units includes at least one of the following: an import relationship, a call relationship, an inclusion relationship, an encapsulation relationship, and a class hierarchy relationship, wherein:
[0031] File 1 (file_A) content:
[0032] class Class_A:
[0033] def __init__(self):
[0034] self.variable_A = "value"
[0035] def function_A(self):
[0036] instance_A=ClassA()
[0037] print(instance_A.variable_A)
[0038] File 2 (file_B) content:
[0039] from file_A import Class_A
[0040] class Class_B:
[0041] def function_B(self):
[0042] X=Class_A()
[0043] return x
[0044] def function_C():
[0045] x=Class_A()
[0046] X.function_A()
[0047] See also Figure 2 , Figure 2 Project structure for this application Figure 1 Schematic diagram of the embodiment, respectively parsing the above-mentioned file 1 and file 2 to obtain the abstract syntax tree 1 corresponding to file 1 ( Figure 2 The left dashed box in the figure), and the abstract syntax tree 2 corresponding to file 2 ( Figure 2), wherein the abstract syntax tree 1 includes nodes 1, 2, 3, and 4, wherein node 1 represents the code unit of class A in file 1 (line 1-6 of the content of file 1), node 2 represents the code unit of the constructor of class A in file 1 (line 2-3 of the content of file 1), node 3 represents the code unit of function A in file 1 (line 4-6 of the content of file 1), node 4 represents the non-functional code in file 1 (the content of file 1 is not shown), and the abstract syntax tree 2 includes nodes 5, 6, 7, and 8, and node 9 represents the non-functional code in file 1. Point 5 represents the code unit of class B in file 2 (lines 2-5 of the content of file 2), node 6 represents the code unit of function B contained in class B in file 2 (lines 3-5 of the content of file 2), node 7 represents the code unit of function C in file 2 (lines 6-8 of the content of file 2), node 8 represents the first line of import statement in file 2 (line 1 of the content of file 2) and other non-functional codes (the content of file 2 is not shown), and then based on the relationship between the code units of file 1 and file 2, the nodes of the abstract syntax tree corresponding to each file are connected to obtain the following: Figure 2 As shown in the overall project structure diagram of file 1 and file 2, node 1 has an inclusion relationship with node 2 and node 3 respectively, node 4 has an encapsulation relationship with node 1, node 2, and node 3 in file 1, node 1 has an import relationship with node 8 (file 2 imports class A from file 1), node 1 has a class hierarchy relationship with node 5, node 1 has a calling relationship with node 7, node 5 has an inclusion relationship with node 6, and node 8 has an encapsulation relationship with node 5, node 6, and node 7 in file 2.
[0048] It should be noted that File 1 and File 2 are only examples of some files in the project to be completed. The project to be completed can also include more files, such as File 3, File 4, etc. Of course, it can also include only one file, and examples are not given one by one here. The specific contents of the code units in File 1 and File 2 are only used as examples and do not constitute a specific limitation on the project to be completed.
[0049] In another implementation scenario, different from the above-mentioned way of presenting the project structure information in the form of a project structure diagram, the project structure information in a vector form can also be obtained by encoding the code units of each file and the relationship between the code units.
[0050] S12: Based on the project structure information, determine the code semantic information and code structure information of the position to be completed, wherein the code semantic information includes the code of at least one code unit that semantically matches the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed.
[0051] In one implementation scenario, in order to obtain the code semantic information of the position to be completed, the representative code of the position to be completed can be obtained to determine the text similarity between the representative code and each target code unit in the project to be completed, and the code of at least one target code unit whose text similarity meets the first similarity requirement is obtained as the code semantic information, wherein the target code unit includes all the first code units in the project to be completed that can realize the specific function. Thus, the code semantic information of the position to be completed can be determined through the project structure information. In the above method, the target code unit includes all the first code units in the project to be completed that can realize the specific function, that is, the target code unit can exist in each file in the project to be completed, which can satisfy the cross-file information as the source of code semantic information. Compared with code completion only achieved by matching between similar codes, the robustness of code completion can be improved.
[0052] In a specific implementation scenario, in order to determine the text similarity between the representative code and each target code unit in the to-be-completed item, the algorithms used may include but are not limited to: jaccard similarity algorithm, cosine similarity algorithm, Levenshtein edit distance algorithm, etc.
[0053] In a specific implementation scenario, at least one target code unit whose text similarity meets the first similarity requirement may be a target code unit corresponding to the first number of text similarities in the order of text similarity from high to low, wherein the first similarity requirement may be 60%, 70%, 80%, 90%, etc., and the first number may be 1, 2, 3, 4, 5, etc. In the above manner, by selecting the target code units corresponding to the first number of text similarities in the order of text similarity from high to low as the code semantic information, different numbers of target code units can be selected according to different scenarios to improve the flexibility of code completion.
[0054] In a specific implementation scenario, the method of obtaining the representative code may be different depending on the location of the position to be completed. As a possible implementation method, when the position to be completed is located inside the first code unit, the code of the first code unit where the position to be completed is located is used as the representative code, and the target code unit is all the first code units in the project to be completed. As another possible implementation method, when the position to be completed is located outside the first code unit, the adjacent code of the position to be completed is used as the representative code, and the adjacent code includes at least one of the following: the code of the first number of lines before the position to be completed, the code of the second number of lines after the position to be completed, and the target code unit is all the code units in the project to be completed. In addition, the number of lines of the first number of lines and the second number of lines can be the same or different, and the number of lines of the first number of lines and the second number of lines can be 10 lines, 15 lines, 20 lines, etc., which are not specifically limited here. Of course, when the position to be completed is located at the first line of code in the project to be completed, only the second number of lines of code after the position to be completed can be obtained, or, when the position to be completed is located at the last line of code in the project to be completed, only the first number of lines of code before the position to be completed can be obtained. The above method adopts different strategies to obtain representative code according to the different positions of the position to be completed, and selects the code unit with the same granularity as the representative code as the target code unit, which can improve the fine granularity of code completion and can minimize the introduction of irrelevant information, thereby improving the accuracy of code completion.
[0055] In an implementation scenario, in order to obtain the code structure information of the position to be completed, the code unit at the position to be completed can be used as a reference code unit, and a code unit that has a target relationship with the reference code unit is selected as an associated code unit, and then the target information of each associated code unit can be obtained as the code structure information of the position to be completed, wherein the associated code unit includes at least one of the following: a code unit of the same type as the reference code unit in the first target file, a code unit of the same type as the reference code unit whose name similarity with the reference code unit meets the second similarity requirement, and a code unit of the same type as the reference code unit in the second file whose name similarity with the first file where the reference code unit is located meets the third similarity requirement, and the first target file includes at least one of the following: the first file where the position to be completed is located, and the third file that has an import relationship with the first file. In the above method, the associated code unit that has a target relationship with the reference code unit in multiple dimensions is obtained by using the project structure information, and the target information of each associated code unit is used as the code structure information of the position to be completed, which can make full use of the relevant information scattered in different files as much as possible to enrich the data source of the code structure information, which is helpful to improve the accuracy of code completion.
[0056] In a specific implementation scenario, the name similarity of the code unit and the name similarity of the file can both be split by underscore or camel case format, and then the split parts are matched to determine whether the corresponding similarity requirements are met. As a possible implementation, for the second similarity requirement corresponding to the name similarity of the code unit, a threshold can be set, that is, the number of similar name parts after splitting reaches above the threshold, so that it can be determined that the name similarity of the code unit and the reference code unit meets the second similarity requirement. The specific setting method of the third similarity requirement corresponding to the name similarity of the file is similar to the second similarity requirement, which will not be repeated here.
[0057] In a specific implementation scenario, the reference code unit is a first code unit that can realize a specific function, and the target information of each associated code unit may include obtaining declaration information of each associated code unit, wherein the associated code unit includes at least one of the following: a first code unit of the same type as the reference code unit in the first target file, and a first code unit of the same type as the reference code unit whose name similarity with the reference code unit meets the second similarity requirement, and then obtaining the target information of each associated code unit may specifically include obtaining declaration information of each associated code unit. In the above manner, when the reference code unit is a first code unit that can realize a specific function, the associated code unit is selected based on the first code unit in a targeted manner, and the declaration information of each associated code unit is obtained, which can reduce the data space occupied to a certain extent and improve the efficiency of code completion.
[0058] In a specific implementation scenario, the reference code unit is the above-mentioned first code unit that can realize a specific function, and the first code unit includes two types: function and class. When the type of the reference code unit is a function, the position to be completed is located inside the function, and the first code unit of the same type as the reference code unit in the first target file is a function, and then the associated code unit includes the following first functions: the function in the first target file, and the function whose name similarity with the reference code unit meets the second similarity requirement, and the declaration information of each associated code unit includes the declaration of each first function. As a possible implementation method, the declaration of each first function may include each function name and each function parameter. The above method selects the target information of the associated code units of different dimensions as the code structure information according to the different types of the first code unit where the position to be completed is located, which can improve the granularity of code completion and thus improve the accuracy of code completion.
[0059] In a specific implementation scenario, the reference code unit is the above-mentioned first code unit that can realize a specific function, and the first code unit includes two types: function and class. When the type of the reference code unit is a class, that is, the position to be completed is located inside the class, and the class includes a class hierarchy relationship of a parent class and a child class. The first code unit of the same type as the reference code unit in the first target file is a class. In addition to all classes in the first target file and classes whose name similarity to the reference code unit meets the second similarity requirement, the associated code unit also includes all classes in the second target file, the parent class of the reference code unit, the child class of the reference code unit, and functions in the first target file and the second target file. The second target file includes at least one of the following: a fourth file where the parent class of the reference code unit is located, and a fifth file that has an import relationship with the fourth file. On this basis, all classes in the first target file and all classes in the second target file are taken as the first category, the parent class of the reference code unit, the subclass of the reference code unit, and the class whose name similarity with the reference code unit meets the second similarity requirement are taken as the second category, and the functions in the first target file and the second target file are taken as the second function, and then the associated code units include the first category, the second category, and the second function, and the declaration information of each associated code unit includes the declaration of variables and functions within the class in each first category, the declaration of the second category, and the declaration of the second function. The above method selects the target information of associated code units of different dimensions as code structure information according to the different types of the first code unit where the position to be completed is located, which can improve the granularity of code completion and thus improve the accuracy of code completion.
[0060] In another specific implementation scenario, based on the fact that the type of the above-mentioned reference code unit is a class, unlike the class hierarchy relationship in which the above-mentioned class includes a parent class and a child class, the class does not include a parent class and a child class. In this case, the associated code unit may include all classes and functions in the first target file, and a class whose name similarity with the reference code unit meets the second similarity requirement, and the function in the first target file is used as the second function. Accordingly, the declaration information of each associated code unit includes the declaration of variables and functions within the class of all classes in a target file, the declaration of a class whose name similarity with the reference code unit meets the second similarity requirement, and the declaration of the second function.
[0061] In a specific implementation scenario, the reference code unit is a second code unit, the code units in the project to be completed include a first code unit that can realize a specific function, and a second code unit, each file corresponds to a second code unit, the second code unit in each file includes the code in the file except the first code unit, and the associated code unit includes at least one of the following: a second code unit of the same type as the reference code unit in the first target file, a second code unit of the second file whose name similarity with the first file where the reference code unit is located meets the third similarity requirement, and a second code unit of other files in the same directory as the first file, and the target information of each associated code unit includes the code of each associated code unit. In the above method, when the reference code unit is the second code unit, the associated code unit is selected based on the second code unit in a targeted manner, and the granularity of the code structure information selected is different from that when the reference code unit is the first code unit, which can improve the flexibility of code completion.
[0062] S13: Generate the code to be completed for the position to be completed by using the code semantic information and code structure information of the position to be completed.
[0063] In an implementation scenario, the code semantic information and code structure information of the position to be completed obtained in the above implementation method are used to generate the code to be completed for the position to be completed, which can provide relevant candidate information for the position to be completed from the semantic level and the structural level, so as to make full use of the code in the project as much as possible and improve the accuracy of code completion.
[0064] In an implementation scenario, a code generation model can be used to generate the code to be completed for the position to be completed. The code generation model can be a large language model or other customized neural network structure. The large language model can include but is not limited to open source large models such as LLAMA, Bloom, etc. The neural network structure can include but is not limited to: CNN, RNN, LSTM, Transformer, etc. The specific method of using the code generation model to generate the code to be completed for the position to be completed can include: based on the code semantic information and code structure information of the position to be completed, construct a prompt text, and input the prompt text into the code generation model to obtain the code to be completed, wherein the prompt text is used to indicate that the code generation model completes the position to be completed in the project to be completed based on the code semantic information and code structure information. In the above method, the code generation model is used to process the code semantic information and code structure information. It only needs to input the prompt text constructed based on the code semantic information and code structure information into the code generation model to obtain the completed code, which can improve the efficiency of code completion, and the code semantic information and code structure information obtained through the project structure information are helpful for the code generation model to understand the project structure and improve the accuracy of code completion using the code generation model.
[0065] Exemplarily, the format of the prompt text is "You are a code expert. Please complete the code at the [placeholder] in the [item to be completed] according to the [code semantic information] and [code structure information] given below. [Code semantic information]: {} [Code structure information]: {} [Item to be completed]: {}", wherein the "{}" in "[Code semantic information]: {}" is the code of at least one code unit obtained in the above implementation manner, and the "{}" in "[Code structure information]: {}" is the target information of at least one code unit obtained in the above implementation manner.
[0066] The above scheme obtains the project structure information of the project to be completed, and determines the code semantic information and code structure information of the position to be completed based on the project structure information, and then uses the code semantic information and code structure information of the position to be completed to generate the code to be completed at the position to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, the project to be completed is provided with the position to be completed of the code to be completed, the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed. On the one hand, by obtaining the project structure information, it is possible to combine the information in the same file and between different files with the position to be completed, so as to complete the code through a rich data source, which helps to improve the accuracy of code completion; on the other hand, the project structure information displays the code units of each file and the relationship between each code unit in a structured manner, which can quickly locate the code unit where the position to be completed is located, and then obtain the code semantic information and code structure information of the position to be completed, which helps to improve the efficiency of code completion. Therefore, the efficiency and accuracy of code completion can be improved.
[0067] See also Figure 3 , Figure 3 This is a schematic diagram of a framework of an embodiment of a code completion device of the present application, wherein the code completion device 30 includes an acquisition module 31, a determination module 32, and a generation module 33. The acquisition module 31 is used to acquire the project structure information of the project to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, and the project to be completed is provided with a position to be completed of the code to be completed; the determination module 32 is used to determine the code semantic information and code structure information of the position to be completed based on the project structure information, wherein the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed; the generation module 33 is used to generate the code to be completed of the position to be completed using the code semantic information and code structure information of the position to be completed.
[0068] In the above scheme, the code completion device 30 obtains the project structure information of the project to be completed, and determines the code semantic information and code structure information of the position to be completed based on the project structure information, and then uses the code semantic information and code structure information of the position to be completed to generate the code to be completed at the position to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, the project to be completed is provided with the position to be completed of the code to be completed, the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed. On the one hand, by obtaining the project structure information, it is possible to combine the information in the same file and between different files with the position to be completed, so as to complete the code through a rich data source, which is helpful to improve the accuracy of code completion; on the other hand, the project structure information displays the code units of each file and the relationship between each code unit in a structured manner, which can quickly locate the code unit at the position to be completed, and then obtain the code semantic information and code structure information of the position to be completed, which is helpful to improve the efficiency of code completion. Therefore, the efficiency and accuracy of code completion can be improved.
[0069] In some disclosed embodiments, the determination module 32 includes a first determination submodule, which is used to determine the code semantic information of the position to be completed based on the project structure information, and is specifically used to obtain the representative code of the position to be completed, determine the text similarity between the representative code and each target code unit in the project to be completed, wherein the target code unit includes all the first code units in the project to be completed that can realize the specific function, and obtain the code of at least one target code unit whose text similarity meets the first similarity requirement as the code semantic information.
[0070] Therefore, the target code unit includes all the first code units in the project to be completed that can realize specific functions, that is, the target code unit can exist in each file in the project to be completed, and can satisfy the cross-file information as the source of code semantic information. Compared with realizing code completion only through matching between similar codes, the robustness of code completion can be improved.
[0071] In some disclosed embodiments, at least one target code unit whose text similarity meets the first similarity requirement is: the target code units corresponding to the first first number of text similarities in the text similarity sorting from high to bottom; the first determination submodule also includes an acquisition unit, the acquisition unit is used to acquire a representative code of the position to be completed, specifically for responding to the position to be completed being located inside the first code unit, taking the code of the first code unit where the position to be completed is located as the representative code; and the target code unit is all the first code units in the project to be completed; responding to the position to be completed being located outside the first code unit, taking the adjacent code of the position to be completed as the representative code, the adjacent code including at least one of the following: a first number of lines of code before the position to be completed, and a second number of lines of code after the position to be completed; and the target code unit is all the code units in the project to be completed.
[0072] Therefore, on the one hand, by selecting the target code units corresponding to the first number of text similarities in the order of text similarity from high to bottom as code semantic information, different numbers of target code units can be selected according to different scenarios to improve the flexibility of code completion; on the other hand, according to the different locations of the positions to be completed, different strategies are adopted to obtain representative codes, and code units with the same granularity as the representative codes are selected as target code units, which can improve the granularity of code completion and minimize the introduction of irrelevant information, thereby improving the accuracy of code completion.
[0073] In some disclosed embodiments, the determination module 32 also includes a second determination submodule, which is used to determine the code structure information of the position to be completed based on the project structure information, and is specifically used to take the code unit at the position to be completed as a reference code unit, and select a code unit that has a target relationship with the reference code unit as an associated code unit, the associated code unit including at least one of the following: a code unit of the same type as the reference code unit in the first target file, a code unit of the same type as the reference code unit whose name similarity with the reference code unit meets a second similarity requirement and is of the same type as the reference code unit, and a code unit of the same type as the reference code unit in the second file whose name similarity with the first file where the reference code unit is located meets a third similarity requirement, wherein the first target file includes at least one of the following: a first file where the position to be completed is located, and a third file that has an import relationship with the first file; the target information of each associated code unit is obtained as the code structure information of the position to be completed.
[0074] Therefore, by using the project structure information to obtain associated code units that have a target relationship with the reference code units in multiple dimensions, and using the target information of each associated code unit as the code structure information of the position to be completed, the relevant information scattered in different files can be utilized as fully as possible to enrich the data source of the code structure information, which helps to improve the accuracy of code completion.
[0075] In some disclosed embodiments, the reference code unit is a first code unit that can implement a specific function, and the associated code units include at least one of the following: a first code unit of the same type as the reference code unit in the first target file, a first code unit whose name similarity with the reference code unit meets a second similarity requirement and is of the same type as the reference code unit, and the second determination submodule is also specifically used to obtain declaration information of each associated code unit.
[0076] Therefore, when the reference code unit is the first code unit that can realize a specific function, the associated code unit is selected based on the first code unit in a targeted manner, and the declaration information of each associated code unit is obtained, which can reduce the data space occupied to a certain extent and improve the code completion efficiency.
[0077] In some disclosed embodiments, the first code unit includes two types: function and class; when the type of the reference code unit is a function, the associated code unit includes the following first functions: a function in the first target file, and a function whose name similarity with the reference code unit meets a second similarity requirement, and the declaration information of each associated code unit includes the declaration of each first function; and / or, when the type of the reference code unit is a class, the associated code unit includes a first class, a second class, and a second function, the first class includes all classes in the first target file and all classes in the second target file, and the second target file includes at least one of the following: a fourth file where the parent class of the reference code unit is located, and a fifth file that has an import relationship with the fourth file; the second class includes the parent class of the reference code unit, the child class of the reference code unit, and a class whose name similarity with the reference code unit meets the second similarity requirement; the second function includes the function in the first target file and the second target file; the declaration information of each associated code unit includes the declaration of variables and functions within the class in each first class, the declaration of the second class, and the declaration of the second function.
[0078] Therefore, according to the different types of the first code unit at the position to be completed, target information of associated code units of different dimensions is selected as code structure information, which can improve the granularity of code completion and further improve the accuracy of code completion.
[0079] In some disclosed embodiments, the reference code unit is a second code unit; the code units in the project to be completed include a first code unit that can implement a specific function, and a second code unit, each file corresponds to a second code unit, and the second code unit in each file includes the code in the file except the first code unit; the associated code units include at least one of the following: a second code unit of the same type as the reference code unit in the first target file, a second code unit of a second file whose name similarity with the first file where the reference code unit is located meets a third similarity requirement, and a second code unit of other files in the same directory as the first file; the target information of each associated code unit includes the code of each associated code unit.
[0080] Therefore, when the reference code unit is the second code unit, the associated code unit is selected based on the second code unit in a targeted manner, and the granularity of the code structure information selected is different from that when the reference code unit is the first code unit, which can improve the flexibility of code completion.
[0081] In some disclosed embodiments, the acquisition module 31 is specifically used to establish a project structure diagram that represents the relationship between code units of each file in the project to be completed, wherein the project structure diagram includes multiple nodes, each node represents a code unit, and the lines between the nodes indicate the existence of a relationship between the code units corresponding to the nodes.
[0082] Therefore, by representing the project structure information through a project structure diagram consisting of nodes and the connections between the nodes, the relationship between the code units of each file in the project to be completed can be displayed as clearly and intuitively as possible, thereby improving the efficiency of code completion for the project to be completed.
[0083] In some disclosed embodiments, the acquisition module 31 includes establishing a sub-module, which is used to establish a project structure diagram that represents the relationship between code units of each file in the project to be completed, and is specifically used to parse each file in the project to be completed separately to obtain an abstract syntax tree corresponding to each file, wherein the abstract syntax tree includes nodes corresponding to each code unit in the file, and the lines between the nodes corresponding to the file indicate that there is a relationship between the code units corresponding to the nodes; based on the relationship between the code units between the files, the nodes of the abstract syntax trees corresponding to the files are connected to obtain a project structure diagram.
[0084] Therefore, through the abstract syntax tree corresponding to each file, the relevant nodes constituting the project structure diagram are extracted, and the nodes of the abstract syntax tree corresponding to each file are connected to obtain the project structure diagram, that is, the method of parsing each file in the project to be completed into an abstract syntax tree can improve the construction efficiency of the project structure diagram compared to directly constructing the project structure diagram based on each file.
[0085] In some disclosed embodiments, the generation module 33 is specifically used to construct a prompt text based on the code semantic information and code structure information of the position to be completed; the prompt text is input into the code generation model to obtain the code to be completed; wherein the prompt text is used to instruct the code generation model to complete the position to be completed in the project to be completed based on the code semantic information and code structure information.
[0086] Therefore, by using the code generation model to process the code semantic information and code structure information, you only need to input the prompt text built based on the code semantic information and code structure information into the code generation model to get the completed code, which can improve the efficiency of code completion. The code semantic information and code structure information obtained through the project structure information can help the code generation model understand the project structure and improve the accuracy of code completion using the code generation model.
[0087] In some disclosed embodiments, the code units of each file include at least one first code unit that can implement a specific function, and a second code unit, the second code unit includes the remaining code in the file except the first code unit, the first code unit includes at least one of a function and a class, and the second code unit includes an import statement; and / or the relationship between the code units includes at least one of the following: an import relationship, a calling relationship, an inclusion relationship, an encapsulation relationship, and a class hierarchy relationship.
[0088] Therefore, on the basis that the code units of each file include at least one first code unit and a second code unit that can implement a specific function, obtaining the project structure information can further refine the granularity of the project structure information and improve the accuracy of code completion.
[0089] See also Figure 4 , Figure 4 4 is a schematic diagram of a framework of an embodiment of an electronic device 40 of the present application. The electronic device 40 includes a memory 41 and a processor 42 coupled to each other, and the processor 42 is used to execute program instructions stored in the memory 41 to implement the steps in any of the above code completion method embodiments. Specifically, the electronic device 40 may include but is not limited to: a microcomputer, a server, a translator, an office book, an in-vehicle interactive system, a dictionary pen, a virtual anchor, etc., which are not limited here.
[0090] Specifically, the processor 42 is used to control itself and the memory 41 to implement the steps of any of the above code completion method embodiments. The processor 42 can also be called a CPU (Central Processing Unit). The processor 42 may be an integrated circuit chip with signal processing capabilities. The processor 42 can also be a general-purpose processor, a digital signal processor (Digital Signal Processor, DSP), an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), a field-programmable gate array (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. In addition, the processor 42 can be implemented by an integrated circuit chip.
[0091] In the above scheme, the electronic device 40 obtains the project structure information of the project to be completed, and determines the code semantic information and code structure information of the position to be completed based on the project structure information, and then uses the code semantic information and code structure information of the position to be completed to generate the code to be completed at the position to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, the project to be completed is provided with the position to be completed of the code to be completed, the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed. On the one hand, by obtaining the project structure information, it is possible to combine the information in the same file and between different files with the position to be completed, so as to complete the code through a rich data source, which helps to improve the accuracy of code completion; on the other hand, the project structure information displays the code units of each file and the relationship between each code unit in a structured manner, which can quickly locate the code unit where the position to be completed is located, and then obtain the code semantic information and code structure information of the position to be completed, which helps to improve the efficiency of code completion. Therefore, the efficiency and accuracy of code completion can be improved.
[0092] See also Figure 5 , Figure 5 The schematic diagram of the framework of an embodiment of a computer-readable storage medium 50 of the present application. The computer-readable storage medium 50 stores program instructions 51 that can be executed by a processor, and the program instructions 51 are used to implement the steps of the above-mentioned code completion method embodiment.
[0093] In the above scheme, the computer-readable storage medium 50 obtains the project structure information of the project to be completed, and determines the code semantic information and code structure information of the position to be completed based on the project structure information, and then uses the code semantic information and code structure information of the position to be completed to generate the code to be completed at the position to be completed, wherein the project structure information includes the code units of each file in the project to be completed and the relationship between each code unit, the project to be completed is provided with the position to be completed of the code to be completed, the code semantic information includes the code of at least one code unit that matches the semantics of the position to be completed, and the code structure information includes the target information of at least one code unit that has a target relationship with the position to be completed. On the one hand, by obtaining the project structure information, it is possible to combine the information in the same file and between different files with the position to be completed, so as to complete the code through a rich data source, which is helpful to improve the accuracy of code completion; on the other hand, the project structure information displays the code units of each file and the relationship between each code unit in a structured manner, which can quickly locate the code unit at the position to be completed, and then obtain the code semantic information and code structure information of the position to be completed, which is helpful to improve the efficiency of code completion. Therefore, the efficiency and accuracy of code completion can be improved.
[0094] In some embodiments, the functions or modules included in the device provided by the embodiments of the present disclosure can be used to execute the method described in the above method embodiments. The specific implementation can refer to the description of the above method embodiments, and for the sake of brevity, it will not be repeated here.
[0095] The above description of various embodiments tends to emphasize the differences between the various embodiments. The same or similar aspects can be referenced to each other, and for the sake of brevity, they will not be repeated herein.
[0096] In the several embodiments provided in the present application, it should be understood that the disclosed methods and devices can be implemented in other ways. For example, the device implementation described above is only schematic. For example, the division of modules or units is only a logical function division. There may be other division methods in actual implementation, such as units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0097] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0098] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) or a processor (processor) to perform all or part of the steps of each implementation method of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program code.
[0099] If the technical solution of this application involves personal information, the product using the technical solution of this application has clearly informed the personal information processing rules and obtained the individual's voluntary consent before processing the personal information. If the technical solution of this application involves sensitive personal information, the product using the technical solution of this application has obtained the individual's separate consent before processing the sensitive personal information, and at the same time meets the "explicit consent" requirement. For example, on personal information collection devices such as cameras, clear and prominent signs are set to inform that the personal information collection scope has been entered and personal information will be collected. If the individual voluntarily enters the collection scope, it is deemed that he or she agrees to the collection of his or her personal information; or on the device that processes personal information, the personal information processing rules are notified by obvious signs / information, and the individual's authorization is obtained through pop-up information or by asking the individual to upload his or her personal information; among them, the personal information processing rules may include information such as the personal information processor, the purpose of personal information processing, the processing method, and the type of personal information processed.
Claims
1. A code completion method, characterized in that: include: Acquire project structure information of the project to be completed, wherein the project structure information includes code units of each file in the project to be completed and the relationship between the code units, and the project to be completed is provided with a to-be-completed position of the code to be completed; Based on the project structure information, determine the code semantic information and code structure information of the position to be completed, wherein the code semantic information includes the code of at least one of the code units that semantically matches the position to be completed, and the code structure information includes the target information of at least one of the code units that has a target relationship with the position to be completed; The code semantic information and code structure information of the position to be completed are used to generate the code to be completed of the position to be completed.
2. The method according to claim 1, characterized in that Determining code semantic information of the position to be completed based on the project structure information includes: Obtaining the representative code of the position to be completed; Determining the text similarity between the representative code and each target code unit in the item to be completed, wherein the target code unit includes all first code units in the item to be completed that can realize a specific function; The code of at least one of the target code units whose text similarity meets the first similarity requirement is obtained as the code semantic information.
3. The method according to claim 2, characterized in that The at least one target code unit whose text similarity meets the first similarity requirement is: the target code units corresponding to the first number of text similarities in the order of the text similarities from high to low; The step of obtaining the representative code of the position to be completed includes: In response to the position to be completed being located inside the first code unit, the code of the first code unit where the position to be completed is located is used as the representative code; and the target code unit is all the first code units in the item to be completed; In response to the position to be completed being located outside the first code unit, the adjacent code of the position to be completed is used as the representative code, and the adjacent code includes at least one of the following: a first number of lines of code before the position to be completed, and a second number of lines of code after the position to be completed; and the target code unit is all code units in the item to be completed.
4. The method according to claim 1, characterized in that: Determining the code structure information of the location to be completed based on the project structure information includes: Taking the code unit at the position to be completed as a reference code unit; Selecting a code unit that has the target relationship with the reference code unit as an associated code unit, wherein the associated code unit includes at least one of the following: a code unit of the same type as the reference code unit in a first target file, a code unit of the same type as the reference code unit whose name similarity with the reference code unit meets a second similarity requirement, and a code unit of the same type as the reference code unit in a second file whose name similarity with the first file where the reference code unit is located meets a third similarity requirement, wherein the first target file includes at least one of the following: the first file where the position to be completed is located, and a third file that has an import relationship with the first file; Target information of each of the associated code units is obtained as code structure information of the position to be completed.
5. The method according to claim 4, characterized in that The reference code unit is a first code unit capable of implementing a specific function; The associated code unit includes at least one of the following: a first code unit in the first target file that is of the same type as the reference code unit, and a first code unit that has a name similarity to the reference code unit that meets a second similarity requirement and is of the same type as the reference code unit; The obtaining of target information of each of the associated code units includes: Obtain declaration information of each of the associated code units.
6. The method according to claim 5, characterized in that The first code unit includes two types: function and class; In the case where the type of the reference code unit is a function, the associated code units include the following first functions: a function in the first target file, and a function whose name similarity with the reference code unit meets a second similarity requirement, and the declaration information of each of the associated code units includes a declaration of each of the first functions; And / or, in the case where the type of the reference code unit is a class, the associated code unit includes a first class, a second class, and a second function, the first class includes all classes in the first target file and all classes in the second target file, the second target file includes at least one of the following: a fourth file where the parent class of the reference code unit is located, and a fifth file that has an import relationship with the fourth file; the second class includes the parent class of the reference code unit, the child class of the reference code unit, and a class whose name similarity with the reference code unit meets a second similarity requirement; the second function includes functions in the first target file and the second target file; the declaration information of each of the associated code units includes declarations of variables and in-class functions in each of the first classes, declarations of the second classes, and declarations of the second functions.
7. The method according to claim 4, characterized in that The reference code unit is a second code unit; the code unit in the to-be-completed project includes a first code unit capable of realizing a specific function, and the second code unit, each of the files corresponds to a second code unit, and the second code unit in each of the files includes the code in the file except the first code unit; The associated code unit includes at least one of the following: a second code unit of the same type as the reference code unit in the first target file, a second code unit of a second file whose name similarity with the first file where the reference code unit is located meets a third similarity requirement, and a second code unit of other files in the same directory as the first file; The target information of each of the associated code units includes a code of each of the associated code units.
8. The method according to claim 1, characterized in that: The obtaining of the project structure information of the project to be completed includes: A project structure diagram is established to characterize the relationship between the code units of each file in the project to be completed, wherein the project structure diagram includes multiple nodes, each node represents a code unit, and the lines between the nodes represent the relationship between the code units corresponding to the nodes.
9. The method according to claim 8, characterized in that The step of establishing a project structure diagram representing the relationship between the code units of the files in the project to be completed includes: Parsing each file in the to-be-completed project respectively to obtain an abstract syntax tree corresponding to each file, wherein the abstract syntax tree includes nodes corresponding to each code unit in the file, and lines between the nodes corresponding to the file represent relationships between the code units corresponding to the nodes; Based on the relationship between the code units of each file, the nodes of the abstract syntax tree corresponding to each file are connected to obtain the project structure diagram.
10. The method according to claim 1, characterized in that The step of generating the code to be completed for the position to be completed by using the code semantic information and the code structure information of the position to be completed includes: Constructing a prompt text based on the code semantic information and code structure information of the position to be completed; Inputting the prompt text into a code generation model to obtain the code to be completed; The prompt text is used to instruct the code generation model to complete the to-be-completed position in the to-be-completed item based on the code semantic information and the code structure information.
11. The method according to claim 1, characterized in that: The code units of each of the files include at least one first code unit capable of implementing a specific function, and a second code unit, wherein the second code unit includes the remaining code in the file except the first code unit, the first code unit includes at least one of a function and a class, and the second code unit includes an import statement; And / or, the relationship between the code units includes at least one of the following: an import relationship, a call relationship, an inclusion relationship, an encapsulation relationship, and a class hierarchy relationship.
12. A code completion device, characterized in that: include: An acquisition module, used for acquiring project structure information of a to-be-completed project, wherein the project structure information includes code units of each file in the to-be-completed project and the relationship between the code units, and the to-be-completed project is provided with a to-be-completed position of the code to be completed; a determination module, configured to determine, based on the project structure information, code semantic information and code structure information of the position to be completed, wherein the code semantic information includes code of at least one of the code units that semantically matches the position to be completed, and the code structure information includes target information of at least one of the code units that has a target relationship with the position to be completed; A generation module is used to generate the code to be completed at the position to be completed by using the code semantic information and code structure information of the position to be completed.
13. An electronic device, characterized in that: It comprises a memory and a processor coupled to each other, wherein the processor is used to execute program instructions stored in the memory to implement the code completion method according to any one of claims 1 to 11.
14. A computer-readable storage medium having program instructions stored thereon, characterized in that: When the program instructions are executed by a processor, the code completion method according to any one of claims 1 to 11 is implemented.