Intelligent code completion method based on domain code knowledge learning
By performing syntax parsing and function-level entity extraction on the source code of the target project, code completion samples are constructed and the pre-trained model is fine-tuned in a domain-specific manner. This solves the problem of insufficient adaptability of existing technologies in specific projects, achieves efficient and semantically consistent code completion, and improves development efficiency and code quality.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-19
- Publication Date
- 2026-04-03
AI Technical Summary
Existing automated code completion technologies struggle to understand the overall semantics and coding style of a project when applied to specific projects or industrial sectors. General-purpose pre-trained models lack domain-specific adaptability, and large language models are costly to train and difficult to deploy efficiently.
By parsing the source code of the target project, constructing an abstract syntax tree, extracting function-level code entities and their structural information, building code completion samples and dividing them into training, validation, and test sets, and then fine-tuning the pre-trained language model based on these sets, integrating it into the IDE to analyze the code context input by the developer in real time, and generating semantically consistent and syntactically correct code completion results.
It achieves high-quality, semantically consistent code completion within specific projects or domains, reduces the cost of manual programming, improves development efficiency and code quality, supports low-latency deployment, and enhances the practicality of intelligent development tools.
Smart Images

Figure CN121785604A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of software engineering and intelligent development technology, specifically relating to an intelligent code completion method based on domain code knowledge learning. Background Technology
[0002] As the scale and complexity of software systems continue to increase, the cost of manually writing high-quality code completion hints or rules has risen significantly, becoming one of the major burdens in the software development process. To reduce the cost of manual development and improve the efficiency of code completion, researchers have proposed a variety of automated completion technologies.
[0003] Software code completion technology is an important means to improve development efficiency and code quality. Its core goal is to automatically predict possible subsequent code fragments within the context of the code already written by the developer, thereby reducing repetitive coding work, lowering the risk of syntax and logic errors, and assisting developers in implementing complex software systems more efficiently. Early methods were mainly based on sequence or structured modeling, including sequence-based and tree-based methods. Sequence-based methods typically treat source code as a sequence of tokens, learning local context through models such as n-grams, RNNs, and LSTMs to predict subsequent code tokens. Tree-based methods introduce structured representations such as ASTs and control flow graphs (CFGs), modeling code hierarchy, dependencies, and syntactic constraints through tree or graph structures. These methods have achieved certain results in syntax completion and short-distance dependency modeling, but their modeling capabilities are mainly concentrated within local files or functions, making it difficult to fully capture the global semantics of the project and the coding style across modules, thus limiting their accuracy and style consistency in completion for the entire project.
[0004] With the development of deep learning, pre-trained code models based on Next-tokens or Masks have gradually emerged, such as CodeBERT, CodeT5, and CodeGPT. By performing self-supervised pre-training on large-scale open-source code, these models can learn the global semantics and structural patterns of the code to a certain extent, thereby improving the syntactic correctness and fluency of the completed code. However, since the pre-training corpora mainly come from general open-source projects, they still suffer from insufficient style adaptability, poor semantic consistency, and limited business logic matching in specific domains or industrial projects. This can lead to inconsistent coding styles or logic that does not match the actual needs in the completion results.
[0005] In recent years, with the rapid development of large language models in code understanding and generation, researchers have begun to explore their application in automated code completion, such as the GPT series and CodeLlama. These models, also trained on general corpora, can generate natural, executable code snippets, but their adaptation to specific domains faces high costs. Furthermore, deploying large models to intelligent IDEs for real-time completion results in slow inference speeds and high resource consumption, limiting their practicality. Summary of the Invention
[0006] The technical problem to be solved by this invention is that although existing automated code completion technology has evolved from local modeling methods to general pre-trained models and then to large language models, when facing specific projects or industrial fields, local modeling methods still have difficulty understanding the global semantics and coding style of the project, general pre-trained models lack adaptability to specific domains, and large language models, although they have strong semantic capabilities, have high training costs and are difficult to deploy efficiently in IDEs.
[0007] To address the aforementioned technical problems, the present invention provides an intelligent code completion method based on domain code knowledge learning, comprising the following steps: Perform syntax parsing on the source code of the target project or code repository to construct an abstract syntax tree, extract function-level code entities and their structural information, classes and modules, and record the nesting level, call relationship and code block boundary information of function-level code entities; Based on function-level code entities and their structural information, classes and modules, combined with the nesting level, calling relationship and code block boundary information of function-level code entities, split points are selected inside the function according to the boundaries of syntax tree nodes, syntax structure and semantic boundaries. The code context before the split point is used as the completion input and the real code fragment after the split point is used as the completion output. Code completion samples are constructed and divided into training set, validation set and test set. The model is fine-tuned for the target domain based on the training set, validation set, and test set. The training set is used to learn the pre-trained language model, enabling the model to master the coding style, syntax patterns, variable naming habits, and logical structure of the target domain. The validation set is used to evaluate and optimize the model's performance in real time based on code generation metrics during the fine-tuning process. The test set is used to conduct a final test on the model's completion performance after the fine-tuning is completed, and to evaluate its adaptability in the target project or domain. The fine-tuned model is encapsulated as a plugin or service module and integrated into the IDE. By calling the model interface, the code context input by the developer is analyzed in real time. Combined with the context information inside and across functions, candidate completion results are generated. The candidate results are sorted, filtered, and automatically inserted to ensure that the completion content is consistent with the project in terms of syntax, semantics, and engineering style, resulting in semantically consistent and syntactically correct code completion results.
[0008] Preferably, the method further includes annotating cross-module calls, class method references, and global variable dependencies, using the annotated data during the model's domain-specific fine-tuning, so that the generated code can refer to complete context information and scope constraints.
[0009] Preferably, the function-level code entity includes the function name, parameter list, return value type, function body content, local variable declarations, and the file path where they are located.
[0010] Preferably, the static analysis tool includes Tree-sitter, Clang, or other AST-based parsing frameworks to extract the function-level code entities.
[0011] Preferably, the semantic boundaries include newline positions, statement terminators, and / or logical code block boundaries.
[0012] Preferably, the code generation metrics are obtained through BLEU and CodeBLEU.
[0013] Preferably, the model is trained using autoregressive language modeling or masked language modeling.
[0014] This invention provides an intelligent code completion method based on domain code knowledge learning. It extracts function-level entities through static analysis of the target project or code repository, constructing a code completion dataset of semantic blocks within functions. By selecting reasonable segmentation points within functions, the code context before the segmentation point is used as input, and the subsequent actual code is used as the completion output. The pre-trained language model is then fine-tuned in a domain-specific manner, dividing the dataset into training, validation, and test sets. This allows the model to learn the coding style, syntax rules, and logical structure of the target domain. The completion quality is verified and optimized based on code generation evaluation metrics such as BLEU. The fine-tuned model is then packaged as a plugin and integrated into an Integrated Development Environment (IDE). By calling the model interface in real time and analyzing the context input by the developer, it automatically generates semantically consistent and syntactically correct code completion results. This achieves high-quality, syntactically and semantically correct intelligent code completion for specific projects or domains, while supporting low-latency deployment in the IDE, improving development efficiency and code quality, reducing manual programming costs, and enhancing the practicality and intelligence of intelligent development tools. Attached Figure Description
[0015] Figure 1 This is a schematic diagram of the intelligent code completion method based on domain code knowledge learning according to an embodiment of the present invention. Detailed Implementation
[0016] The present invention will be further illustrated below with reference to specific embodiments. It should be understood that these embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. Furthermore, it should be understood that after reading the teachings of this invention, those skilled in the art can make various alterations or modifications to the invention, and these equivalent forms also fall within the scope defined by the appended claims.
[0017] like Figure 1 As shown, this embodiment of the invention provides an intelligent code completion method based on domain code knowledge learning, including the following steps: (i) Domain code parsing and function extraction: Static analysis tools are used to perform syntax parsing on the source code of the target project or code repository, construct an abstract syntax tree (AST), and automatically extract function-level code entities and their structural information, classes and modules based on the AST. The nesting level, calling relationship and code block boundary information of function-level code entities are recorded to provide standardized and structured code corpus for the construction of subsequent code completion datasets.
[0018] At the same time, cross-module calls, class method references, and global variable dependencies are annotated so that the subsequent model completion can refer to complete context information and scope constraints when generating code.
[0019] Through this step, this embodiment of the invention establishes a function entity index system covering the entire project, providing a reliable foundation for semantic block analysis within functions and the construction of code completion datasets.
[0020] The function-level code entity includes the function name, parameter list, return type, function body content, local variable declarations, and file path.
[0021] The static analysis tools include Tree-sitter, Clang, or other AST-based parsing frameworks, used to automatically extract information such as function name, parameter list, function body content, and variable declarations, and record the file path and structure information of the function.
[0022] For C language kernel source code in a specific domain, an automated function-level code parsing and extraction process is built to provide a structured, high-quality corpus foundation for dataset construction and model training. First, the source code directory is traversed, reading the .c and .h files. The chardet library is used to automatically identify the file encoding and convert it to UTF-8 format to ensure accuracy and stability in content reading and processing. After source code reading is complete, Tree-sitter is introduced as the core parsing tool. Its C language syntax module converts the source code into an Abstract Syntax Tree (AST), automatically locating all function definition nodes (function_definition) in the syntax tree and extracting the corresponding start and end line numbers to precisely define the scope of the function body in the source file. Based on the position recorded in the syntax tree, the complete function content is extracted line by line from the source file, preserving function declarations, parameters, comments, and internal logic to ensure semantic integrity and contextual continuity. All extracted function fragments are organized in a unified format, including file path, function name, start and end line numbers, and function source text, forming a standardized function-level code corpus. This enables efficient and accurate function extraction across versions and file structures, providing reliable data support for subsequent dataset construction, model fine-tuning, and semantic modeling.
[0023] (II) Construction of Block-Level Code Completion Dataset: The core is to break down function code into semantically continuous, completeable block-level fragments to form the dataset required for model training, validation, and testing. The specific steps are as follows: Based on the extracted function-level code entities and their structural information, classes, and modules, combined with the nesting level, call relationships, and code block boundary information of the function-level code entities, reasonable split points are selected within the function according to rules such as syntax tree node boundaries, syntax structure, and semantic boundaries (newline positions, statement termination characters, and / or logical code block boundaries) to ensure that the generated completion input and output maintain semantic integrity. The code context before the split point is used as the completion input, and the real code fragment after the split point is used as the completion output, ensuring the semantic integrity and continuity of each sample. Code completion samples are constructed and divided into training, validation, and test sets according to a preset ratio for model training, optimization, and performance evaluation.
[0024] This step can combine internal function variables, calling context, and scope information to generate rich context annotations, thereby improving the model's ability to understand the semantic logic of functions and variable dependencies, and achieving semantically consistent and executable completed training samples.
[0025] After extracting function-level code snippets, a high-quality "input-output" pair dataset suitable for code completion tasks is constructed through multi-stage processing. First, the overall size of the functions is initially screened, retaining function snippets with 5 to 50 lines. Samples that are too short (resulting in insufficient semantic information) or too long (causing excessive completion range and increased noise) are removed to meet the code completion requirements of local semantic prediction. Then, during sample construction, appropriate split points are selected from the latter half of the function snippets, dividing the code into "input snippets" and "completion parts." The code before the split point is used as input, and the code after the split point is used as output, ensuring the model can make reasonable inferences based on context information and generate accurate completions. To ensure the data can be directly used for model training, the input and output text undergo uniform formatting, including replacing newline characters "\n", tab characters "\t", backslashes "\" and their escape characters, removing redundant spaces and indentation, ensuring a standardized, concise, and consistent text structure, and avoiding format differences from affecting training results. After processing, each sample is stored with "input" and "output" fields, forming a structured block-level code completion training sample library. Through the above filtering, partitioning, and normalization operations, a high-quality block-level code completion dataset for a specific domain is constructed, providing reliable data support for subsequent domain-specific fine-tuning of pre-trained language models.
[0026] (III) Fine-tuning of the pre-trained language model: Domain-specific fine-tuning of the model is performed based on the training set, validation set, and test set. The training set is used to learn the pre-trained language model, enabling the model to master the coding style, syntax patterns, variable naming conventions, and logical structure of the target domain. The validation set is used to evaluate and optimize the model's performance in real time during the fine-tuning process. The model parameters are continuously adjusted based on BLEU, CodeBLEU, or other code generation metrics to optimize the completion quality and ensure the semantic accuracy and readability of the completion results. The test set is used to conduct a final test on the model's completion performance after fine-tuning, to evaluate its adaptability in the target project or domain, thereby verifying the model's domain adaptability and completion accuracy.
[0027] The fine-tuning is performed using autoregressive language modeling or masked language modeling.
[0028] Through this step, the pre-trained model not only inherits general grammatical and semantic knowledge, but also gains a deep understanding of a specific project or domain, achieving high-quality, semantically consistent code completion.
[0029] After the dataset was constructed, the CodeT5 model was selected as the base model for fine-tuning. This model is a variant of the T5 model, specifically designed for programming language processing, and possesses strong code understanding and generation capabilities. During fine-tuning, the constructed block-level code completion dataset was first processed into a "source text-target text" format, and the data was loaded using the Pandas library, divided into 80% training set, 10% validation set, and 10% test set. Then, it was packaged into batches using DataLoader for efficient input into the model. During training, the Adam optimizer was used, with appropriate learning rate, batch size, and text length parameters set, and the model weights were updated through multiple iterations. After each epoch, cross-entropy loss was calculated based on the validation set, and BLEU scoring was used to evaluate the similarity between the generated code and the target code to quantify the model's completion effect. After fine-tuning, the model's performance in actual code completion tasks was further validated on the test set. The fine-tuned model was then saved for direct use in subsequent tasks, achieving high-quality code completion for specific projects or domains, significantly improving the accuracy and semantic consistency of the generated code.
[0030] (iv) Plugin Development and Integration: The fine-tuned model is encapsulated as a plugin or service module and integrated into the IDE. By calling the model interface, the code context input by the developer is analyzed in real time. The candidate completion results are generated by combining the context information inside and across functions. The candidate results are sorted, filtered and automatically inserted to ensure that the completion content is consistent with the project in terms of syntax, semantics and engineering style. The result is a semantically consistent and syntactically correct code completion result, which completes efficient, usable and automated intelligent code completion.
[0031] Meanwhile, the plugin optimizes inference performance and resource consumption to achieve low-latency response and meet the real-time completion requirements of the development environment.
[0032] This step allows the fine-tuned model to efficiently provide domain-specific intelligent completion services within the IDE, significantly improving development efficiency, code consistency, and engineering usability.
[0033] By developing a VS Code plugin integrated with a cloud-based fine-tuning model, automated code completion and interactive development support are achieved. In the cloud, a RESTful API interface is built based on the Flask framework, using PyTorch and the Hugging FaceTransformers library to call the fine-tuned CodeT5 model for inference. Users can submit task descriptions via POST requests and obtain generated code. Locally, the VS Code plugin listens for editor events, such as selection changes and document content updates, automatically triggering a model inference request when user activity stops. To improve response efficiency and user experience, the plugin introduces a caching mechanism, using a Map data structure to store input and generated results, and reusing the cache to prevent duplicate requests. The generated code is displayed through the QuickPick interface, where users can choose to accept or cancel suggestions; once accepted, it is automatically inserted into the editor. The interaction flow between the plugin and the cloud API is as follows: the user inputs a task description; the plugin first checks the cache; if no results are found, it calls the cloud API to obtain the generated code, then displays it through the QuickPick interface and completes the insertion operation. Through the collaboration between cloud-based model inference and the local plugin, this process achieves efficient, low-latency, intelligent code completion and generation, significantly improving development efficiency and user experience.
[0034] This invention focuses on function-level static analysis and block-level semantic modeling, combining static structural information with the semantic learning capabilities of pre-trained language models to form a closed-loop process from code parsing, completion data construction, model fine-tuning to IDE integration. This significantly improves the accuracy of code completion, semantic consistency, and project style adaptability, providing strong technical support for the efficient development and intelligent assistance of complex software systems.
[0035] This invention, through the introduction of static code parsing, block-level code completion dataset construction, pre-trained language model fine-tuning, and IDE plugin development and integration mechanisms, achieves a fully intelligent code completion system encompassing function-level code extraction, dataset construction, completion generation, and engineering deployment. Specifically, it first obtains structured function corpora through static code parsing, providing a reliable data foundation for code completion tasks. Based on this, a block-level code completion dataset is constructed to effectively decompose code semantic fragments and define learning objectives. Subsequently, this dataset is used to fine-tune the pre-trained language model to adapt it to the coding habits and logical characteristics of the target project, forming a completion model with domain awareness. Finally, the model is integrated into the IDE as a plugin or service, achieving a closed-loop code completion process from corpus acquisition and model training to practical development scenario deployment, ensuring consistency in accuracy, real-time performance, and engineering practicality of the completion results.
[0036] In summary, the embodiments of the present invention construct an intelligent code completion framework with function-level semantic block analysis as the core and domain-specific pre-trained model fine-tuning as the driving force. This framework can not only achieve high-quality, semantically consistent code completion for specific projects or domains, but also provide real-time, low-latency development assistance with the support of plugin integration, significantly improving development efficiency and code quality, reducing manual coding costs, and providing an efficient and scalable technical solution for intelligent software development and engineering practice.
Claims
1. An intelligent code completion method based on domain code knowledge learning, characterized in that, Includes the following steps: Perform syntax parsing on the source code of the target project or code repository to construct an abstract syntax tree, extract function-level code entities and their structural information, classes and modules, and record the nesting level, call relationship and code block boundary information of function-level code entities; Based on function-level code entities and their structural information, classes and modules, combined with the nesting level, calling relationship and code block boundary information of function-level code entities, split points are selected inside the function according to the boundaries of syntax tree nodes, syntax structure and semantic boundaries. The code context before the split point is used as the completion input and the real code fragment after the split point is used as the completion output. Code completion samples are constructed and divided into training set, validation set and test set. The model is fine-tuned for the target domain based on the training set, validation set, and test set. The training set is used to learn the pre-trained language model, enabling the model to master the coding style, syntax patterns, variable naming habits, and logical structure of the target domain. The validation set is used to evaluate and optimize the model's performance in real time based on code generation metrics during the fine-tuning process. The test set is used to conduct a final test on the model's completion performance after the fine-tuning is completed, and to evaluate its adaptability in the target project or domain. The fine-tuned model is encapsulated as a plugin or service module and integrated into the IDE. By calling the model interface, the code context input by the developer is analyzed in real time. Combined with the context information inside and across functions, candidate completion results are generated. The candidate results are sorted, filtered, and automatically inserted to ensure that the completion content is consistent with the project in terms of syntax, semantics, and engineering style, resulting in semantically consistent and syntactically correct code completion results.
2. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The method also includes annotating cross-module calls, class method references, and global variable dependencies. The annotated data is used during the model's domain-specific fine-tuning to ensure that the generated code can refer to complete context information and scope constraints.
3. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The function-level code entity includes the function name, parameter list, return type, function body content, local variable declarations, and file path.
4. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The static analysis tools include Tree-sitter, Clang, or other AST-based parsing frameworks to extract the function-level code entities.
5. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The semantic boundaries include newline positions, statement terminators, and / or logical code block boundaries.
6. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The code generation metrics are obtained through BLEU and CodeBLEU.
7. The intelligent code completion method based on domain code knowledge learning as described in claim 1, characterized in that, The model is trained using either autoregressive language modeling or masked language modeling.