Code recommendation method and device, electronic equipment and storage medium
By acquiring and analyzing the code snippets browsed and edited by users in code editing tools and combining them with a large language model to generate recommended code, the problem of inaccurate prediction of user coding intentions in existing technologies is solved, achieving higher code recommendation accuracy.
Patent Information
- Application Number
- CN202510941248.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-08
- Publication Date
- 2025-10-24
AI Technical Summary
In the existing technology, the accuracy of large language models based on the Transformer architecture in predicting user coding intentions in the field of code completion still needs to be improved.
By obtaining the code snippets that users browse and edit in code editing tools, combined with the context of the code recommendation location, a large language model is used to generate recommended code, integrating the code snippets and contextual information that users are interested in.
Improved the accuracy of code recommendations and more accurately predict user coding intentions.
Smart Images

Figure CN120832130A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer technology, and in particular, to a code recommendation method and device, electronic equipment and storage medium. BACKGROUND
[0002] In the field of code completion of software development tools, the current mainstream solution adopts a large language model (LLM) based on a Transformer architecture to generate and recommend subsequent code snippets in real time by analyzing the code context of the current file in the editor. To further enhance the relevance and reliability of the model output, retrieval-augmented generation (RAG) is introduced into the process, and its core mechanism is to not only rely on the local context of the current editing file when constructing a code completion prompt, but also actively retrieve information from the code repository of the user that is helpful for code recommendation, such as the definition of key methods, the implementation of other methods similar to the current coding logic or functional requirements, etc. However, the accuracy of predicting the user's coding intention in related solutions still needs to be improved. SUMMARY
[0003] This summary is provided to introduce concepts in a simplified form that are further described below in the detailed description. This summary is not intended to identify key features or essential features of the claimed technology, nor is it intended to be used to limit the scope of the claimed technology.
[0004] In a first aspect, according to one or more embodiments of the present disclosure, a code recommendation method is provided, comprising:
[0005] obtaining at least one first code snippet browsed by a user in a code editing tool;
[0006] obtaining at least one second code snippet edited by the user in the code editing tool;
[0007] generating recommended code based on the at least one first code snippet, the at least one second code snippet, and the code context corresponding to the code recommendation position.
[0008] In a second aspect, according to one or more embodiments of the present disclosure, a code recommendation device is provided, comprising:
[0009] a first obtaining unit configured to obtain at least one first code snippet browsed by a user in a code editing tool;
[0010] a second obtaining unit configured to obtain at least one second code snippet edited by the user in the code editing tool;
[0011] The code generation unit is configured to generate recommended code based on the at least one first code snippet, the at least one second code snippet, and a code context corresponding to the code recommendation position.
[0012] In a third aspect, an electronic device is provided, including at least one memory and at least one processor, wherein the memory is configured to store program code, and the processor is configured to invoke the program code stored in the memory to cause the electronic device to perform the method according to one or more embodiments of the present disclosure.
[0013] In a fourth aspect, a non-transitory computer storage medium is provided, which stores program code, and when the program code is executed by a computer device, the computer device performs the method according to one or more embodiments of the present disclosure.
[0014] According to one or more embodiments of the present disclosure, by obtaining the code snippets browsed and edited by the user in the code editing tool, and generating recommended code based on the code snippets focused by the user and the code context corresponding to the code recommendation position, the coding intention of the user can be more accurately predicted, thereby improving the accuracy of code recommendation. BRIEF DESCRIPTION OF DRAWINGS
[0015] The above and other features, advantages, and aspects of the present disclosure will become more apparent by describing in detail some embodiments thereof with reference to the annexed drawings in which:
[0016] Figure 1 A flowchart of a code recommendation method according to an embodiment of the present disclosure;
[0017] Figure 2 A structural diagram of a code recommendation device according to an embodiment of the present disclosure;
[0018] Figure 3 A structural diagram of an electronic device according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0019] Embodiments of the present disclosure will be described below in greater detail with reference to the accompanying drawings. While certain embodiments of the present disclosure are shown in the drawings, it is understood that the present disclosure can be embodied in various forms and should not be construed as being limited to the embodiments set forth herein, but rather should be construed to encompass all modifications, equivalents, and substitutes falling within the scope of the present disclosure. It is understood that the drawings and embodiments of the present disclosure are for exemplary purposes only and should not be construed to limit the scope of the present disclosure.
[0020] It is understood that the steps recited in the embodiments of the present disclosure can be performed in different orders and / or in parallel. In addition, the embodiments can include additional steps and / or omit performing the steps shown. The scope of the present disclosure is not limited in this regard.
[0021] The term "comprising" and variations thereof as used herein are used inclusively, i.e., "comprising, but not limited to." The term "based on" is "based at least in part on." The term "one embodiment" is used to mean "at least one embodiment;" the term "another embodiment" is used to mean "at least one additional embodiment;" and the term "some embodiments" is used to mean "at least some embodiments." The term "responsive to" and related terms are used to mean "responsive at least in part to." If an event x is "responsive to" an event y, then x can be directly or indirectly responsive to y. For example, y can cause x to occur, but other intervening events and / or conditions can also cause x to occur. In other cases, y can not cause x to occur, and x can occur even if y has not occurred. Further, the term "responsive to" can also mean "responsive at least in part to."
[0022] The term "determining" encompasses a wide variety of actions and, therefore, "determining" can include calculating, computing, processing, deriving, investigating, looking up (such as looking up in a table, a database or another data structure), ascertaining and the like. Additionally, "determining" can include receiving (such as receiving information), accessing (such as accessing data in a memory) and the like. Also, "determining" can include resolving, selecting, choosing, establishing and the like.
[0023] It can be understood that the data involved in the technical solution (including but not limited to the data itself, the obtaining or use of the data) should comply with the relevant legal regulations.
[0024] It can be understood that, before using the technical solutions disclosed in the embodiments of the present disclosure, the type of personal information involved in the present disclosure, the use range, the use scenario, etc. should be informed to the user and the authorization of the user should be obtained through appropriate means according to relevant laws and regulations. For example, in response to receiving the active request of the user, the user is sent prompt information to explicitly prompt the user that the operation requested to be performed will need to obtain and use the personal information of the user, so that the user can voluntarily choose whether to provide the personal information to the software or hardware such as electronic device, application program, server or storage medium, etc. that performs the operation of the technical solutions of the present disclosure according to the prompt information.
[0025] As an optional but non-limiting implementation manner, in response to receiving the active request of the user, the manner of sending prompt information to the user may, for example, be a pop-up window manner, and the prompt information may be presented in the form of text in the pop-up window. In addition, the pop-up window may also carry selection controls for the user to select "agree" or "disagree" to provide personal information to the electronic device.
[0026] It can be understood that the above notification and user authorization process is only illustrative and does not limit the implementation of the present disclosure, and other manners meeting the relevant laws and regulations can also be applied to the implementation of the present disclosure.
[0027] It should be noted that the concepts of "first", "second", etc. mentioned in the present disclosure are only used to distinguish different objects, devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these objects, devices, modules or units.
[0028] It should be noted that the modification of "one" or "multiple" in the present disclosure is illustrative but not limiting, and those skilled in the art should understand that, unless otherwise explicitly indicated in the context, it should be understood as "one or more".
[0029] For the purpose of the present disclosure, the phrase "A and / or B" means (A), (B) or (A and B).
[0030] The names of the messages or information exchanged between the plurality of devices in the embodiments of the present disclosure are only used for illustrative purposes, and are not used to limit the scope of the messages or information.
[0031] Reference Figure 1 which shows a flowchart of a code recommendation method 100 provided by an embodiment of the present disclosure. The method 100 includes:
[0032] Step S110: Obtain at least one first code snippet browsed by a user in a code editing tool.
[0033] Step S120: Obtain at least one second code snippet edited by the user in the code editing tool.
[0034] In some embodiments, the code editing tool is a software environment providing code editing functions, including but not limited to a code editor, an integrated development environment (IDE), a cloud development environment, a low-code platform, etc.
[0035] In some embodiments, the code snippets browsed or edited by the user in the code editing tool within a preset time recently and / or a preset number of times recently can be obtained. For example, the code snippets browsed by the user in the last 5 minutes or the last 50 times can be tracked, and the code snippets edited by the user in the last 12 hours can be tracked.
[0036] Steps S110 and S120 can be executed in parallel or sequentially, for example, steps S110 and S120 can be executed simultaneously, or step S110 can be executed first and then step S120, or step S120 can be executed first and then step S110, and the scope of the present disclosure is not limited in this respect.
[0037] Step S130: Generate recommended code based on the at least one first code snippet, the at least one second code snippet, and the code context corresponding to the code recommendation position.
[0038] In some embodiments, the code recommendation position includes the current stopping position of the cursor in the code file, which indicates the position where the user is writing or needs to continue to supplement the code. In other embodiments, the code recommendation position can include a position or range selected by the user that needs code recommendation or code rewriting.
[0039] The code context corresponding to the code recommendation position can include the code content before the code recommendation position and / or the code content after the code recommendation position. In some embodiments, a collection range threshold can be set for the code context in advance to collect the code content within the range threshold based on the code recommendation position. For example, 1-100 lines of code before the code recommendation position can be collected.
[0040] In some embodiments, the at least one first code snippet, the at least one second code snippet, and the code context corresponding to the code recommendation position can be input into a trained code recommendation model to obtain the recommended code. Illustratively, the code recommendation model can be a large language model (LLM). The large language model model learns the statistical rules and semantic representations inherent in text, uses a self-attention mechanism to realize context-aware word vector dynamic coding, and uses a multi-layer stacked decoder and encoder module to build deep semantic understanding capabilities. By pre-training on a large-scale dataset, the large language model has strong general modeling and generalization capabilities, and can handle various natural language processing tasks, including but not limited to text generation, question and answer systems, logical reasoning, semantic parsing, etc.
[0041] In some embodiments, the first code snippet and / or the second code snippet obtained in the foregoing steps are code snippets in other code files other than the code file to which the code recommendation position belongs. In this way, not only can the code context corresponding to the obtained code snippets and the code recommendation position be avoided, but also the generation of the recommended code can cover the parts of other code files that are concerned by the user in addition to the current code file.
[0042] In some embodiments, in addition to the foregoing content, the input content of the code recommendation model can also include other content, such as user description of requirements, annotations, function definitions or declarations, code file paths, other files currently opened, project structures, or dependent libraries, etc., but the present disclosure is not limited thereto.
[0043] In some embodiments, the generated recommended code can be displayed at a position corresponding to the code recommendation position (e.g., below the code recommendation position) or in a preset area provided by the code editing tool, which is not limited herein.
[0044] According to one or more embodiments of the present disclosure, by obtaining the code snippets that the user has browsed and edited in the code editing tool, and generating the recommended code based on the code snippets concerned by the user and the code context corresponding to the code recommendation position, the user's coding intention can be more accurately predicted, thereby improving the accuracy of code recommendation.
[0045] In some embodiments, the acquiring the at least one first code snippet browsed by the user in the code editing tool comprises: acquiring cursor click information of the user in the code editing tool, wherein the cursor click information comprises cursor click time and cursor click position; and determining the at least one first code snippet based on the cursor click information. The cursor click position can comprise the code line number on which the cursor is clicked. In this embodiment, by acquiring the cursor click information, it can be determined when the user clicks which line of code, and by arranging these events in reverse order of time, the code line number recently browsed by the user can be determined, and based on the code line number, the first code snippet recently browsed by the user can be obtained.
[0046] In some embodiments, the determining the at least one first code snippet based on the cursor click information comprises: performing sliding window slicing on one or more files recently browsed by the user in the code editing tool to obtain at least one candidate code snippet; and determining the at least one first code snippet from the at least one candidate code snippet based on the cursor click information corresponding to each candidate code snippet, wherein the cursor click information further comprises the number of times of cursor clicking. In some embodiments, the cursor click information corresponding to the first code snippet satisfies at least one of the following conditions: occurring within a first preset time period, occurring within a preset number of times of clicking, and the number of times of cursor clicking being not less than a first preset threshold.
[0047] Exemplarily, the implementation steps of the sliding window slicing are as follows:
[0048] (1) The size of the window is set to H lines;
[0049] (2) The first H lines from the start of the file are taken as the first candidate code snippet;
[0050] (3) The window is slid down by a step S, wherein 1≤S≤H (for example, S=1 means that the window is moved down by 1 line each time);
[0051] (4) The subsequent code snippets are taken in turn until the entire file is covered, so as to obtain a plurality of candidate code snippets each having a size of H lines.
[0052] Next, from the obtained multiple candidate code snippets, a candidate code snippet that is clicked by the user within a first preset time period and is clicked within a preset number of clicks (such as a code snippet that is clicked within the last 5 minutes and is clicked within the last 500 times) is selected, the candidate code snippets are sorted according to the number of clicks corresponding to the code snippets, and the first N candidate code snippets with the most number of clicks are selected as the first code snippets. The number of clicks corresponding to the code snippet can be the sum or average of the number of clicks corresponding to each line of code included in the snippet, the click time corresponding to the code snippet can be the click time corresponding to the most recently clicked line of code in the snippet, or the average click time of each line of code, or other suitable statistical methods, and the present disclosure is not limited thereto.
[0053] In some embodiments, a first preset threshold value can also be set for the number of cursor clicks corresponding to the selected first code snippet, to ensure that the number of clicks corresponding to each of the multiple first code snippets finally obtained is sufficient.
[0054] In some embodiments, the obtaining of the at least one second code snippet edited by the user in the code editing tool includes: obtaining editing operation information of the user in the code editing tool, and obtaining the at least one second code snippet based on the editing operation information. The editing operation information includes editing range information and editing content information, and the editing operation information satisfies at least one of the following conditions: occurring within a second preset time period, occurring within a preset number of edits.
[0055] For example, the editing operation of the user in the code editing tool can be listened to, and the editing operation information can be generated according to the editing operation. The editing operation information includes: a start offset, which is used to represent a global character index of a text change start position; an end offset, which is used to represent a global character index of a text change end position; and a change content, which is used to represent new text content inserted between the start offset and the end offset. Then, one or more second code snippets are obtained based on the editing information corresponding to the editing operation occurring within a second preset time period (such as within the last 2 hours). The actual range of the second code snippet can be the same as or greater than the editing range in the editing information corresponding to the second code snippet. For example, for an editing operation information, a code snippet corresponding to the editing range thereof and a code snippet of a preset number of lines before and / or after the code snippet can be collected to be used as a second code snippet.
[0056] In some embodiments, the obtaining the at least one second code segment based on the editing operation information comprises: merging the editing operation information that meets a preset editing range condition in the obtained editing operation information before determining the second code segment based on the editing operation information. The preset editing range condition may, for example, be that the interval between the editing ranges corresponding to two pieces of editing operation information does not exceed a preset number of characters or lines. If the interval between the editing ranges corresponding to two pieces of editing operation information does not exceed a preset number of characters or lines, it indicates that the two editing ranges are adjacent or close. In this way, the editing operation information that meets the preset editing range condition in the obtained editing operation information is merged, so as to ensure that each second code segment obtained based on each piece of editing operation information has high continuity and integrity.
[0057] In some embodiments, the generating the recommended code based on the first code segment set, the second code segment set, and the code context corresponding to the code recommendation position comprises: generating a third code segment based on the at least one first code segment and the at least one second code segment; and generating the recommended code based on the third code segment and the code context. The at least one first code segment and the at least one second code segment can be fused to obtain a third code segment, which can reflect the code content recently focused on by the user, and the recommended code can be generated based on the third code segment and the code context.
[0058] In some embodiments, the generating the recommended code based on the third code segment and the code context comprises: generating a prompt statement based on the third code segment and the code context; and inputting the prompt statement into a trained code recommendation model to obtain the recommended code. A prompt statement template with a preset structure can be set in advance, and the third code segment and the code context can be filled into the corresponding parts of the prompt statement template to obtain a prompt statement, which is input into the trained code recommendation model to obtain the recommended code.
[0059] In some embodiments, the generating the third code segment based on the at least one first code segment and the at least one second code segment comprises: alternately collecting code segments from the at least one first code segment and the at least one second code segment into the third code segment until the third code segment exceeds a preset maximum allowed value. In the process of generating the third code, when the third code segment exceeds the preset maximum allowed value, the process of generating the third code can be stopped, and the last added code segment can be revoked or the exceeding part can be deleted. For example, assuming that the queue formed by the at least one first code segment is {A1, A2, A3, A4, A5, …, An} and the queue formed by the at least one second code segment is {B1, B2, B3, B4, B5, …, Bm}, the content of the third code segment can be represented as {A1, B2, A2, B2, A3, B3, A4, B4}, but the present disclosure is not limited thereto.
[0060] In the present embodiment, by alternately collecting code segments from the first code segment queue and the second code segment queue into the third code segment until the third code segment exceeds a preset maximum allowed value, the third code segment can be made to be as uniform and fully integrated as possible within a limited length.
[0061] In some embodiments, the generating the third code segment based on the at least one first code segment and the at least one second code segment comprises: if there are two or more code segments in the at least one first code segment and the at least one second code segment that satisfy a preset positional relationship, performing a merging and deduplication processing on the two or more code segments, wherein the merging and deduplication processing is used to generate a new code segment based on the earliest starting position and the latest ending position of the two or more code segments to replace the two or more code segments. The code segments that are merged and deduplicated can come from the same code segment queue or from different code segment queues.
[0062] For example, if the first code segment A and the second code segment B in the at least one first code segment and the at least one second code segment are the 1st-4th lines and the 6th-9th lines of the same code file respectively, the two code segments can be merged and deduplicated to obtain a new code segment that replaces the two code segments, and the new code segment is the 1st-9th lines of code of the code file. For another example, if the first code segment A and the second code segment B in the at least one first code segment and the at least one second code segment are the 1st-6th lines and the 5th-10th lines of the same code file respectively, the two code segments can be merged and deduplicated to obtain a new code segment that replaces the two code segments, and the new code segment is the 1st-10th lines of code of the code file.
[0063] In some embodiments, the first and second code segments can be merged for de-duplication before the third code segment is generated based on the merging of the first and second code segments, or the first and second code segments included in the third code segment can be merged for de-duplication after the third code segment is generated based on the merging of the first and second code segments.
[0064] According to one or more embodiments of the present disclosure, by performing the merging processing operation on the code segments in the at least one first code segment and the at least one second code segment that meet the preset positional relationship, the continuity and integrity of the code content can be ensured.
[0065] Accordingly, with reference to Figure 2 , according to an embodiment of the present disclosure, a code recommendation apparatus 300 is provided, comprising:
[0066] A first obtaining unit 301 is configured to obtain at least one first code segment browsed by a user in a code editing tool;
[0067] A second obtaining unit 302 is configured to obtain at least one second code segment edited by the user in the code editing tool;
[0068] A code generating unit 303 is configured to generate recommended code based on the at least one first code segment, the at least one second code segment, and a code context corresponding to a code recommendation position.
[0069] In some embodiments, the first obtaining unit is configured to obtain cursor click information of the user in the code editing tool, and determine the at least one first code segment based on the cursor click information; wherein the cursor click information comprises cursor click time and cursor click position.
[0070] In some embodiments, the determination of the at least one first code segment based on the cursor click information comprises: slicing a file recently browsed by the user in the code editing tool by a sliding window to obtain at least one candidate code segment; and determining the at least one first code segment from the at least one candidate code segment based on the cursor click information corresponding to each candidate code segment; wherein the cursor click information further comprises cursor click times.
[0071] In some embodiments, the cursor click information corresponding to the first code segment satisfies at least one of the following conditions: occurring within a recent first preset time period, occurring within a recent preset number of clicks, and cursor click times being no less than a first preset threshold.
[0072] In some embodiments, the first obtaining unit is configured to obtain editing operation information of the user in the code editing tool, and obtain the at least one second code segment based on the editing operation information; wherein the editing operation information comprises editing range information and editing content information, and the editing operation information satisfies at least one of the following conditions: occurring within a second preset time period, occurring within a preset number of editing times.
[0073] In some embodiments, the obtaining of the at least one second code segment based on the editing operation information comprises: merging the editing operation information that satisfies a preset editing range condition in the obtained editing operation information before determining the second code segment based on the editing operation information.
[0074] In some embodiments, the code generation unit is configured to generate a third code segment based on the at least one first code segment and the at least one second code segment, and generate the recommended code based on the third code segment and the code context.
[0075] In some embodiments, the generation of the third code segment based on the at least one first code segment and the at least one second code segment comprises: alternately collecting code segments from the at least one first code segment and the at least one second code segment into the third code segment until the obtained third code segment exceeds a preset maximum allowed value.
[0076] In some embodiments, the generation of the third code segment based on the at least one first code segment and the at least one second code segment comprises: if there are more than two code segments that satisfy a preset position relationship in the at least one first code segment and the at least one second code segment, performing a merging and deduplication processing on the more than two code segments, wherein the merging and deduplication processing is configured to generate a new code segment based on a first start position and a last end position in the more than two code segments to replace the more than two code segments.
[0077] In some embodiments, the generation of the recommended code based on the third code segment and the code context comprises: generating a prompt statement based on the third code segment and the code context; and inputting the prompt statement into a trained code recommendation model to obtain the recommended code.
[0078] In some embodiments, the first code segment and / or the second code segment is a code segment in another code file other than the code file to which the code recommendation position belongs.
[0079] For the embodiment of the device, since it basically corresponds to the method embodiment, the relevant part can be seen from the part of the method embodiment. The device embodiment described above is only illustrative, and the modules described as separate modules can or can not be separated. Part or all of the modules can be selected according to actual needs to achieve the purpose of the embodiment. Those skilled in the art can understand and implement without creative labor.
[0080] Accordingly, according to one or more embodiments of the present disclosure, an electronic device is provided, comprising:
[0081] at least one memory and at least one processor;
[0082] The memory is configured to store program code, and the processor is configured to invoke the program code stored in the memory to cause the electronic device to perform the code recommendation method according to one or more embodiments of the present disclosure.
[0083] Accordingly, according to one or more embodiments of the present disclosure, a non-transitory computer storage medium is provided, which stores program code executable by a computer device to cause the computer device to perform the code recommendation method according to one or more embodiments of the present disclosure.
[0084] Reference is made below to Figure 3 which shows a structural schematic diagram of an electronic device (such as a terminal device or a server) 800 suitable for implementing embodiments of the present disclosure. The terminal device in the embodiments of the present disclosure can include, but is not limited to, terminal devices such as extended reality devices (such as head-mounted displays), mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablets), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), digital TVs, desktop computers, etc. It should be noted that, Figure 3 The electronic device shown is only an example and should not impose any limitation on the functions and use range of the embodiments of the present disclosure.
[0085] As shown in Figure 3 The electronic device 800 can include a processing device (such as a central processing unit, a graphics processing unit, etc.) 801, which can perform various appropriate actions and processes according to programs stored in a read-only memory (ROM) 802 or programs loaded from a storage device 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the electronic device 800 are also stored. The processing device 801, the ROM 802, and the RAM 803 are connected to each other through a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0086] Generally, the following devices can be connected to the I / O interface 805: input devices 806 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, and the like; output devices 807 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, and the like; storage devices 808 including, for example, a tape, a hard disk, and the like; and communication devices 809. The communication devices 809 can allow the electronic device 800 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 3 The electronic device 800 is shown with various devices, but it is understood that all of the illustrated devices are not required to implement or have the electronic device. More or less devices can alternatively be implemented or have.
[0087] In particular, the processes described above with reference to the flowcharts can be implemented as a computer software program according to embodiments of the present disclosure. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a computer readable medium, the computer program containing program code for executing the methods illustrated by the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network through the communication devices 809, or installed from the storage devices 808, or installed from the ROM 802. When the computer program is executed by the processing devices 801, the above-mentioned functions defined in the methods of the embodiments of the present disclosure are performed.
[0088] It should be noted that the computer-readable medium described above can be a computer-readable signal medium or a computer-readable storage medium or any combination thereof. The computer-readable storage medium, for example, can be, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus or device, or any suitable combination of the foregoing. More specific examples of the computer-readable storage medium can include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the disclosure, the computer-readable storage medium can be any tangible medium that contains or stores a program used by or in connection with an instruction execution system, apparatus or device. In the disclosure, the computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, in which the computer-readable program code is contained. Such a propagated data signal can take any of a variety of forms, including, but not limited to, an electromagnetic signal, an optical signal, or any suitable combination of the foregoing. The computer-readable signal medium can also be any computer-readable medium that is not a storage medium and that can communicate, propagate or transport a program for use by or in connection with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including, but not limited to, wire, cable, RF (radio frequency), or the like, or any suitable combination of the foregoing.
[0089] In some embodiments, the client, server, or both can communicate using any current known or future developed network protocol, such as HTTP (HyperText Transfer Protocol), and can be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include local area networks ("LAN"), wide area networks ("WAN"), the Internet, and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any current known or future developed networks.
[0090] The computer-readable medium described above can be included in the electronic device described above; or can exist separately from the electronic device, and can be accessed via the electronic device.
[0091] The computer-readable medium described above carries one or more programs, which, when executed by the electronic device, cause the electronic device to perform the method of the disclosure described above.
[0092] Computer program code for carrying out operations of the present disclosure can be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computer (for example, through the Internet using an Internet Service Provider).
[0093] The computer program instructions can also be loaded onto a computer or other programmable information processing apparatus to cause a series of operations to be performed on the computer or other programmable information processing apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable information processing apparatus implement the functions / acts specified in the flowchart and / or block diagram block or blocks.
[0094] The units described in the embodiments of the present disclosure can be implemented by hardware, software, or a combination of hardware and software. In some cases, the names of the units do not constitute a limitation on the units themselves.
[0095] The functions described in this specification can be performed by one or more hardware logic components. For example, non-limiting examples of hardware logic components that can be used include Field-programmable Gate Arrays (FPGAs), Program-specific Integrated Circuits (ASICs), program-specific Standard Products (ASSPs), System-on-a-chip systems (SOCs), Complex Programmable Logic Devices (CPLDs), etc.
[0096] In the context of this disclosure, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0097] According to one or more embodiments of the present disclosure, a code recommendation method is provided, including: obtaining at least one first code snippet browsed by a user in a code editing tool; obtaining at least one second code snippet edited by the user in the code editing tool; and generating a recommended code based on the at least one first code snippet, the at least one second code snippet, and a code context corresponding to a code recommendation position.
[0098] According to one or more embodiments of the present disclosure, the obtaining of the at least one first code snippet browsed by the user in the code editing tool includes: obtaining cursor click information of the user in the code editing tool, wherein the cursor click information includes cursor click time and cursor click position; and determining the at least one first code snippet based on the cursor click information.
[0099] According to one or more embodiments of the present disclosure, the determining of the at least one first code snippet based on the cursor click information includes: performing sliding window slicing on files browsed by the user in the code editing tool within a preset time period to obtain at least one candidate code snippet; and determining the at least one first code snippet from the at least one candidate code snippet based on cursor click information corresponding to each candidate code snippet, wherein the cursor click information further includes cursor click times.
[0100] According to one or more embodiments of the present disclosure, the cursor click information corresponding to the first code snippet satisfies at least one of the following conditions: occurring within a first preset time period, occurring within a preset number of clicks, and a cursor click time being not less than a first preset threshold.
[0101] According to one or more embodiments of the present disclosure, the obtaining the at least one second code snippet edited by the user in the code editing tool comprises: obtaining editing operation information of the user in the code editing tool, wherein the editing operation information comprises editing range information and editing content information, and the editing operation information satisfies at least one of the following conditions: occurring within a second preset time period, occurring within a preset number of editing times; and the at least one second code snippet is obtained based on the editing operation information.
[0102] According to one or more embodiments of the present disclosure, the obtaining the at least one second code snippet based on the editing operation information comprises: before determining the second code snippet based on the editing operation information, merging editing operation information that satisfies a preset editing range condition in the obtained editing operation information.
[0103] According to one or more embodiments of the present disclosure, the generating the recommended code based on the at least one first code snippet, the at least one second code snippet, and the code context corresponding to the code recommendation position comprises: generating a third code snippet based on the at least one first code snippet and the at least one second code snippet; and generating the recommended code based on the third code snippet and the code context.
[0104] According to one or more embodiments of the present disclosure, the generating the third code snippet based on the at least one first code snippet and the at least one second code snippet comprises: alternately collecting code snippets from the at least one first code snippet and the at least one second code snippet into the third code snippet until the obtained third code snippet exceeds a preset maximum allowed value.
[0105] According to one or more embodiments of the present disclosure, the generating the third code snippet based on the at least one first code snippet and the at least one second code snippet comprises: if there are more than two code snippets that satisfy a preset position relationship in the at least one first code snippet and the at least one second code snippet, performing a merging and deduplication processing on the more than two code snippets, wherein the merging and deduplication processing is used to generate a new code snippet based on a first start position and a last end position in the more than two code snippets to replace the more than two code snippets.
[0106] According to one or more embodiments of the present disclosure, the generating the recommended code based on the third code snippet and the code context comprises: generating a prompt statement based on the third code snippet and the code context; and inputting the prompt statement into a trained code recommendation model to obtain the recommended code.
[0107] According to one or more embodiments of the present disclosure, the first code snippet and / or the second code snippet is a code snippet in another code file other than a code file to which the code recommendation position belongs.
[0108] According to one or more embodiments of the present disclosure, a code recommendation apparatus is provided, comprising: a first obtaining unit configured to obtain at least one first code snippet browsed by a user in a code editing tool; a second obtaining unit configured to obtain at least one second code snippet edited by the user in the code editing tool; and a code generating unit configured to generate recommended code based on the at least one first code snippet, the at least one second code snippet, and a code context corresponding to a code recommendation position.
[0109] According to one or more embodiments of the present disclosure, an electronic device is provided, comprising: at least one memory and at least one processor; wherein the memory is configured to store program code, and the processor is configured to invoke the program code stored in the memory to enable the electronic device to perform the code recommendation method according to one or more embodiments of the present disclosure.
[0110] According to one or more embodiments of the present disclosure, a non-transitory computer storage medium is provided, which stores program code, and the program code is executed by a computer device to enable the computer device to perform the code recommendation method according to one or more embodiments of the present disclosure.
[0111] The above description is merely preferred embodiments of the present disclosure and a description of principles of applied technologies. It should be understood by those skilled in the art that the disclosed scope of the present disclosure is not limited to the technical solutions formed by the specific combinations of the above technical features, and should also cover other technical solutions formed by any combinations of the above technical features or equivalent features without departing from the above disclosed concepts. For example, the above technical features can be replaced with technical features disclosed in the present disclosure (but not limited to) having similar functions to form technical solutions.
[0112] In addition, although each operation is depicted in a particular order, this should not be understood as requiring the operations to be performed in the particular order shown or in sequential order. In certain circumstances, multitasking and parallel processing can be advantageous. Similarly, although several implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Certain features described in the context of separate embodiments can also be combined in a single embodiment. Conversely, various features described in the context of a single embodiment can also be separated and implemented in multiple embodiments.
[0113] Although the subject matter has been described in language specific to structural features and / or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are disclosed as example forms of implementing the claims.
Claims
1. A code recommendation method characterized by, The method comprises the following steps: acquiring at least one first code segment browsed by a user in a code editing tool; acquiring at least one second code segment edited by the user in the code editing tool; generating recommended code based on the at least one first code segment, the at least one second code segment and a code context corresponding to a code recommendation position.
2. The method of claim 1, wherein, The acquiring of the at least one first code segment browsed by the user in the code editing tool comprises the following steps: acquiring cursor click information of the user in the code editing tool, wherein the cursor click information comprises cursor click time and cursor click position; determining the at least one first code segment based on the cursor click information.
3. The method of claim 2, wherein, The determination of the at least one first code segment based on the cursor click information comprises the following steps: sliding window slicing of files recently browsed by the user in the code editing tool to obtain at least one candidate code segment; determining the at least one first code segment from the at least one candidate code segment based on the cursor click information corresponding to each candidate code segment, wherein the cursor click information further comprises cursor click times.
4. The method of claim 3, wherein, The cursor click information corresponding to the first code segment satisfies at least one of the following conditions: occurring within a first preset time period, occurring within a preset number of clicks, and the number of cursor clicks being not less than a first preset threshold.
5. The method of claim 1, wherein, The acquiring of the at least one second code segment edited by the user in the code editing tool comprises the following steps: acquiring editing operation information of the user in the code editing tool, wherein the editing operation information comprises editing range information and editing content information, and the editing operation information satisfies at least one of the following conditions: occurring within a second preset time period, occurring within a preset number of edits; acquiring the at least one second code segment based on the editing operation information.
6. The method of claim 5, wherein, The acquisition of the at least one second code segment based on the editing operation information comprises the following steps: before determining the second code segment based on the editing operation information, merging editing operation information in the acquired editing operation information that satisfies a preset editing range condition.
7. The method of claim 1, wherein, The generation of recommended code based on the at least one first code segment, the at least one second code segment and a code context corresponding to a code recommendation position comprises the following steps: generating a third code segment based on the at least one first code segment and the at least one second code segment; generating the recommended code based on the third code segment and the code context.
8. The method of claim 7, wherein, The generation of a third code segment based on the at least one first code segment and the at least one second code segment comprises the following steps: alternately collecting code segments from the at least one first code segment and the at least one second code segment into the third code segment until the obtained third code segment exceeds a preset maximum allowed value.
9. The method of claim 7, wherein, The generation of a third code segment based on the at least one first code segment and the at least one second code segment comprises the following steps: If there are more than two code segments satisfying the preset positional relationship in the at least one first code segment and the at least one second code segment, the more than two code segments are subjected to a merging and deduplication processing, wherein the merging and deduplication processing is used to generate a new code segment based on a first start position and a last end position of the more than two code segments, so as to replace the more than two code segments.
10. The method of claim 7, wherein, The generating the recommended code based on the third code segment and the code context comprises: generating a prompt statement based on the third code segment and the code context; inputting the prompt statement into a trained code recommendation model to obtain the recommended code.
11. The method of claim 1, wherein, The first code segment and / or the second code segment is a code segment in a code file other than the code file to which the code recommendation position belongs.
12. A code recommendation apparatus characterized by comprising: The method comprises: a first obtaining unit configured to obtain at least one first code segment browsed by a user in a code editing tool; a second obtaining unit configured to obtain at least one second code segment edited by the user in the code editing tool; a code generating unit configured to generate a recommended code based on the at least one first code segment, the at least one second code segment, and a code context corresponding to a code recommendation position.
13. An electronic device, comprising: The method comprises: at least one memory and at least one processor; wherein the memory is configured to store program code, and the processor is configured to invoke the program code stored in the memory to enable the electronic device to perform the method in any one of claims 1 to 11.
14. A non-transitory computer storage medium, comprising: the non-transitory computer storage medium stores program code, and the program code is executed by a computer device to enable the computer device to perform the method in any one of claims 1 to 11.