Code editing method and device, storage medium and program product
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- IFLYTEK CO LTD
- Filing Date
- 2026-03-31
- Publication Date
- 2026-08-04
AI Technical Summary
[0005]本发明提供一种代码编辑方法、装置、存储介质和程序产品,用以解决现有技术中代码编辑效率低的缺陷,实现高效高质的代码编辑方法
[0020]本发明提供的代码编辑方法、装置、存储介质和程序产品,获取用于指示代码编辑需求的初始上下文信息,且初始上下文信息包括多个历史编辑位置信息、多个历史编辑位置信息指示的多个历史编辑位置对应的代码变更记录和当前编辑位置的代码上下文,从而确保后续可以准确预测目标编辑位置和代码预测信息,进而提高代码编辑的准确性;将初始上下文信息输入至相关性筛选模型,得到相关性筛选模型输出的与当前编辑需求相关的代码上下文信息,从而后续可以基于更相关、更精简的代码上下文信息进行位置预测和代码预测,进而进一步提高代码编辑的准确性和效率;将代码上下文信息和初始上下文信息中未被筛选的上下文信息输入至位置预测模型,得到位置预测模型输出的待编辑的目标编辑位置,从而无需开发者手动确定待编辑的目标编辑位置,进而提高代码编辑效率,最终提升用户使用体验,且无需依赖专家经验人工确认目标编辑位置,从而提高代码编辑的准确性;将代码上下文信息和初始上下文信息中未被筛选的上下文信息输入至代码编辑模型,得到代码编辑模型输出的目标编辑位置的代码预测信息,从而无需开发者手动编写代码,进而提高代码编辑效率,最终提升用户使用体验。
Smart Images

