Code completion method, computer program product and computer device
By constructing a context-aware compressed prefix tree and type inference, combined with the contextual features of the editing position, the problem of low accuracy and slow response speed of existing code completion methods is solved, realizing intelligent completion of closed-source software and improving response speed and accuracy.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SHENZHEN JINGYUAN INFORMATION TECH CO LTD
- Filing Date
- 2026-05-20
- Publication Date
- 2026-07-10
AI Technical Summary
Existing code completion methods cannot effectively combine the contextual features of the editing position for filtering, resulting in low completion accuracy, slow response speed, and inability to parse the internal structure of binary packets, leading to network latency and privacy leakage issues.
By constructing a context-aware compressed prefix tree to store the signature of the application programming interface and its context label, and combining type inference and multi-factor sorting, offline intelligent completion is achieved.
It significantly improves the response speed and accuracy of completion, supports intelligent completion for closed-source software, and enhances the semantic relevance of completion results and user experience.
Smart Images

Figure CN122363679A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of software engineering and intelligent programming assistance technology, and in particular relates to a code completion method, computer program products, and computer equipment. Background Technology
[0002] Code auto-completion is one of the core functions of a code editor. It can provide suggestions for completing program symbols such as function names, variable names, and class names based on the prefixes of the strings entered by the user while coding, so as to improve coding efficiency and reduce the rate of syntax errors.
[0003] Current mainstream code completion methods typically rely on static analysis or abstract syntax trees to parse the source file being edited in real time, extracting available symbols defined within or imported through import statements. These symbols are then matched using prefix trees or hash tables, and the matching results are sorted and presented to the user. Some solutions also employ cloud-based deep learning models to predict possible code snippets by analyzing large corpora of code.
[0004] However, the above methods have the following technical drawbacks: traditional prefix trees cannot carry context labels, which means that the context features of the editing position cannot be combined for filtering during completion, resulting in low completion accuracy; real-time parsing and dynamic indexing methods result in slow response speed; and cloud-based models suffer from network latency, privacy leaks, and offline unavailability. Summary of the Invention
[0005] One object of the present invention is to overcome at least one technical defect in the prior art and to provide a code completion method, a computer program product, and a computer device.
[0006] A further objective of this invention is to improve autocomplete response speed and enable offline availability by constructing an index to store the signature and context labels of the application programming interface.
[0007] Another further objective of this invention is to overcome the technical deficiency of existing completion engines in being unable to parse the internal structure of binary packets.
[0008] Another further objective of this invention is to improve the accuracy of completion by combining the contextual features of the editing position with type inference during completion queries.
[0009] Another further objective of this invention is to perform multi-factor weighted ranking on the candidate completion set to improve the relevance of the completion results and the user experience.
[0010] Specifically, according to a first aspect of the present invention, the present invention provides a code completion method applied to a code editor, comprising: Static analysis is performed on the target codebase to extract the signatures of the application programming interfaces and their corresponding context tags; Construct a context-aware compressed prefix tree to store the signature and the context label, and generate an index file; In response to the launch of the code editor, the index file is loaded to restore the context-aware compressed prefix tree; In response to the code editor opening a source file, the source file is parsed to generate an abstract syntax tree and the context features of the editing location are extracted; Obtain the string prefix entered by the user at the editing position, and perform a prefix matching query in the context-aware compressed prefix tree based on the context features to obtain a completion candidate set; The candidate completion set is sorted, and the sorting result is returned to the code editor.
[0011] Optionally, the target codebase includes Python modules provided as compiled binary packages; and the static analysis includes parsing the binary packages to extract the signature of the application programming interface therefrom.
[0012] Optionally, the context-aware compressed prefix tree uses path compression and subtree sharing mechanisms to store the signature and the context label.
[0013] Optionally, the context-aware compressed prefix tree is a multi-way tree structure, where each node includes: A character fragment used to match the prefix of the string; Child node pointers are used to point to the next level node based on the subsequent characters of the string prefix during the matching process; The context label is used to compare with the context features of the edit location to filter matching application programming interfaces; and The signature of the application programming interface is used to display interface information to the user after a successful match.
[0014] Optionally, each of the nodes further includes: A unique identifier, used to uniquely identify the character segment; Metadata is used to store the parameter signatures, return types, document summaries, and usage frequency of the application programming interface; and Reference counting is used to manage the memory lifecycle of the node.
[0015] Optionally, the context label includes: Scope type, used to indicate the valid scope of code symbols; Object type, used to indicate the data type of a variable or expression; Access permissions, used to indicate the access level of an application programming interface; and Frequency of use is used to indicate the frequency of calls to the application's programming interface in the preset codebase or historical coding behavior.
[0016] Optionally, the step of performing a prefix matching query in the context-aware compressed prefix tree based on the context features to obtain a completion candidate set includes: Based on the aforementioned contextual features, the inferred type set of the current object at the edit location is determined by the type inference engine; Using the string prefix as the matching benchmark, nodes that match the string prefix are retrieved in the context-aware compressed prefix tree to obtain an initial matching set; The initial matching set is filtered using the inference type set to remove nodes that do not match the inference type set, thus obtaining a completion candidate set.
[0017] Optionally, the step of sorting the candidate completion set includes: Calculate the matching degree between each candidate in the completion candidate set and the context feature; Obtain the historical usage frequency of the application programming interface; Calculate the string naming similarity between the string prefix and the candidate name; The strings are sorted by weight based on their matching degree, historical usage frequency, and string naming similarity.
[0018] According to a second aspect of the present invention, the present invention provides a computer program product comprising a computer program that, when executed by a processor, implements the steps of the code completion method described in any one of the above descriptions.
[0019] According to a third aspect of the present invention, a computer device is provided, including a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the steps of the code completion method described in any one of the preceding descriptions.
[0020] The code completion method of this invention performs static analysis on the target code library to extract the signature of the application programming interface and its corresponding context label. It pre-constructs a context-aware compressed prefix tree for storing the signature and context label and generates an index file. When the code editor starts, it loads the index file to restore the context-aware compressed prefix tree. During coding, it parses the source file to generate an abstract syntax tree and extracts the context features of the editing position. It obtains the string prefix entered by the user at the editing position, performs a prefix matching query in the context-aware compressed prefix tree based on the context features to obtain a completion candidate set, and sorts the completion candidate set before returning it to the code editor. This achieves a completely offline intelligent completion function, significantly improving the response speed and accuracy of completion.
[0021] Furthermore, the code completion method of the present invention limits the target code library to Python modules provided in the form of compiled binary packages, and parses the binary packages to extract the signature of the application programming interface therein. This enables the present invention to obtain the signature information of the application programming interface even if the target code library is distributed in closed-source or compiled binary form. This overcomes the technical defect of existing completion engines that cannot parse the internal structure of binary packages, and realizes complete intelligent completion support for closed-source software development kits and private modules.
[0022] Furthermore, the code completion method of this invention determines the inferred type set of the current object at the editing position based on contextual features through a type inference engine. When performing a prefix matching query in the context-aware compressed prefix tree, this inferred type set is used to filter the initial matching set item by item, eliminating nodes that do not match the inferred type set, thereby retaining type-compatible completion candidates. This process effectively excludes application programming interfaces that are inconsistent with the current variable type, scope constraints, or expression context, avoiding presenting users with unusable or semantically irrelevant completion options, thus significantly improving the semantic relevance of the completion result to the current encoding context and the overall completion accuracy.
[0023] Furthermore, the code completion method of the present invention calculates the matching degree between each completion candidate and the context features, obtains the historical usage frequency of the application programming interface, calculates the string naming similarity between the string prefix and the candidate name, and performs weighted sorting based on the matching degree, the historical usage frequency, and the string naming similarity. This results in the completion candidates that best match the context features of the current editing position, have the highest historical usage frequency, and have the closest string names being placed at the top of the completion candidate set. This significantly improves the semantic relevance, usability, and user selection efficiency of the completion results, further enhancing the coding experience.
[0024] The above and other objects, advantages and features of the present invention will become more apparent to those skilled in the art from the following detailed description of specific embodiments of the invention in conjunction with the accompanying drawings. Attached Figure Description
[0025] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings: Figure 1 This is a flowchart of a code completion method according to an embodiment of the present invention; Figure 2 This is a flowchart of obtaining a completion candidate set according to an embodiment of the present invention; Figure 3 This is a flowchart of sorting the candidate completion set according to an embodiment of the present invention; Figure 4 This is a schematic diagram of a computer program product according to an embodiment of the present invention; Figure 5 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present invention; Figure 6 This is a schematic block diagram of a computer device according to an embodiment of the present invention. Detailed Implementation
[0026] Reference will now be made in detail to embodiments of the invention, one or more of which are illustrated in the accompanying drawings. The various embodiments provided are intended to explain the invention and not to limit it. In fact, various modifications and variations to the invention will be apparent to those skilled in the art without departing from the scope or spirit of the invention. For example, a feature illustrated or described as part of one embodiment may be used with another embodiment to produce yet another embodiment. Therefore, the invention is intended to cover such modifications and variations within the scope of the appended claims and their equivalents.
[0027] In the description of this embodiment, it should be understood that the term "multiple" means at least two, such as two, three, etc., unless otherwise explicitly specified. When a feature "includes or contains" one or more of the features it covers, unless otherwise specifically described, this indicates that other features are not excluded and may be further included.
[0028] In the description of this embodiment, the terms "one embodiment," "some embodiments," "some examples," "one example," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.
[0029] Figure 1 This is a flowchart of a code completion method according to an embodiment of the present invention. This code completion method can be applied to code editors, such as Python code editors, Visual Studio Code, PyCharm, or other integrated development environments that support code completion functionality. Figure 1 As shown, the code completion method in this embodiment includes at least the following steps: Step S101: Perform static analysis on the target code library to extract the signature of the application programming interface and its corresponding context label.
[0030] Specifically, the target code library includes, but is not limited to, Python software development kits (SDKs), third-party closed-source libraries, and the project's own business code.
[0031] Static analysis refers to the parsing and processing of the structure, interface, and symbol information of a target code library without running the target code or relying on a compilation and execution environment.
[0032] Static analysis is used to extract the signatures of each application programming interface (API), including but not limited to the interface name, parameter list, parameter type, and return value type. At the same time, the corresponding context tags are matched and extracted for each API to characterize semantic information such as the use case, scope of validity, and access attributes of the interface.
[0033] Step S102: Construct a context-aware compressed prefix tree to store signatures and context labels, and generate an index file.
[0034] Specifically, the Context-Aware Compressed Trie (CACT) uses the character features of the application programming interface (API) as its index and context labels as its semantic filtering criterion. It is specifically designed for storing and managing interface signatures and context association information. Simultaneously, an index file is generated based on the constructed Context-Aware Compressed Trie, which is used to achieve persistent storage and fast loading of the trie.
[0035] In step S103, in response to the launch of the code editor, the index file is loaded to restore the context-aware compressed prefix tree.
[0036] Specifically, when a user launches the code editor, the system automatically reads and loads the index file generated in step S102, and fully reconstructs the context-aware compressed prefix tree in memory based on the structured data in the index file. This avoids the performance loss caused by real-time data structure construction during coding and ensures low-latency response of the code completion function.
[0037] In step S104, in response to the code editor opening the source file, the source file is parsed to generate an abstract syntax tree and the context features of the editing location are extracted.
[0038] Specifically, when a user opens a source file to be edited in the code editor, the system performs syntax parsing on the source file and generates an Abstract Syntax Tree (AST). The AST can accurately represent the syntactic structure, hierarchical relationships, symbol definitions, and scope distribution of the code. Based on the AST, the system extracts the contextual features corresponding to the user's current editing position. These contextual features are used to represent the semantic information of the current editing position, such as the syntactic environment, object type, available scope, and imported modules, providing a basis for subsequent accurate code completion.
[0039] Step S105: Obtain the string prefix entered by the user at the edit position, and perform a prefix matching query in the context-aware compressed prefix tree based on context features to obtain a completion candidate set.
[0040] Specifically, the string prefix entered by the user at the editing position is obtained in real time and used as the keyword for completion search; combined with the context features extracted in step S104, a prefix matching query is performed in the loaded context-aware compressed prefix tree to filter out application programming interfaces that match the string prefix and conform to the semantic constraints of the current context, forming a completion candidate set.
[0041] Step S106: Sort the candidate set for completion and return the sorting result to the code editor.
[0042] Specifically, the candidate completion set obtained in step S105 can be sorted in a multi-dimensional weighted manner. The sorting rules are calculated in combination with factors such as the degree of context matching, interface usage habits, and character similarity. The sorted completion results are then returned to the completion display interface of the code editor for users to quickly select and use, thus completing the code completion process.
[0043] Using the above method, by performing static analysis on the target codebase, the signature of the application programming interface and its corresponding context label are extracted. A context-aware compressed prefix tree for storing the signature and context label is pre-built and an index file is generated. When the code editor starts, the index file is loaded to restore the context-aware compressed prefix tree. During coding, the source file is parsed to generate an abstract syntax tree and extract the context features of the editing position. The string prefix entered by the user at the editing position is obtained. Based on the context features, a prefix matching query is performed in the context-aware compressed prefix tree to obtain the completion candidate set. After sorting the completion candidate set, it is returned to the code editor, thus realizing a completely offline intelligent completion function, which significantly improves the response speed and accuracy of completion.
[0044] In actual development, many Python modules and SDKs provided by companies or third parties do not publicly disclose their source code, but are instead released as compiled binary packages (such as .so, .pyd, .pyc, etc.). These binary packages cannot be directly read by text, their source code cannot be viewed, and their function and class definitions cannot be parsed. As a result, traditional code completion engines cannot recognize their APIs and therefore cannot provide completion functionality for closed-source modules.
[0045] In this embodiment, the target code library includes Python modules provided as compiled binary packages, and static analysis includes parsing the binary packages to extract the signatures of the application programming interfaces (APIs). Therefore, even if the target code library is distributed in closed-source or compiled binary form, this embodiment can still extract the signature information of its APIs, thereby overcoming the technical deficiency of existing completion engines that cannot parse the internal structure of binary packages, and achieving complete intelligent completion support for closed-source software development kits and proprietary modules.
[0046] In an optional instance, the context-aware compressed prefix tree uses path compression and subtree sharing mechanisms to store signatures and context tags, thereby significantly reducing memory usage and improving retrieval efficiency.
[0047] Specifically, the context-aware compressed prefix tree can be a multi-branch tree structure, where each node can include a character fragment, a child node pointer, a context label, and a signature of the application programming interface (API). The character fragment is used to match the string prefix; the child node pointer is used to point to the next level node based on subsequent characters of the string prefix during the matching process; the context label is used to compare with the context features of the editing position to filter matching APIs; and the signature of the API is used to display interface information to the user after a successful match.
[0048] Furthermore, each node can also include a unique identifier, metadata, and a reference count. The unique identifier is used to uniquely identify character fragments, the metadata is used to store the parameter signatures, return types, document summaries, and usage frequencies of the application programming interface, and the reference count is used to manage the node's memory lifecycle.
[0049] Contextual tags can include scope type, object type, access permissions, and usage frequency. Scope type indicates the effective scope of code symbols, object type indicates the data type of variables or expressions, access permissions indicate the access level of the application programming interface (API), and usage frequency indicates the frequency of calls to the API in the default codebase or historical coding behavior.
[0050] In this embodiment, the index file can be generated during the continuous integration or continuous deployment (CI / CD) phase. By performing static analysis on the target codebase, the signatures and context tags of the application programming interfaces (APIs) are extracted, thereby constructing a context-aware compressed prefix tree and generating an index file in .cact format. This index file is then distributed along with the code editor's installation package. When a customer first installs and launches the editor, they automatically obtain the complete index file, enjoying full proprietary API completion capabilities without additional configuration or internet download.
[0051] It should be noted that the above code completion method can be deeply integrated into the code editor. The completion engine shares memory with the editor's document model, syntax parser, and event bus, allowing context data such as cursor position, variable binding, and scope information in the editor to be synchronized to the completion engine in real time without the need for data exchange through inter-process communication, thus achieving zero communication latency. Furthermore, the completion engine is embedded directly into the editor as a core module, rather than running as a plugin. Because the completion engine and the editor run in the same process and share core data structures, any changes in the editor (such as cursor movement, variable assignment, code completion, syntax highlighting, and symbol jumping) can be driven by the same symbol system, avoiding the fragmented experience caused by multi-component interactions in plugin solutions. Through this deep integration, this invention maintains functional integrity while stabilizing the completion response time at 8 to 15 milliseconds, significantly outperforming traditional plugin solutions that rely on inter-process communication.
[0052] Figure 2 This is a flowchart of obtaining a completion candidate set according to an embodiment of the present invention, such as... Figure 2 As shown, performing a prefix matching query in a context-aware compressed prefix tree based on contextual features to obtain a completion candidate set includes at least the following steps: Step S201: Based on contextual features, determine the set of inferred types for the current object at the editing location using the type inference engine.
[0053] Specifically, based on the contextual features of the editing position extracted in step S104 (such as the variable type, scope information, and imported modules at the current cursor position), the type inference engine performs type inference on the current object at the editing position. The type inference engine can employ a static type inference algorithm, inferring one or more possible data types of the current object based on information such as variable assignments, function return type annotations, and historical usage patterns, forming an inferred type set. For example, if the current object has been assigned a string type, the inferred type set may include the str type; if the current object is the return value of a function call and the function has a type annotation, its inferred type can be determined based on the annotation.
[0054] Step S202: Using the string prefix as the matching benchmark, retrieve the nodes that match the string prefix in the context-aware compressed prefix tree to obtain the initial matching set.
[0055] Specifically, the string entered by the user at the edit location is retrieved as the prefix query term. Using this string prefix as the matching benchmark, the search proceeds layer by layer downwards from the root node of the context-aware compressed prefix tree, following the tree's hierarchical structure: first, the character fragment of the root node is matched against the first character of the string prefix; if a match is found, the search continues along the corresponding child node pointers to match subsequent characters, and so on, until the entire string prefix is matched or no further matching is possible. All application programming interfaces (APIs) corresponding to nodes that successfully match the string prefix during the search process are collected to form an initial matching set. This initial matching set contains all candidate APIs whose names begin with the string prefix.
[0056] Step S203: Filter the initial matching set using the inference type set, remove nodes that do not match the inference type set, and obtain the completion candidate set.
[0057] Specifically, the inferred type set determined in step S201 is used to filter the initial matching set obtained in step S202. For each completion candidate in the initial matching set, it is checked whether the return type or parameter type declared in the signature of its application programming interface is compatible with any type in the inferred type set (e.g., the same type, inheritance relationship, or implicit type conversion rules). If compatible, the candidate is retained; if incompatible, it is removed from the set. After filtering, the remaining completion candidates constitute the completion candidate set that is compatible with the type context semantics of the current editing position.
[0058] Using the above method, the inferred type set of the current object at the editing position is determined by the type inference engine based on context features. When performing prefix matching query in the context-aware compressed prefix tree, the inferred type set is used to filter the initial matching set and remove nodes that do not match the inferred type set. This effectively filters out type-compatible completion candidates, which greatly improves the semantic relevance of the completion result to the current encoding context and the overall completion accuracy.
[0059] Figure 3 This is a flowchart of sorting the candidate completion set according to an embodiment of the present invention, such as... Figure 3 As shown, sorting the candidate completion set includes at least the following steps: Step S301: Calculate the matching degree between each candidate in the candidate set and the context features.
[0060] Specifically, for each completion candidate in the completion candidate set obtained in step S105, the matching degree between it and the editing position context features extracted in step S104 is calculated. This matching degree is used to measure the semantic consistency between the current completion candidate and the encoding context (e.g., current scope type, object type, imported modules, etc.). For example, if the current editing position is inside a class definition, the matching degree of methods or properties belonging to that class will be higher than that of global functions; if the current object has a specific data type, the matching degree of APIs compatible with that type will be higher.
[0061] Step S302: Obtain the historical usage frequency of the application programming interface.
[0062] Specifically, obtain the historical usage frequency of each application programming interface (API) in the candidate completion set. This historical usage frequency can be a pre-statistical total number of times the API was called in the target codebase or historical coding data, or a normalized frequency. APIs with higher historical usage frequencies generally have better reliability and versatility, and therefore should be given higher weight in the sorting.
[0063] Step S303: Calculate the string naming similarity between the string prefix and the candidate name.
[0064] Specifically, the system retrieves the string prefix entered by the user at the edit position and calculates the string naming similarity between this prefix and the name of each completion candidate. Naming similarity can be calculated based on edit distance, longest common substring length, prefix matching length, or other string similarity algorithms. The closer the string prefix is to the candidate name, the higher the degree of match between the candidate and the actual encoding intent, and it should be presented first during sorting.
[0065] Step S304: Perform a weighted sort based on matching degree, historical usage frequency, and string naming similarity.
[0066] Specifically, the candidate set for completion is weighted and ranked based on three factors: the matching degree calculated in step S301, the historical usage frequency obtained in step S302, and the string naming similarity calculated in step S303. In practical applications, different weight coefficients can be set for each of the three factors. For example, a higher weight can be set for the matching degree to emphasize contextual semantic consistency, a medium weight can be set for the historical usage frequency to reflect the reliability of the API, and a higher weight can be set for the naming similarity to prioritize the candidates that are closest to the user's input. After weighted ranking, the ranking result is returned to the code editor, which then presents it to the user in the form of a drop-down list or similar format.
[0067] Using the above method, by calculating the matching degree between each completion candidate and the context features, obtaining the historical usage frequency of the application programming interface, calculating the string naming similarity between the string prefix and the candidate name, and weighting and sorting according to three factors—matching degree, historical usage frequency, and string naming similarity—the completion candidates that best match the context features of the current editing position, have the highest historical usage frequency, and have the closest string names are placed at the top of the completion candidate set. This significantly improves the semantic relevance, usability, and user selection efficiency of the completion results, and further improves the coding experience.
[0068] It is understood that this invention achieves an out-of-the-box, millisecond-level response, and secure and controllable code completion experience through an intelligent completion method based on a context-aware compressed prefix tree. This code completion method is essentially a semantic completion method, which combines the parsing results of the abstract syntax tree with the inference results of the type inference engine to achieve multi-dimensional context matching such as scope type, object type, and imported modules. This solves the problems of invisibility, high latency, high resource consumption, and fragmented experience that exist in existing Python code completion technologies in enterprise-level private application programming interface scenarios.
[0069] The code completion methods described in the above embodiments can be implemented using computer program instructions. Therefore, this embodiment also provides a computer program product 41, a computer-readable storage medium 42, and a computer device 43. Figure 4 This is a schematic diagram of a computer program product according to an embodiment of the present invention. Figure 5 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present invention. Figure 6 This is a schematic block diagram of a computer device according to an embodiment of the present invention.
[0070] Computer program product 41 includes computer program 411, which, when executed by processor 431, implements any of the aforementioned code completion methods. Computer-readable storage medium 42 stores the aforementioned computer program 411, which, when executed by processor 431, implements any of the aforementioned code completion methods. Computer device 43 may include memory 432, processor 431, and computer program 411 stored in memory 432 and running on processor 431.
[0071] The computer program 411 used to perform the operations of this invention may be assembly instructions, instruction set architecture (ISA) instructions, machine instructions, machine-related instructions, microcode, firmware instructions, status setting data, integrated circuit configuration data, or source code or object code written in any combination of one or more programming languages and procedural programming languages.
[0072] Computer program 411 may execute entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the latter case, the remote computer may be connected to the user's computer via any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider). In some embodiments, to perform aspects of the invention, electronic circuits including, for example, programmable logic circuits, field-programmable gate arrays (FPGAs), or programmable logic arrays (PLAs) may execute computer-readable program instructions to personalize the electronic circuits by utilizing state information of computer-readable program instructions.
[0073] For the purposes of this embodiment, computer program product 41 refers to a related product containing computer program 411. Computer-readable storage medium 42 is a tangible device capable of holding and storing computer program 411, and can be any device capable of containing, storing, communicating, propagating, or transmitting computer program 411 for use by or in conjunction with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable storage medium 42 include: portable computer disks, hard disks, random access memory 432 (RAM), read-only memory 432 (ROM), erasable programmable read-only memory 432 (EPROM or flash memory), static random access memory 432 (SRAM), portable optical disc read-only memory 432 (CD-ROM), digital multifunction disc (DVD), memory stick, floppy disk, mechanical encoding device, and any suitable combination thereof.
[0074] Therefore, those skilled in the art should recognize that although numerous exemplary embodiments of the present invention have been shown and described in detail herein, many other variations or modifications conforming to the principles of the present invention can be directly determined or derived from the disclosure of the present invention without departing from the spirit and scope of the invention. Thus, the scope of the present invention should be understood and construed as covering all such other variations or modifications.
Claims
1. A code completion method, applied to a code editor, characterized in that, include: Static analysis is performed on the target codebase to extract the signatures of the application programming interfaces and their corresponding context tags; Construct a context-aware compressed prefix tree to store the signature and the context label, and generate an index file; In response to the launch of the code editor, the index file is loaded to restore the context-aware compressed prefix tree; In response to the code editor opening a source file, the source file is parsed to generate an abstract syntax tree and the context features of the editing location are extracted; Obtain the string prefix entered by the user at the editing position, and perform a prefix matching query in the context-aware compressed prefix tree based on the context features to obtain a completion candidate set; The candidate completion set is sorted, and the sorting result is returned to the code editor.
2. The code completion method according to claim 1, characterized in that, The target codebase includes Python modules provided as compiled binary packages; and the static analysis includes parsing the binary packages to extract the signature of the application programming interface therefrom.
3. The code completion method according to claim 1, characterized in that, The context-aware compressed prefix tree uses path compression and subtree sharing mechanisms to store the signature and the context label.
4. The code completion method according to claim 1, characterized in that, The context-aware compressed prefix tree is a multi-way tree structure, and each node includes: A character fragment used to match the prefix of the string; Child node pointers are used to point to the next level node based on the subsequent characters of the string prefix during the matching process; The context label is used to compare with the context features of the edit location to filter matching application programming interfaces; and The signature of the application programming interface is used to display interface information to the user after a successful match.
5. The code completion method according to claim 4, characterized in that, Each of the nodes also includes: A unique identifier, used to uniquely identify the character segment; Metadata is used to store the parameter signatures, return types, document summaries, and usage frequency of the application programming interface; and Reference counting is used to manage the memory lifecycle of the node.
6. The code completion method according to claim 1, characterized in that, The context labels include: Scope type, used to indicate the valid scope of code symbols; Object type, used to indicate the data type of a variable or expression; Access permissions, used to indicate the access level of an application programming interface; and Frequency of use is used to indicate the frequency of calls to the application's programming interface in the preset codebase or historical coding behavior.
7. The code completion method according to claim 1, characterized in that, Based on the context features, the step of performing a prefix matching query in the context-aware compressed prefix tree to obtain a completion candidate set includes: Based on the aforementioned contextual features, the inferred type set of the current object at the edit location is determined by the type inference engine; Using the string prefix as the matching benchmark, nodes that match the string prefix are retrieved in the context-aware compressed prefix tree to obtain an initial matching set; The initial matching set is filtered using the inference type set to remove nodes that do not match the inference type set, thus obtaining a completion candidate set.
8. The code completion method according to claim 1, characterized in that, The steps for sorting the candidate completion set include: Calculate the matching degree between each candidate in the completion candidate set and the context feature; Obtain the historical usage frequency of the application programming interface; Calculate the string naming similarity between the string prefix and the candidate name; The strings are sorted by weight based on their matching degree, historical usage frequency, and string naming similarity.
9. A computer program product, characterized in that, It includes a computer program that, when executed by a processor, implements the steps of the code completion method according to any one of claims 1 to 8.
10. A computer device, characterized in that, The method includes a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the steps of the code completion method according to any one of claims 1 to 8.