Figure CN122507355A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software development technology, and in particular to a code editing method, apparatus, storage medium, and program product. Background Technology
[0002] With the rapid development of technology, the demands of software development are constantly increasing. Code completion is one of the most widely used functions in code editing tools. Code completion refers to the automatic provision of possible completion options by the code editing tool for the user to choose from. Traditional code completion methods mainly rely on static source code analysis, such as completion based on syntax and Application Programming Interface (API); however, this traditional method cannot understand the semantics of the code, and the completion options it provides may not match the developer's intentions, resulting in poor code completion performance and impacting the user experience.
[0003] Currently, machine learning is used to train a code completion model that can understand the semantics of code, thus providing more accurate code completion. However, existing code completion models predict the next possible token (lexical unit) based on existing code, but in real-world code editing scenarios, developers don't always add new code (complete code); more often, they continuously modify and optimize existing code. Therefore, in code modification scenarios, developers need to manually determine the editing position of existing code before making changes. For example, users might need to manually delete erroneous code before code completion, leading to low editing efficiency and impacting user experience. Furthermore, developers need to manually confirm the next editing position, which also results in low editing efficiency and over-reliance on expert experience, potentially leading to low accuracy.
[0004] In summary, existing code completion methods can only achieve automatic code completion, not automatic code editing. In code editing scenarios, developers still need to manually determine the editing location, resulting in low code editing efficiency. Summary of the Invention
[0005] This invention provides a code editing method, apparatus, storage medium, and program product to address the shortcomings of low code editing efficiency in the prior art and to achieve a highly efficient and high-quality code editing method.
[0006] This invention provides a code editing method, comprising: Obtain initial context information for indicating code editing needs; the initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; The initial context information is input into the relevance filtering model to obtain the code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is built based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records filtered from the code change records corresponding to the multiple historical editing locations; The code context information and the unfiltered context information from the initial context information are input into the location prediction model to obtain the target editing position to be edited output by the location prediction model; the location prediction model is used to predict the editing position related to the current editing needs; The code context information and the unfiltered context information from the initial context information are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code editing model is used to predict the code at the target editing position. Based on the code prediction information and the target editing location, code editing is performed.
[0007] According to a code editing method provided by the present invention, the code editing based on the code prediction information and the target editing position includes: Based on the target editing position, determine the next editing position; If the next editing position is within the current view window, based on the code prediction information, the predicted code is displayed at the next editing position; If the next editing position is not within the current visible window, the predicted code is displayed in a preset edge area of the current visible window based on the code prediction information; If the user accepts the predicted code based on the user's instructions, the code is edited at the next editing position based on the predicted code.
[0008] According to a code editing method provided by the present invention, the number of target editing locations is one or more; Determining the next editing position based on the target editing position includes: Determine the next editing position according to the order of the target editing positions in the current code file; After determining that the user accepts the predicted code based on the user instruction, and then editing the code at the next editing position based on the predicted code, the method further includes: If there are still unedited editing positions among the target editing positions, return to the step of determining the next editing position according to the order of the target editing positions in the current code file.
[0009] According to a code editing method provided by the present invention, the step of displaying predicted code at the next editing position based on the code prediction information includes: If the type of editing behavior at the next editing position is determined to include code modification based on the code prediction information, the predicted code is displayed in a pop-up window at the next editing position. If the type of editing behavior at the next editing position, determined based on the code prediction information, does not include code modification but includes code completion, the predicted code is displayed in grayscale at the next editing position.
[0010] According to a code editing method provided by the present invention, the step of displaying predicted code in a preset edge region of the currently visible window based on the code prediction information includes: Based on the code prediction information, the original code and predicted code at the next editing position are displayed in a pop-up window in the preset edge area of the current visible window.
[0011] According to a code editing method provided by the present invention, the current editing position is the current cursor position; the step of editing the code at the next editing position based on the predicted code includes: Jump the current cursor position to the next editing position, and change the code at the next editing position to the predicted code.
[0012] A code editing method provided by the present invention further includes: If, based on user instructions, it is determined that the user has not accepted the predicted code or the user has manually edited the code, the actual editing position and the actual edited code at the actual editing position are recorded until a first preset time is reached; The location prediction model and the code editing model are fine-tuned and optimized by using the actual edit locations and actual edit codes as the real labels in the training data.
[0013] According to a code editing method provided by the present invention, the step of inputting the code context information and unfiltered context information from the initial context information into a code editing model to obtain code prediction information of the target editing position output by the code editing model includes: The code context information, the unfiltered context information in the initial context information, and the code development document are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code development document includes project development document and / or application programming interface (API) design document.
[0014] According to a code editing method provided by the present invention, the step of inputting the initial context information into a relevance filtering model to obtain code context information related to the current editing needs output by the relevance filtering model includes: The initial context information and preset prompt words are input into the relevance filtering model to obtain the code context information related to the current editing needs output by the relevance filtering model. The preset prompts are used to indicate at least one of the following: screening requirements, relevance assessment dimensions, screening strategies, output format requirements, and special scenario processing requirements.
[0015] According to a code editing method provided by the present invention, the initial context information further includes current compiler error information, multiple call stack information within a preset range of the current editing position, and at least one of the currently open file sets; The code context information also includes call stack information filtered from the plurality of call stack information and / or files filtered from the file set.
[0016] According to a code editing method provided by the present invention, obtaining initial context information for indicating code editing needs includes: When a code editing requirement is triggered, the initial context information is obtained; The triggering methods for the code editing requirement include at least one of the following: The cursor stays at the end of the line of code for a duration greater than or equal to the second preset duration; The cursor is on a newline.
[0017] The present invention also provides a code editing device, comprising: The information acquisition module is used to acquire initial context information that indicates code editing needs; the initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; An information filtering module is used to input the initial context information into a relevance filtering model to obtain code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is built based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records filtered from the code change records corresponding to the multiple historical editing locations; The position prediction module is used to input the code context information and the unfiltered context information from the initial context information into the position prediction model to obtain the target editing position to be edited output by the position prediction model; the position prediction model is used to predict the editing position related to the current editing needs; The code prediction module is used to input the code context information and the unfiltered context information from the initial context information into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code editing model is used to predict the code at the target editing position. The code editing module is used to edit the code based on the code prediction information and the target editing position.
[0018] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code editing method as described above.
[0019] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the code editing method described above.
[0020] The code editing method, apparatus, storage medium, and program product provided by this invention acquire initial context information to indicate code editing needs. This initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location. This ensures that subsequent predictions of the target editing location and code prediction information can be accurately made, thereby improving the accuracy of code editing. The initial context information is input into a relevance filtering model to obtain code context information related to the current editing needs, output by the relevance filtering model. This allows for subsequent location prediction and code prediction based on more relevant and concise code context information, further improving code editing accuracy. The accuracy and efficiency of editing are improved by inputting unfiltered context information from the code context and initial context into the location prediction model. This yields the target editing location output by the model, eliminating the need for developers to manually determine the target editing location, thus improving code editing efficiency and ultimately enhancing the user experience. Furthermore, it eliminates the need for expert experience to manually confirm the target editing location, thereby improving the accuracy of code editing. The model also predicts the target editing location by inputting unfiltered context information from the code context and initial context into the code editing model. This eliminates the need for developers to manually write code, thus improving code editing efficiency and ultimately enhancing the user experience. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0022] Figure 1 This is one of the flowcharts illustrating the code editing method provided by this invention.
[0023] Figure 2 This is the second flowchart of the code editing method provided by the present invention.
[0024] Figure 3 This is one of the code diagrams provided by the present invention.
[0025] Figure 4 This is the second code illustration provided by the present invention.
[0026] Figure 5 This is the third code diagram provided by the present invention.
[0027] Figure 6 This is the fourth code diagram provided by the present invention.
[0028] Figure 7 This is a schematic diagram of the structure of the code editing device provided by the present invention. Detailed Implementation
[0029] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0030] Current code editing solutions mostly require users to manually determine the editing position, and then use a code completion model to complete the code at that position. For example, users need to manually move the cursor position before writing code. Although this code editing solution can improve the efficiency of developers to some extent compared to purely manual code writing, it still requires users to manually determine the editing position, so the code editing efficiency is still not high, thus affecting the user experience.
[0031] Given the low efficiency and poor user experience of current code editing solutions, this invention addresses this issue. The initial approach was to predict the editing location based on complete existing code, and then generate the corresponding target code using a code generation model. However, predicting the editing location based on complete existing code is inefficient due to the potentially large amount of existing code, thus resulting in low overall code editing efficiency.
[0032] To address the problems with the aforementioned approach, this invention continued its research. During the research process, the idea was to break down the complete existing code into individual code texts to be edited separately. Furthermore, considering the logical relationships often existing between different parts of code written in real-world development, the next editing position could be predicted using at least the previous historical code text and the current code text in a code modification scenario. However, this code splitting is necessary, which still impacts editing efficiency; and forcibly splitting the complete existing code may reduce the accuracy of the predicted editing position.
[0033] To address the shortcomings of the aforementioned code editing schemes involving code splitting, this invention further investigates and proposes a code editing method. This method does not require predicting the editing position based on complete existing code. Instead, it accurately predicts the target editing position based on initial context information, including the code context of the current editing position. This ensures the accuracy of the editing position prediction while improving the prediction efficiency, thereby enhancing code editing efficiency. Furthermore, the initial context information is input into a relevance filtering model to filter the initial context information, enabling prediction based on code context information relevant to the current editing requirements, further improving both code editing efficiency and accuracy.
[0034] The code editing method provided by the present invention will be described below through various embodiments. Figures 1-6 The code editing method of the present invention is described.
[0035] Figure 1 This is one of the flowcharts illustrating the code editing method provided by the present invention, such as... Figure 1 As shown, the code editing method includes the following steps 110, 120, 130, 140 and 150.
[0036] Step 110: Obtain initial context information to indicate code editing needs.
[0037] Here, code editing requirements refer to the intentions detected by developers when programming in an Integrated Development Environment (IDE) that may require code completion, code correction, or code modification.
[0038] The initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location.
[0039] Here, initial context information refers to the raw, multidimensional feature data collected from the current development environment before dimensionality reduction filtering, when code editing needs are detected.
[0040] Here, the historical editing position information indicates the historical editing position. The historical editing position is the position where the code was previously edited. Further, the historical editing position is the cursor position where the code was previously edited; that is, during code editing, the cursor is displayed so the user can know the code editing position. In other words, the historical editing position information refers to a record of the cursor trajectory of the user's previous operations, for example, obtaining the cursor position information of the user's previous 30 operations. In a specific embodiment, the historical editing position information may include, but is not limited to, at least one of the following: file path, line and column number, and dwell time, etc.
[0041] Furthermore, the duration of the editing time corresponding to multiple historical editing positions relative to the current time is shorter than the duration of the editing time corresponding to other historical editing positions relative to the current time. This means the editing positions that have been most recently edited are considered historical editing positions. This further improves the accuracy of editing position prediction and the accuracy of code prediction, thereby improving the accuracy of code editing. The number of these multiple historical editing positions can be set according to actual needs, for example, 30, representing the editing positions from the previous 30 code edits.
[0042] Here, the code change record corresponding to any historical editing location is used to record the code change information between the code before and after the editing at that historical editing location. For example, the code change record refers to the actual code modification history performed by the user at the historical editing location, which includes, for example, the code change content and timestamp.
[0043] Here, the current editing position is the current code editing position. If the current editing position is displayed by the cursor, then the current editing position is the current cursor position.
[0044] Here, the code context of the current editing position is the code content determined based on the current editing position. For example, the code context includes the code on the line where the current editing position is located, the code 10 lines above the current editing position, and the code 10 lines below the current editing position. Of course, the specific code included in this code context can be set according to actual needs, and is not specifically limited here. For example, the code context refers to the code text surrounding the current cursor position, such as the code text approximately 30 lines before and after the line where the current cursor position is located.
[0045] Furthermore, the initial context information also includes current compiler error information, multiple call stack information within a preset range of the current editing location, and at least one of the currently open file sets.
[0046] In one embodiment, the initial context information can be obtained because it is recorded locally or in the cloud. Alternatively, the code to be edited and its initial context information can be transmitted to other code editing devices to achieve shared code editing.
[0047] It should be understood that by comprehensively collecting user development behavior data and code environment status from multiple dimensions (including location trajectory, content changes, and current environment), a necessary and rich data foundation is provided for subsequent models to accurately understand the user's true programming intent and logical coherence, thereby improving the accuracy of editing location prediction and code prediction, and thus improving the accuracy of code editing.
[0048] Step 120: Input the initial context information into the relevance filtering model to obtain the code context information related to the current editing requirements output by the relevance filtering model.
[0049] The relevance filtering model is built upon a large model (such as a large language model). For example, it is a model that evaluates the relevance of various data points to the current development task and performs information filtering based on a specific filtering strategy and weighting algorithm, from a massive amount of initial contextual information.
[0050] The reason this invention uses a Large Language Model (LLM) as the initial foundational model is that, compared to traditional small models or pre-trained models, the LLM possesses richer prior knowledge and reasoning capabilities. Furthermore, compared to implicit information extraction and modeling, the LLM can explicitly output thinking, analysis, and reasoning steps, thereby enhancing the correctness of the final reasoning result at the semantic level. Based on this, the accuracy of code editing can be improved.
[0051] The code context information includes historical editing location information filtered from the plurality of historical editing location information, and code change records filtered from the code change records corresponding to the plurality of historical editing locations.
[0052] For example, the 10 most relevant historical cursor position information are selected from the first 30 historical cursor position information, and the 10 most relevant code change records are selected from the first 30 code change records.
[0053] Furthermore, the code context information also includes call stack information filtered from multiple call stack information within a preset range of the current editing location and / or files filtered from the currently open file set.
[0054] For example, the 10 most relevant call stack information (such as the 10 most relevant variable and function call stack information) can be filtered from the call stack information of the 30 lines before and after the current line, and the 5 most relevant code files can be filtered from all files opened by the user in the IDE.
[0055] It should be understood that, due to the extremely large amount of initial context information in real engineering projects, directly inputting all of it into the model would lead to excessive computation and easily introduce interference noise. By extracting the most relevant code context information through a large model-based filtering mechanism, accurate information denoising can be achieved. While retaining key historical patterns, irrelevant information is filtered out, thereby significantly improving the accuracy and response efficiency of subsequent location prediction models and code editing models. In other words, it improves the accuracy of editing location prediction and code prediction, and thus improves the accuracy of code editing.
[0056] Step 130: Input the code context information and the unfiltered context information in the initial context information into the location prediction model to obtain the target editing position to be edited output by the location prediction model.
[0057] Here, the unfiltered context information in the initial context refers to the immediate state information that is directly passed to subsequent models without being filtered. Specifically, it mainly includes the code context of the current editing position. Furthermore, it also includes current compiler error information, etc.
[0058] The location prediction model is used to predict the editing position related to the current editing needs. For example, the location prediction model refers to a deep learning model specifically designed to analyze the inherent logical relationships within code and the user's editing trajectory to output specific location coordinates (such as file paths and specific row and column numbers). Further, the location prediction model can be a cursor prediction model to predict the target cursor position, i.e., to display the target editing position based on the target cursor position. In one embodiment, the location prediction model can be obtained by training the model based on sample training data. In another embodiment, the location prediction model can be constructed based on a large language model.
[0059] Here, the target editing position is the next editing location to be edited. For example, it refers to the position calculated by the position prediction model that the user is highly likely to need to perform code editing or completion operations in the next step. This target position can be a single location or multiple locations ordered according to the content of the current code file (e.g., predicting the locations of the next 1-5 actions), enabling automatic prediction of the target editing position without requiring manual confirmation from the user. Furthermore, the target editing position is the cursor position, which can then be displayed so the user knows the location to be edited.
[0060] It's important to note that in real-world code development, there are often logical relationships between different code snippets written at different times. Therefore, previous edits can affect other parts of the code, allowing us to predict the next target editing location based on the code change log. In other words, the code editing process involves a series of strongly related editing operations, so previous edits will influence other parts of the code, thus predicting the next target editing location to be modified or completed. Furthermore, there may be errors in the code context; therefore, based on the code context or current compiler error information, we can predict the location of errors, and consequently, predict the next target editing location to be modified.
[0061] For example, a previous code change log indicates a modification to the code style, thus predicting target editing locations in other code that require a similar code style, allowing further modification of the code style at those target editing locations. Alternatively, a previous code change log indicates a modification to a variable name or variable type, thus predicting the target editing location as the location where the variable is used. Or, a previous code change log indicates a modification to a function name, thus predicting the target editing location as the location where the function is called. Or, a previous code change log indicates a modification to a function return type, thus predicting the target editing locations as the beginning and end of the code within the function implementation that needs modification and the location where the function is called. Or, a previous code change log indicates a modification to a function parameter, thus predicting the target editing locations as the beginning and end of the code within the function implementation that needs modification and the location where the function is called. Or, a previous code change log indicates the addition or deletion of a function parameter, thus predicting the target editing locations as the beginning and end of the code within the function implementation that needs modification and the location where the function is called. Alternatively, if the user's previous code change history indicated modifications to macro definitions or constants, the predicted target editing location is the position where the macro definition or constant is used. Alternatively, if the user's previous code change history indicated modifications to structure or enumeration members, the predicted target editing location is the position where the structure or enumeration is used. Alternatively, if the user's previous code change history indicated modifications to conditional statements in a conditional structure, the predicted target editing location is the first or last line of the code within the conditional structure's logical implementation that needs modification. Alternatively, if the user's previous code change history indicated modifications to conditional statements in a loop structure, the predicted target editing location is the first or last line of the code within the loop body that needs modification. Alternatively, if the user's previous code change history indicated the use of modules or functions not imported into this code file, the predicted target editing location is the position of the imported module content or function in the first few lines of this code file. Alternatively, if the user's previous code change history indicated the addition or deletion of a code block, the predicted target editing location is the first or last line of the code that needs modification. Alternatively, the user's previous code change history may indicate that a portion of the code block was extracted into a function implementation, thus predicting the target edit location as the beginning or end of the code that needs to be modified in the context.
[0062] For example, the target editing location can be determined as the first or last line of the error code if the following errors exist in the code context: misspelled keywords or reserved words, implicit data type conversion (incorrect assignment), missing or incorrect symbols, undefined or uninitialized variables, array out-of-bounds access, incorrect loop conditions leading to infinite loops, mismatched number of function arguments, logical errors (e.g., incorrect conditional statements), unreleased resources (e.g., files or database connections not closed), API call errors due to version inconsistencies, unhandled exceptions or errors (e.g., missing exception handling), regular expression errors, incorrect file paths, or files that do not exist.
[0063] In one embodiment, the number of target edit positions is 1, meaning only one edit position to be edited is predicted each time. In another embodiment, the number of target edit positions is multiple, meaning multiple edit positions to be edited can be predicted each time, enabling code editing at multiple edit positions with a single prediction.
[0064] It should be understood that this overcomes the limitation of traditional code completion, which can only passively generate code at the current cursor position. It can proactively predict the user's next action location based on logical connections within the context (e.g., modifying function parameters inevitably requires modifying the location where the function is called). This eliminates the need for developers to manually move the cursor between lines of code to find the point to be modified, greatly reducing the time spent navigating and locating code, thus improving both the efficiency and accuracy of code editing.
[0065] Step 140: Input the code context information and the unfiltered context information in the initial context information into the code editing model to obtain the code prediction information of the target editing position output by the code editing model.
[0066] The code editing model is used to predict the code at the target editing location. For example, the code editing model refers to a deep learning model capable of understanding code semantics and business logic and generating code change content. It should be noted that the input to the code editing model is the same as that of the location prediction model, but its output task is to generate specific code content at the predicted location. This code editing model can be trained based on sample training data or constructed based on a large language model.
[0067] Here, code prediction information can include not only completion operations for inserting new code at the target editing location, but also modification operations for deleting or replacing existing code at the target editing location. In other words, code editing includes both code modification and code completion; that is, code can be modified or completed at the target editing location.
[0068] In one specific embodiment, code prediction information refers to the specific code content or change information output for the target editing position, such as diff information.
[0069] It should be noted that, in real-world code development, there are often logical relationships between different parts of the code written. Therefore, previous edits to code can affect other parts of the code. Based on the code change history, we can predict the code for the same target editing location. Furthermore, since there may be erroneous code within the code context, we can predict the correct code (predicted code) corresponding to the erroneous code based on the code context.
[0070] Furthermore, to improve the accuracy of code prediction information and thus enhance code editing accuracy, the unfiltered context information in the initial context includes current compiler error information. Based on this, compiler error information can also assist in accurately identifying erroneous code and predicting correct code, thereby allowing erroneous code to be corrected.
[0071] It should be understood that the code editing model not only has the ability to continue writing code, but also the ability to modify existing code. When a user's existing code undergoes logical changes or contains errors, the code editing model can output accurate modification difference information for the target editing location. This avoids the problem of low code usability caused by continuing to generate code along with erroneous code in the traditional way, ensuring the integrity and consistency of code modifications, thus improving the accuracy of code editing.
[0072] Step 150: Based on the code prediction information and the target editing position, perform code editing.
[0073] Specifically, after determining the target editing location and obtaining the code prediction information corresponding to that location, the predicted code indicated by the code prediction information is edited at the target editing location. This predicted code is the modified code predicted for the target editing location.
[0074] The code editing method provided in this invention obtains initial context information indicating code editing needs. This initial context information includes multiple historical editing location information, code change records corresponding to the multiple historical editing locations indicated by the historical editing location information, and the code context of the current editing location. This ensures accurate prediction of the target editing location and code prediction information, thereby improving the accuracy of code editing. The initial context information is input into a relevance filtering model to obtain code context information related to the current editing needs, output by the relevance filtering model. This allows for subsequent location prediction and code prediction based on more relevant and concise code context information, further improving the accuracy of code editing. This improves both efficiency and effectiveness. By inputting unfiltered context information from the code context and initial context into the location prediction model, the model outputs the target editing location, eliminating the need for developers to manually determine this location. This enhances code editing efficiency and ultimately improves user experience. Furthermore, it eliminates the need for expert confirmation of the target editing location, thus increasing accuracy. The model also predicts the target editing location by inputting unfiltered context information from the code context and initial context into the code editing model, eliminating the need for developers to manually write code. This further improves code editing efficiency and ultimately enhances user experience.
[0075] Based on any of the above embodiments, a specific embodiment of the code editing method is given below. Figure 2 This is the second flowchart illustrating the code editing method provided by this invention, as shown below. Figure 2 As shown, step 150 above includes: Step 151: Determine the next editing position based on the target editing position.
[0076] In this embodiment of the invention, the number of target editing positions is one or more, that is, a set of one or more code positions that the user may edit. The next editing position refers to the specific code position that is decided to be prioritized and prepared for execution to the user at the current moment.
[0077] In one specific embodiment, when there are multiple target editing positions (for example, the position prediction model predicts the next 1-5 behavior positions), the next editing position to be processed can be determined sequentially according to the order of each target editing position in the current code file content.
[0078] Step 152: If the next editing position is within the current visible window, display the predicted code at the next editing position based on the code prediction information.
[0079] Here, the currently visible window is the interface that the current user can see. In one specific embodiment, the currently visible window refers to the source code area in the code editor of the integrated development environment that is directly presented to the user on the current screen or monitor and can be seen without scrolling up or down or switching files.
[0080] Here, the predicted code is determined based on code prediction information; it represents the changed code at the predicted next edit position. The display method of this predicted code can be set according to actual needs. For example, it can be displayed as a pop-up window, in grayscale, or in a different display format than the current code, so that users can distinguish between the original code and the predicted code, thereby improving the user experience.
[0081] It should be understood that when the point to be modified is located near the user's current visual focus, the in-place display method is adopted so that the user can directly examine the correctness of the prediction code in conjunction with the context, thus providing the user with intuitive visual feedback.
[0082] Step 153: If the next editing position is not inside the current visible window, display the predicted code in the preset edge area of the current visible window based on the code prediction information.
[0083] Here, the preset edge area may include, but is not limited to, the bottom, top, left or right side of the currently visible window, etc.
[0084] For example, if the next editing location exceeds the user's current field of view (e.g., spanning dozens of lines of code, or located in another file), forcibly jumping the cursor and screen there would interrupt the user's current train of thought. Therefore, embodiments of the present invention adopt a strategy of displaying the predicted code in a preset edge area of the currently visible window.
[0085] It should be understood that this significantly optimizes the development experience for cross-screen / cross-file collaboration. When there is a need for collaborative code modification at a distance (such as modifying a function signature and needing to simultaneously modify the calling part of a file at a distance), the edge area display mechanism serves both as a warning of changes and as a way to protect the user's current reading context. This allows users to safely and calmly review upcoming code changes at a distance without losing focus on the current window, thereby improving the user experience and ensuring the accuracy of code editing.
[0086] Step 154: If it is determined from the user's instruction that the user accepts the predicted code, then the code is edited at the next editing position based on the predicted code.
[0087] Here, the triggering method for user commands can be set according to actual needs, such as the user pressing an confirmation key on the keyboard (e.g., the TAB key) or clicking to confirm with the mouse. For example, a user command refers to a confirmation operation triggered by the developer using an input device such as a keyboard or mouse, for example, pressing the TAB key. Once a user command indicating acceptance is received, not only will the predicted code (the completed or modified code) be actually applied to the source code document, but the user's cursor will also be automatically moved to the next editing position.
[0088] Specifically, the code at the next editing position is edited based on predicted code. This code editing includes code modification and code completion, meaning that code modifications (such as code correction) and code completion can be performed at the next editing position.
[0089] It should be understood that the embodiments of the present invention realize one-click confirmation and application, eliminating the tedious process of manually scrolling the page, manually positioning the cursor, manually deleting old code, and manually typing new code. It compresses the complex cross-line editing action into a simple key confirmation, which greatly improves coding efficiency, that is, improves code editing efficiency.
[0090] The code editing method provided in this invention, through the aforementioned approach and by using the current visible window as a judgment dimension, achieves differentiated visual window display and effectively eliminates the context loss and visual disorientation problems caused by large-step code jumps. This gives developers complete control over automated code editing, making the complex code modification process safe, transparent, and highly ergonomic, thereby improving user experience while ensuring the accuracy of code editing. Furthermore, displaying predicted code for user confirmation of whether to agree to editing avoids code modifications even when the predicted code does not meet user needs, thus ensuring that the modified code meets the user's code editing requirements, thereby improving the accuracy of code editing and enhancing user experience.
[0091] Based on any of the above embodiments, in this method, the number of target editing locations is one or more; step 151 includes: Determine the next editing position based on the order of the target editing positions in the current code file.
[0092] It should be noted that the target editing location refers to the set of code coordinates output by the location prediction model that the user is highly likely to need to perform editing operations later. In real-world complex code modification scenarios, a change in one piece of code often triggers related changes in multiple places within the same code file; therefore, this number is usually one or more.
[0093] Here, the order in the current code file refers to the physical arrangement of the target editing positions in the source code text from top to bottom (i.e., line numbers from smallest to largest). Based on this, the target editing position that is listed earlier and has not yet been edited is prioritized as the next editing position.
[0094] For example, the location prediction model might predict the next 1-5 action locations in the current code file. These target edit locations are then extracted and strictly sorted according to their line numbers in the current code file. The first target edit location in the sorted order is determined as the first next edit location, and so on, until the next edit location is determined.
[0095] It should be understood that since code files are essentially linear text written line by line, determining the next editing position according to the order of the file content highly aligns with developers' natural top-down reading and review habits. This orderly scheduling avoids the cursor jumping back and forth haphazardly in the file, greatly reducing visual fatigue and cognitive burden for developers when continuously modifying code, thereby improving the user experience.
[0096] Accordingly, after step 154 above, the method further includes: If there are still unedited editing positions among the target editing positions, return to the step of determining the next editing position according to the order of the target editing positions in the current code file.
[0097] Here, "edit location that has not been edited" refers to the target edit location among the predicted multiple target edit locations that has not yet been shown to the user or has not yet been confirmed by the user for execution.
[0098] It should be noted that this embodiment of the invention constructs an automated loop determination mechanism. Once the editing action for the next editing position is completed (e.g., the user accepts and applies the code), it automatically checks whether there are any target editing positions waiting to be processed in the queue.
[0099] For example, when a user accepts the predicted code (e.g., the cursor jumps to the predicted position and the modified code is applied), the system automatically determines whether there is content for the next cursor prediction and editing completion. If so, it automatically enters the next loop, triggering the display of code prediction for the next sorting position; otherwise, it ends the current batch editing process and returns to the initial state of waiting for new code editing requests.
[0100] Furthermore, if there are no unedited editing locations among the target editing locations, the step of obtaining initial context information to indicate code editing needs is returned for the next code editing.
[0101] It should be understood that this achieves automated, continuous workflow for multi-step code modifications. In scenarios requiring multiple strongly related edits (such as macro definition modifications leading to adaptation at multiple usage points or batch modifications of the same object's properties), users only need to input consecutive confirmation commands to automatically move the cursor sequentially to each point to be modified and provide the modification solution. This eliminates the tedious operation of manually searching for the next point after each modification, ensuring task continuity.
[0102] The code editing method provided in this invention, through the above-described manner, sorts each target editing position according to its order in the current code file, and triggers the next step upon completion, thus guiding developers in complex refactoring or debugging scenarios to process all logically related code modification points one by one, in an orderly and comprehensive manner, just like completing a checklist. This not only maximizes the efficiency of continuous writing during the code development phase, thereby improving code editing efficiency, but also significantly reduces errors caused by human negligence in missing updates of some related positions, thereby ensuring the integrity and consistency of code changes and improving the accuracy of code editing.
[0103] Based on any of the above embodiments, in this method, displaying the predicted code at the next editing position based on the code prediction information includes: If the type of editing behavior at the next editing position is determined to include code modification based on the code prediction information, the predicted code is displayed in a pop-up window at the next editing position. If the type of editing behavior at the next editing position, determined based on the code prediction information, does not include code modification but includes code completion, the predicted code is displayed in grayscale at the next editing position.
[0104] Here, the edit behavior type indicates the specific operation category to be taken on the original code at the next editing location. That is, the edit behavior type indicates the specific code editing behavior, which may include, but is not limited to: code modification (code optimization) and code completion, etc.
[0105] Here, code modification refers to code prediction information that includes operations to change, replace, or delete existing code; for example, replacing existing variable names with new ones, or changing the existing function parameter list. Code completion refers to code prediction information that only includes pure new code insertion operations, without involving the deletion or replacement of any existing code text; for example, continuing to generate subsequent logic code at the cursor's location or the end of the line.
[0106] Here, the pop-up method refers to displaying a separate graphical user interface control floating near the current line of code in the integrated development environment. In one embodiment, this control typically covers part of the background code to explicitly display a comparison of the code before and after the modification. For example, when the parsed diff information (code prediction information) shows the deletion of old code and the insertion of new code, it is determined that a code modification operation is involved. A comparison window will pop up directly above or below the next editing position, displaying the original code with a red strikethrough on the left or above, and the predicted modified code with a green highlight on the right or below.
[0107] For example, such as Figure 3 As shown, the existing code at the target editing location is "if(jCheckBoxShowAuthor.isselected())jTextFieldAuthor", and the predicted code is "if(jCheckBoxShowAuthor.isselected())jTextFieldAuthor.requestFocusInwindow();". Figure 4 As shown, the existing code for the target editing position is "jComboBoxCiteStyle.setenabled(isPreamblevisible);", and the modified predicted code is "jComboBoxCiteStyle.setEnabled(isPreambleVisible);".
[0108] Here, grayscale mode refers to appending the predicted code to the next editing position or the next line in a code editor in a color different from the normal code (usually light gray, semi-transparent, or low-contrast). For example, when parsing the diff information (predicted code information) reveals no deletion operations and only a completion operation, the predicted completed code will quietly appear behind the cursor like ghost text to avoid interrupting the user's normal thought process. The user can then simply press a specific key (such as the TAB key) to convert it into actual code.
[0109] For example, such as Figure 5 and Figure 6 As shown, the predicted code is displayed in gray at the target editing location. Among them, Figure 5 The code highlighted in red is the prediction code. Figure 6 The code highlighted in red is the prediction code.
[0110] It should be understood that code modification involves overriding or disrupting the developer's original logic, which is a high-risk operation. Using pop-up windows provides a strong visual barrier and prompt. This forces developers to focus their attention on the upcoming changes, allowing them to intuitively compare the differences before and after the modification. This avoids accidental alteration of the developer's hard-earned correct code due to model misjudgment, thereby improving the accuracy of code editing.
[0111] It should be understood that since simple code completion does not disrupt existing code, it is a low-risk, sequential input aid. Using grayscale displays minimizes visual interference for users. This lightweight, non-intrusive display method allows developers to smoothly preview recommended content while reading and typing normally, maintaining the fluidity and immersion of the coding process, thereby improving the user experience.
[0112] The code editing method provided in this embodiment of the invention displays code modifications in a pop-up window and code completion in grayscale through the above-described manner, thereby ensuring that users can understand the type of editing behavior through the display method, thus displaying it more intuitively, improving the accuracy of code editing, and ultimately improving the user experience.
[0113] Based on any of the above embodiments, in this method, displaying the predicted code in a preset edge region of the current visible window based on the code prediction information includes: Based on the code prediction information, the original code and predicted code at the next editing position are displayed in a pop-up window in the preset edge area of the current visible window.
[0114] Here, the preset edge region refers to a pre-defined outer area within the visible window of the current code editor in the integrated development environment (IDE) that does not interfere with the user's current reading focus. Furthermore, this preset edge region is typically the bottom edge of the current visible window.
[0115] Here, the pop-up method refers to rendering a graphical user interface window with independent layout format on top of the original interface.
[0116] Here, the original code refers to the existing, unmodified code that already exists at the next editing position. Since it's not within the current visible window, it might be hundreds of lines away from the current cursor or located in a completely different file, thus requiring the original code to be displayed. Predicted code, on the other hand, refers to the specific modified, replaced, or completed code output by the code editing model for the next editing position; it is determined based on code prediction information.
[0117] For example, when it is determined that the next edit position of the prediction is not within the current screen, a Diff (difference comparison) view window will pop up at the bottom of the visible window. The pop-up window will display the original code and the predicted code recommended by the model at the far position side by side or in the same row above and below, and highlight the modified parts (e.g., red indicates deleted original code, and green indicates newly added predicted code).
[0118] It should be understood that since the next editing location is not within the current visible window, developers cannot determine the rationality of changes by only viewing the predicted code. By simultaneously displaying the original code and the predicted code, the remote context is brought to the user's view in a diff format, allowing the user to review the code change logic without actually jumping to that location. Furthermore, placing the pop-up in a preset edge area (such as the bottom) serves both as a prominent prompt and strictly avoids obscuring the core code area the user is currently reading or writing. This allows developers to calmly decide whether to process the logic within the current visible window first or immediately confirm the remote changes in the edge pop-up, thus protecting the developer's visual focus and mental continuity, thereby improving the user experience.
[0119] The code editing method provided in this invention, through the design of edge pop-up windows and before-and-after code comparison, perfectly solves the core engineering pain point of how to safely confirm remote code changes without interrupting the developer's current context. This WYSIWYG non-intrusive cross-screen collaborative interaction greatly reduces the cognitive burden caused by developers frequently switching navigation between different files or code blocks, ensuring the accuracy of code modifications and improving overall development efficiency, that is, improving the accuracy and efficiency of code editing.
[0120] Based on any of the above embodiments, in this method, the current editing position is the current cursor position; the step of editing the code at the next editing position based on the predicted code includes: Jump the current cursor position to the next editing position, and change the code at the next editing position to the predicted code.
[0121] It's important to note that the current editing position refers to the current cursor position where the developer is writing or viewing code, which is represented by the blinking insertion point coordinates in the integrated development environment (IDE) editor. Cursor position jump, on the other hand, refers to the automatic navigation behavior of the IDE editor interface, which automatically moves the input focus and cursor insertion point from the current coordinate system and repositions them to the pre-predicted next editing position.
[0122] Specifically, it automatically performs text replacement or insertion operations, replacing the original code content (if any) at the next editing position with the predicted code generated by the model, or inserting the predicted completion code content at the next editing position.
[0123] For example, when the user presses the accept command (such as the TAB key), this combined action is immediately triggered: the editor screen may scroll to ensure the new cursor position is in the visible center, while simultaneously applying the diff to the code. If the next editing location is in another file, that file will be automatically opened, and the cursor will be positioned and the code written.
[0124] It should be understood that traditional code assistance tools usually require users to manually click to jump or manually trigger application prediction code. However, the embodiments of the present invention merge displacement and modification into a single logical unit, eliminating the need for users to manually address after accepting suggestions, thereby improving the efficiency of code editing and enhancing the user experience.
[0125] The code editing method provided in this invention strongly couples the cursor prediction trajectory with the code editing result at the physical level, changing the traditional two-stage programming habit of first locating and then modifying. Through the automatic synchronization mechanism of jump + change, it greatly shortens the time for developers to locate and type in the code library, thereby improving the efficiency of code editing and ultimately improving the user experience.
[0126] Based on any of the above embodiments, the method further includes: If, based on user instructions, it is determined that the user has not accepted the predicted code or the user has manually edited the code, the actual editing position and the actual edited code at the actual editing position are recorded until a first preset time is reached; The location prediction model and the code editing model are fine-tuned and optimized by using the actual edit locations and actual edit codes as the real labels in the training data.
[0127] Here, "actual editing location" refers to the position where the user's cursor actually rests and code changes occur during the operation cycle in which the user rejects the predicted code. "Actual edited code" refers to the actual source code content that the user manually writes, modifies, or completes during this operation cycle.
[0128] Here, the first preset duration can be set according to actual needs, such as 5 minutes; it refers to the observation window period set to capture the user's true intentions.
[0129] For example, a user can expressly refuse the code prediction by clicking the close button or pressing the Esc key, or implicitly ignore it by continuing to type characters in the same place instead of pressing the accept shortcut. This indicates that the user has not adopted the recommended predicted code but has instead entered, deleted, or modified the code text themselves.
[0130] Here, "true label" refers to the reference standard representing the correct result in supervised learning of machine learning. Fine-tuning optimization refers to training the pre-trained cursor prediction model and code editing model using newly collected training data with true labels, and adjusting the model's internal parameters.
[0131] Specifically, the initial context information that triggers this prediction is used as input, and the actual edit location and the actual edit code are used as the expected output targets of the location prediction model and the code editing model, respectively, to calculate the loss function and update the gradient.
[0132] It should be understood that when a user rejects a recommendation, it indicates a bias in the model's prediction. By recording the user's actual behavior over a subsequent period, the correct editing logic and location within that specific code context can be captured. This provides the most direct and realistic reference sample for correcting model errors, thereby improving the accuracy of code editing. Furthermore, it automatically transforms the user's natural programming behavior into labeled training data, eliminating the need for manual evaluation and labeling by dedicated personnel, significantly improving the efficiency and scale of data acquisition and reducing model optimization costs.
[0133] It should be understood that by feeding users' actual programming behavior back to the model, self-evolution and personalized adaptation are achieved. The finely tuned and optimized model can learn the user's specific coding style, the business logic of a particular project, and boundary cases previously missed by the model. As negative feedback data accumulates, the accuracy of the location prediction model and the code editing model will continue to improve, making the subsequently generated recommendations more aligned with the developer's true intentions, thereby improving the accuracy of code editing.
[0134] The code editing method provided in this invention defines the operation trajectory after a user rejects a recommendation as the training gold standard, which can continuously correct the deviation of the editing location prediction and the error of code generation. This negative feedback-driven mechanism based on real development scenarios enables the location prediction model and the code editing model to become more and more accurate in matching the programming habits of different developers and the specific constraints of different engineering projects as the usage time increases. It fundamentally solves the technical problem that the generalization ability of the general code model is insufficient in specific private or complex business scenarios, thereby continuously improving the accuracy of code editing.
[0135] Based on any of the above embodiments, in this method, step 140 includes: The code context information, the unfiltered context information in the initial context information, and the code development document are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model.
[0136] The code development documentation includes project development documentation and / or application programming interface (API) design documentation.
[0137] Here, code development documentation refers to descriptive documents, not in source code form, stored in a code repository or project management system, used to guide software development. Project development documentation refers to documents that record the overall project architecture design, inter-module dependencies, project-specific programming standards (such as specific naming conventions and exception handling strategies), and business logic descriptions. API design documentation refers to specifications that detail the calling methods, parameter types and numbers, return value structures, error code definitions, and preconditions for calling specific functions, methods, or interfaces (such as requiring initialization before calling).
[0138] For example, when it is detected that a user is calling a specific business interface, the API design document fragment corresponding to that interface will be automatically retrieved and extracted, and sent to the code editing model along with the code context.
[0139] For example, an API design document might specify that "Database.initialize() must be called before calling Database.query()". When the code editing model is aware of this document constraint, it will automatically include initialization check logic when generating code prediction information, thereby avoiding runtime pitfalls caused by violating API calling specifications.
[0140] Furthermore, a unified coding style and naming convention are implemented. For example, if the project development documentation defines that utility functions must be prefixed with "util" and components must be named using PascalCase, the code editing model will follow these conventions to generate predictive code such as "utilFormatCurrency" that meets the specific requirements of the project, ensuring a high degree of consistency in the codebase style.
[0141] It should be understood that plain code text often fails to reflect complex business rules and project-specific design intent. By introducing project development documentation, the code editing model can understand the deep logical connections between code modules, thereby generating code prediction information that better aligns with business logic and ultimately improving the accuracy of code editing.
[0142] The code editing method provided in this invention integrates external code development documents across modalities within the code editing model. Instead of simply performing pattern matching based on existing code, the model can, like a human developer, write and modify code in compliance with regulations based on an understanding of project development documents and API design documents. This significantly improves the compliance, accuracy, and maintainability of the generated predictive code in a real engineering environment, reducing subsequent rework costs due to violations of business logic or interface specifications, thus improving the accuracy of code editing.
[0143] Based on any of the above embodiments, in this method, step 120 includes: The initial context information and preset prompts are input into the relevance filtering model to obtain the code context information related to the current editing requirements output by the relevance filtering model.
[0144] The preset prompts are used to indicate at least one of the following: screening requirements, relevance assessment dimensions, screening strategies, output format requirements, and special scenario processing requirements.
[0145] Here, preset prompts refer to pre-constructed instruction text used to guide the relevance screening model in performing specific logical tasks. The relevance screening model performs semantic parsing on the preset prompts, using them as constraints or evaluation criteria to weight and filter massive amounts of initial contextual information.
[0146] Here, the filtering requirements are used to specify the boundary conditions for filtering. For example, it may require the model to retain only member variable declarations directly related to the current error class name, or to filter out all third-party library implementation code and retain only the business logic layer code.
[0147] Here, the relevance assessment dimension is used to define the metrics for relevance. For example, it indicates whether the model evaluates whether a piece of code is relevant to the current editing requirements from the dimensions of data flow dependencies, control flow logic, or class inheritance relationships.
[0148] Here, the filtering strategy is used to specify the execution mode of the filtering. For example, the Top-K strategy (only retaining the K segments with the highest relevance scores) or the threshold strategy (only retaining segments with a matching score higher than 0.8).
[0149] Here, the output format requirements are used to standardize how the filtering results are presented. For example, the output should be in a structured JSON format with code snippets, file paths, and reasons for relevance, so that the subsequent code editing model can parse it more efficiently.
[0150] Here, special scenario handling requirements refer to instructions specific to a particular programming environment. For example, when handling multi-threaded deadlock repair needs, the model is required to prioritize filtering contexts involving the synchronized keyword, Lock objects, and thread state transitions.
[0151] For example, the preset prompt words are as follows: "Please select the most relevant code information based on the multidimensional development context to assist the current programming task;" Input information: 1. Cursor history: The user's last 30 cursor positions (including file path, line and column number, and dwell time); 2. Code change history: The last 30 code modifications (including change details, timestamps, and file paths); 3. Current code context: The code surrounding the cursor position (approximately ±30 lines); 4. Compiler error messages: Error / warning messages encountered so far; 5. Call stack context: Call relationship information for the 30 lines before and after the current line; 6. IDE Status: A list of all open files and a summary of their contents; Screening criteria: Cursor history → Select the 10 most relevant historical positions; Code changes → Select the 10 most relevant modification records; Call stack → Select the 10 most relevant variable / function call details; Open the file → Select the 5 files most relevant to the current file; Relevance assessment dimensions: 1. Semantic similarity: The degree of matching between code concepts, naming patterns, and functional intent; 2. Time proximity: Prioritize recent operations, but retain key historical patterns; 3. Structural relationships: Elements within the same module, class hierarchy, or call chain; 4. Error relevance: The context directly related to the current compilation error; 5. Task continuity: Operations within the same development task sequence; Specific screening strategies: A. Cursor position filtering algorithm: Weight calculation: Same file / class: +40% relevance; Within the same method / function: +30% correlation; Recent actions (last 5): +20% correlation; Semantically relevant regions (such as operations on the same object): +25% relevance; Location related to the current error: +50% relevance; Exclude: Instantaneous cursors with a dwell time of less than 1 second; B. Code Change Screening Algorithm: Priority order: 1. Fix the current error change history (highest priority); 2. Most recently modified (in reverse chronological order) files of the same type; 3. Modification history of related APIs / methods; 4. Architectural adjustments for identical modules; 5. Areas that users repeatedly modify (display mode changes); C. Call stack filtering algorithm: Selection criteria: 1. Directly call the function of the current method (the caller); 2. The key function being called in the current method call (the callee); 3. Variables that transmit important data streams; 4. Calls along the exception / error propagation path; 5. The function chain related to the semantics of the current cursor; D. Related document filtering algorithm: Relevance score: 1. Import / Reference Relationships: Files directly imported receive a +60% bonus; 2. Same project / package structure: +40%; 3. Recent co-editing (within 30 minutes): +35%; 4. Similar code patterns (class / interface implementations): +30%; 5. File pairs frequently switched by users: +25%; Output format requirements: json { "relevant_cursor_positions": [ { "file": "path / to / file.java", "line": 45, "column": 12, "relevance_score": 0.92, "reason": "Handling the same type of exception within the same method" } ], "relevant_code_changes": [ { "timestamp": "2024-01-15T10:30:00", "file": "path / to / file.java", "change_summary": "Fixed null pointer exception", "relevance_score": 0.95 } ], "relevant_call_stack_items": [ { "element": "UserService.validate()", "type": "function", "context": "The main entry point for calling the current method", "relevance_score": 0.88 } ], "relevant_open_files": [ { "file": "path / to / related.py", "relevance_score": 0.85, "relation_type": "Interface implementation" } ] } Special scene handling: 1. New documents / features: Emphasis on similar design patterns and project conventions; 2. Debug mode: Prioritizes displaying error-related context and recent modifications; 3. Refactoring process: Focus on structural changes and dependencies; 4. Learning phase: Retain more examples and pattern matching; Optimization tips: Use vector similarity to calculate semantic relevance; Establish a graph showing the relationships between code elements and perform graph traversal; Consider the developer's personalized patterns (such as specific naming habits); Real-time updates: Relevance weights are reassessed after each user action.
[0152] It should be understood that by using preset prompts, the focus of the filtering can be dynamically adjusted according to the current editing task type, avoiding the randomness that exists in the filtering of traditional models, thereby improving the accuracy of code editing; and clear filtering requirements can effectively eliminate noisy data that is semantically similar but does not substantially help solve the current problem, ensuring that the information input into the subsequent code editing model has high gain value.
[0153] The code editing method provided in this invention, through preset prompt word definition, filtering requirements, relevance evaluation dimensions, and filtering strategies, can compress a large amount of initial context information into highly representative key fragments, significantly reducing the computational load of the subsequent code editing model and thus improving the efficiency of code editing. Furthermore, because the preset prompt words cover special scenario processing requirements, when facing highly complex programming tasks, targeted guidance can uncover contextually strong logical coupling relationships hidden deep within the project, thereby supporting the code editing model in generating higher-quality, more practical modification suggestions, thus improving the accuracy of code editing. Moreover, through constraints on output format requirements, the filtering results possess good structured characteristics, which not only facilitates data flow between models but also makes it easier to debug and trace the filtering logic during development, thereby improving the accuracy and efficiency of code editing.
[0154] Based on any of the above embodiments, in this method, the initial context information further includes current compiler error information, multiple call stack information within a preset range of the current editing position, and at least one of the currently open file sets; the code context information further includes call stack information filtered from the multiple call stack information and / or files filtered from the file set.
[0155] Here, the current compiler error message refers to the exception message thrown by the compiler or static code analysis tool during the code writing or compilation build phase. Examples include syntax errors, undefined variables, type mismatches, or incompatible method signatures. These messages typically include the error code, error description, and the specific line number where the error occurred.
[0156] It should be understood that introducing the current compiler error information as the initial context allows us to know not only what the code is, but also where the code goes wrong, thereby guiding the model to directly generate fix code based on the cause of the error.
[0157] Here, the preset range can be set according to actual needs, such as 30 lines before and after the current editing position. For example, it refers to the upstream and downstream call hierarchy of the function or class where the current cursor is located (such as tracing up 3 levels of callers and down 3 levels of callees).
[0158] Here, the call stack information records in detail the execution path under which the current code segment was triggered, as well as the execution sequence of each stage of the runtime. Specifically, the call stack information refers to the detailed definition and implementation code of all variables and functions in the relevant lines.
[0159] It should be understood that traditional static code snippets are difficult to reflect the runtime call order. Introducing multiple call stack information enables the model to understand the current editing position in the entire program lifecycle and the data flow, effectively solving the problem of logical discontinuity across function calls.
[0160] Here, the currently open file set refers to the collection of all source code files or configuration files that the developer has currently opened and are in the active tab of in the integrated development environment or code editor. This collection typically highly reflects the developer's current working context and focus.
[0161] It should be understood that the currently opened set of files is often a strongly related set of files that developers are collaboratively modifying. Including it in the initial context can capture the business intent of cross-file modification with very low overhead.
[0162] For example, suppose a developer is editing a class named OrderService and encounters a compiler error causing a NullPointerException. The initial context information captures a call stack up to 20 levels deep and includes 10 files open in the IDE. After processing by a relevance filtering model, the filtered code context information only includes the direct upstream call stack that triggered the NullPointerException, and the data transfer object files in the currently open file set directly related to the error. This precisely refined information is then fed into the code editing model. The compiler error information is not filtered.
[0163] It should be understood that, since the complete call stack can be extremely long and the set of opened files may contain irrelevant files, the filtering mechanism, while utilizing rich diagnostic information, strictly controls the amount of input finally fed into the code editing model, avoiding model attention distraction caused by context overload, thereby improving the efficiency and accuracy of code editing.
[0164] The code editing method provided in this embodiment of the invention provides more contextual information through the above-described manner, thereby improving the accuracy of code editing. It also filters the newly provided call stack information and files, thereby improving the efficiency of code editing.
[0165] Based on any of the above embodiments, in this method, step 110 includes: When a code editing requirement is triggered, the initial context information is obtained; The code editing requirement can be triggered in at least one of the following ways: the cursor remains at the end of the code line for a duration greater than or equal to a second preset duration; or the cursor moves to a new line. Alternatively, the code editing requirement can be triggered in other ways, such as clicking the automatic code editing option.
[0166] Here, the second preset duration can be set according to actual needs, for example, 1 second.
[0167] The code editing method provided in this embodiment of the invention obtains initial context information when a code editing requirement is triggered. That is, the code editing method is only executed when a code editing requirement is triggered, rather than being executed continuously. This avoids executing the method when the user has no need for automatic code editing, thus avoiding affecting the user experience and avoiding the waste of computing resources by continuously executing the method when there is no need.
[0168] Based on any of the above embodiments, another embodiment of the code editing method is given below. After performing code editing based on the code prediction information and the target editing position, the method further includes: Return to the step of obtaining the initial context information used to indicate code editing needs for the next code edit.
[0169] Specifically, after modifying existing code at the target editing location, the system automatically returns to the initial step for the next code edit, thus automatically triggering the prediction and code editing of the next target editing location. In other words, if the user accepts the predicted code, the predicted code is applied to the current code, and the next code edit is automatically triggered.
[0170] The code editing method provided in this embodiment of the invention modifies the existing code at the target editing location and then returns to the initial step to perform the next code editing, thereby automatically performing multiple code editing operations, i.e., automatically performing multiple code editing operations, and can automatically edit to obtain the final code without much intervention from the developer, thereby further improving the efficiency and intelligence level of code editing.
[0171] Based on the above embodiments, the present invention can help users improve efficiency in the code development stage. Specifically, by predicting edit locations, modifying code, and completing code, it offers greater flexibility and significantly improves code editing efficiency compared to simple code completion that only adds new code.
[0172] The code editing apparatus provided by the present invention will be described below. The code editing apparatus described below can be referred to in correspondence with the code editing method described above.
[0173] Figure 7 This is a schematic diagram of the structure of the code editing device provided by the present invention, as shown below. Figure 7 As shown, the code editing device includes: an information acquisition module 710, an information filtering module 720, a location prediction module 730, a code prediction module 740, and a code editing module 750.
[0174] The information acquisition module 710 is used to acquire initial context information for indicating code editing needs; the initial context information includes multiple historical editing position information, code change records corresponding to multiple historical editing positions indicated by the multiple historical editing position information, and the code context of the current editing position; The information filtering module 720 is used to input the initial context information into the relevance filtering model to obtain the code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is built based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records filtered from the code change records corresponding to the multiple historical editing locations; The position prediction module 730 is used to input the code context information and the unfiltered context information in the initial context information into the position prediction model to obtain the target editing position to be edited output by the position prediction model; the position prediction model is used to predict the editing position related to the current editing needs. The code prediction module 740 is used to input the code context information and the unfiltered context information in the initial context information into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code editing model is used to predict the code at the target editing position. The code editing module 750 is used to edit code based on the code prediction information and the target editing position.
[0175] This invention also provides an electronic device, which may include: a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus. The processor can invoke logical instructions in the memory to execute a code editing method, which includes: acquiring initial context information indicating code editing needs; the initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; inputting the initial context information into a relevance filtering model to obtain code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is constructed based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records from the multiple historical editing location information. The code change records corresponding to the historical editing positions are selected; the code context information and the unselected context information in the initial context information are input into the position prediction model to obtain the target editing position to be edited output by the position prediction model; the position prediction model is used to predict the editing position related to the current editing needs; the code context information and the unselected context information in the initial context information are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code editing model is used to predict the code at the target editing position; based on the code prediction information and the target editing position, code editing is performed.
[0176] Furthermore, the logical instructions in the aforementioned memory can be implemented as software functional units and sold or used as independent products, and can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0177] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the code editing method provided by the above methods. The method includes: obtaining initial context information for indicating code editing needs; the initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; inputting the initial context information into a relevance filtering model to obtain code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is constructed based on a large model; the code context information... This includes historical editing location information filtered from the plurality of historical editing location information, and code change records filtered from the code change records corresponding to the plurality of historical editing locations; inputting the code context information and unfiltered context information from the initial context information into a location prediction model to obtain the target editing location to be edited output by the location prediction model; the location prediction model is used to predict the editing location related to the current editing needs; inputting the code context information and unfiltered context information from the initial context information into a code editing model to obtain code prediction information for the target editing location output by the code editing model; the code editing model is used to predict the code for the target editing location; and performing code editing based on the code prediction information and the target editing location.
[0178] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, is implemented to perform the code editing methods provided by the methods described above. The method includes: acquiring initial context information for indicating code editing needs; the initial context information including multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; inputting the initial context information into a relevance filtering model to obtain code context information related to the current editing needs output by the relevance filtering model; the relevance filtering model is constructed based on a large model; the code context information includes information from the multiple historical editing locations... The code editor selects historical editing location information from the information and code change records from the code change records corresponding to the multiple historical editing locations; inputs the code context information and the unselected context information from the initial context information into the location prediction model to obtain the target editing location to be edited output by the location prediction model; the location prediction model is used to predict the editing location related to the current editing needs; inputs the code context information and the unselected context information from the initial context information into the code editing model to obtain the code prediction information of the target editing location output by the code editing model; the code editing model is used to predict the code at the target editing location; and performs code editing based on the code prediction information and the target editing location.
[0179] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0180] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0181] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code editing method characterized by, include: Obtain initial context information to indicate code editing needs; The initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; The initial context information is input into the relevance filtering model to obtain the code context information related to the current editing requirements output by the relevance filtering model. The relevance filtering model is built based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records filtered from the code change records corresponding to the multiple historical editing locations; The code context information and the unfiltered context information in the initial context information are input into the location prediction model to obtain the target editing position to be edited output by the location prediction model; The location prediction model is used to predict the editing location related to the current editing needs; The code context information and the unfiltered context information in the initial context information are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; The code editing model is used to predict the code at the target editing location; Based on the code prediction information and the target editing location, code editing is performed.
2. The code editing method according to claim 1, characterized by, The code editing based on the code prediction information and the target editing position includes: Based on the target editing position, determine the next editing position; If the next editing position is within the current view window, based on the code prediction information, the predicted code is displayed at the next editing position; If the next editing position is not within the current visible window, the predicted code is displayed in a preset edge area of the current visible window based on the code prediction information; If the user accepts the predicted code based on the user's instructions, the code is edited at the next editing position based on the predicted code.
3. The code editing method according to claim 2, characterized by, The number of the target editing locations is one or more; Determining the next editing position based on the target editing position includes: Determine the next editing position according to the order of the target editing positions in the current code file; After determining that the user accepts the predicted code based on the user instruction, and then editing the code at the next editing position based on the predicted code, the method further includes: If there are still unedited editing positions among the target editing positions, return to the step of determining the next editing position according to the order of the target editing positions in the current code file.
4. The code editing method according to claim 2, characterized by, The step of displaying the predicted code at the next editing position based on the code prediction information includes: If the type of editing behavior at the next editing position is determined to include code modification based on the code prediction information, the predicted code is displayed in a pop-up window at the next editing position. If the type of editing behavior at the next editing position, determined based on the code prediction information, does not include code modification but includes code completion, the predicted code is displayed in grayscale at the next editing position.
5. The code editing method according to claim 2, characterized by, The step of displaying the predicted code in a preset edge region of the currently visible window based on the code prediction information includes: Based on the code prediction information, the original code and predicted code at the next editing position are displayed in a pop-up window in the preset edge area of the current visible window.
6. The code editing method according to claim 2, characterized in that, The current editing position is the current cursor position; The step of editing the code at the next editing position based on the predicted code includes: Jump the current cursor position to the next editing position, and change the code at the next editing position to the predicted code.
7. The code editing method according to claim 2, characterized in that, Also includes: If, based on user instructions, it is determined that the user has not accepted the predicted code or the user has manually edited the code, the actual editing position and the actual edited code at the actual editing position are recorded until a first preset time is reached; The location prediction model and the code editing model are fine-tuned and optimized by using the actual edit locations and actual edit codes as the real labels in the training data.
8. The code editing method according to claim 1, characterized in that, The step of inputting the code context information and the unfiltered context information from the initial context information into the code editing model to obtain the code prediction information of the target editing position output by the code editing model includes: The code context information, the unfiltered context information in the initial context information, and the code development document are input into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; the code development document includes project development document and / or application programming interface (API) design document.
9. The code editing method according to claim 1, characterized in that, The step of inputting the initial context information into the relevance filtering model to obtain the code context information related to the current editing requirements output by the relevance filtering model includes: The initial context information and preset prompt words are input into the relevance filtering model to obtain the code context information related to the current editing needs output by the relevance filtering model. The preset prompts are used to indicate at least one of the following: screening requirements, relevance assessment dimensions, screening strategies, output format requirements, and special scenario processing requirements.
10. The code editing method according to any one of claims 1 to 9, characterized in that, The initial context information also includes current compiler error information, multiple call stack information within a preset range of the current editing location, and at least one of the currently open file sets; The code context information also includes call stack information filtered from the plurality of call stack information and / or files filtered from the file set.
11. The code editing method according to any one of claims 1 to 9, characterized in that, The process of obtaining initial context information for indicating code editing needs includes: When a code editing requirement is triggered, the initial context information is obtained; The triggering methods for the code editing requirement include at least one of the following: The cursor stays at the end of the line of code for a duration greater than or equal to the second preset duration; The cursor is on a newline.
12. A code editing device, characterized in that, include: The information acquisition module is used to acquire initial context information that indicates code editing requirements; The initial context information includes multiple historical editing location information, code change records corresponding to multiple historical editing locations indicated by the multiple historical editing location information, and the code context of the current editing location; The information filtering module is used to input the initial context information into the relevance filtering model to obtain the code context information related to the current editing requirements output by the relevance filtering model. The relevance filtering model is built based on a large model; the code context information includes historical editing location information filtered from the multiple historical editing location information, and code change records filtered from the code change records corresponding to the multiple historical editing locations; The location prediction module is used to input the code context information and the unfiltered context information in the initial context information into the location prediction model to obtain the target editing location to be edited output by the location prediction model; The location prediction model is used to predict the editing location related to the current editing needs; The code prediction module is used to input the code context information and the unfiltered context information in the initial context information into the code editing model to obtain the code prediction information of the target editing position output by the code editing model; The code editing model is used to predict the code at the target editing location; The code editing module is used to edit the code based on the code prediction information and the target editing position.
13. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the code editing method as described in any one of claims 1 to 11.
14. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the code editing method as described in any one of claims 1 to 11.