Code completion method and device, equipment and storage medium

By automatically converting the types of recommended variables based on syntax tree query and type comparison in the code editor, the problem of variable types mismatch in the existing technology is solved, and the fluency and writing efficiency of the code completion function are improved.

CN119668578AActive Publication Date: 2025-03-21GUANGZHOU ZHIYUAN ELECTRONICS CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411548025.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-01
Publication Date
2025-03-21
Estimated Expiration
2044-11-01

AI Technical Summary

Technical Problem

Existing code editors may have type mismatch when recommending variables, and users need to manually adjust them, which affects the fluency and writing efficiency of the code completion function.

Method used

By querying the pre-generated fault-tolerant syntax tree based on the current input position, determine the current tree node and code location, obtain the recommended variables in the recommendation list, and compare their current variable type with the target variable type. If it is inconsistent, query the target type conversion function based on the preset mapping list, combine the recommended variable with the function, and replace the original recommended variable to match the target type.

Benefits of technology

It realizes the automatic conversion of recommended variables into variable types required by users, without manual adjustment by users, ensuring the fluency of the code completion function and improving the efficiency of code writing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119668578A_ABST
    Figure CN119668578A_ABST
Patent Text Reader

Abstract

Embodiments of the invention provide a code completion method and apparatus, a device and a storage medium. The method comprises the steps of querying a pre-generated fault-tolerant syntax tree based on a current input position to determine a corresponding current tree node; determining the code position of the current input position according to the associated tree node corresponding to the current tree node; determining a target variable type of the current input content according to the code position; obtaining a recommendation list corresponding to the current input content, and extracting recommendation variables in the recommendation list; comparing the current variable type of the recommended variable with the target variable type; under the condition that the current variable type is inconsistent with the target variable type, querying a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function; and combining the recommendation variables with the target type conversion function to obtain a target expression, and replacing the recommendation variables in the recommendation list with the target expression. The recommendation variable is automatically converted into the target variable type.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a code completion method, apparatus, device, and storage medium. Background Art

[0002] A code editor is an important tool for programmers to write, test, and debug code. A code editor is a text editor used to write and modify computer program code. It usually provides syntax highlighting, auto-completion, code folding and other functions to improve programming efficiency and code readability. When a user enters a part of the code, the code editor can automatically display possible content options through the code completion function without the user having to type the complete code content.

[0003] Existing code editors can recommend variables based on user input content. However, the recommended variables may have type mismatch problems, requiring users to manually adjust them, affecting the user's fluency in using the code completion function and reducing the user's code writing efficiency. Summary of the invention

[0004] The embodiments of the present application provide a code completion method, apparatus, device and storage medium to solve the problem that variables recommended by existing code editors may have type mismatches, requiring users to manually adjust them, affecting the fluency of users in using the code completion function and reducing the user's code writing efficiency. The method automatically converts recommended variables into the variable type required by the user without the need for manual adjustment by the user, thereby ensuring the fluency of users in using the code completion function and improving the user's code writing efficiency.

[0005] In a first aspect, an embodiment of the present application provides a code completion method, including:

[0006] Based on the current input position, a pre-generated fault-tolerant syntax tree is queried to determine the corresponding current tree node;

[0007] Determine the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node;

[0008] Determine the target variable type corresponding to the current input content according to the code position;

[0009] Obtaining a recommendation list corresponding to the current input content, and extracting a recommendation variable in the recommendation list;

[0010] Comparing the current variable type of the recommended variable with the target variable type;

[0011] In the case where the current variable type is inconsistent with the target variable type, querying a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function;

[0012] The recommended variables are combined with a target type conversion function to obtain a target expression, and the recommended variables in the recommendation list are replaced with the target expression.

[0013] Optionally, determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node includes:

[0014] Determine whether there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node;

[0015] When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0016] Optionally, determining the target variable type corresponding to the current input content according to the code position includes:

[0017] When the code position is inside a function parameter, obtaining a call expression corresponding to the function;

[0018] Extracting an internal parameter corresponding to the function from the calling expression, and determining a first variable type corresponding to the internal parameter;

[0019] The first variable type is determined as the target variable type corresponding to the current input content.

[0020] Optionally, also include:

[0021] If there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket;

[0022] In the case where the parent node corresponding to the current tree node is a left square bracket, it is determined that the code position corresponding to the current input position is inside the array index.

[0023] Optionally, determining the target variable type corresponding to the current input content according to the code position includes:

[0024] In a case where the code position is inside an array index, determining a second variable type corresponding to the array index;

[0025] The second variable type is determined as the target variable type corresponding to the current input content.

[0026] Optionally, also include:

[0027] When the parent node corresponding to the current tree node is not a left bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;

[0028] In the case where an assignment symbol exists in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the assignment statement.

[0029] Optionally, determining the target variable type corresponding to the current input content according to the code position includes:

[0030] When the code position is inside an assignment statement and the current input position is located on the right side of the assignment symbol, obtaining a variable on the left side of the assignment symbol and determining a third variable type corresponding to the variable;

[0031] The third variable type is determined as the target variable type corresponding to the current input content.

[0032] Optionally, before querying the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node, the method further includes:

[0033] Acquire function registration data, wherein the function registration data includes an original variable type, an expected variable type, and a conversion function name;

[0034] According to the original variable type, the expected variable type and the conversion function name, a target key-value pair is constructed, and the target key-value pair is stored in a preset mapping list, wherein the original variable type and the expected variable type are the keys of the target key-value pair, and the conversion function name is the value of the target key-value pair.

[0035] In a second aspect, an embodiment of the present application further provides a code completion device, the code completion device comprising:

[0036] A tree node determination unit, configured to query a pre-generated fault-tolerant syntax tree based on a current input position to determine a corresponding current tree node;

[0037] A code position determining unit, configured to determine a code position corresponding to the current input position according to an associated tree node corresponding to the current tree node;

[0038] a variable type determination unit, configured to determine a target variable type corresponding to the current input content according to the code position;

[0039] a recommendation variable determining unit, configured to obtain a recommendation list corresponding to the current input content, and extract a recommendation variable from the recommendation list;

[0040] a variable type comparison unit, configured to compare the current variable type of the recommended variable with the target variable type;

[0041] a type conversion function determining unit, configured to query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function when the current variable type is inconsistent with the target variable type;

[0042] The type conversion function completion unit is configured to combine the recommended variable with a target type conversion function to obtain a target expression, and replace the recommended variable in the recommendation list with the target expression.

[0043] Optionally, the code location determination unit comprises:

[0044] The first position determination module is configured as follows:

[0045] Determine whether there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node;

[0046] When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0047] Optionally, a variable type determination unit includes:

[0048] The first type determination module is configured as follows:

[0049] When the code position is inside a function parameter, obtaining a call expression corresponding to the function;

[0050] Extracting an internal parameter corresponding to the function from the calling expression, and determining a first variable type corresponding to the internal parameter;

[0051] The first variable type is determined as the target variable type corresponding to the current input content.

[0052] Optionally, the code position determination unit further includes:

[0053] The second position determination module is configured as follows:

[0054] If there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket;

[0055] In the case where the parent node corresponding to the current tree node is a left square bracket, it is determined that the code position corresponding to the current input position is inside the array index.

[0056] Optionally, a variable type determination unit includes:

[0057] The second type determination module is configured as follows:

[0058] In a case where the code position is inside an array index, determining a second variable type corresponding to the array index;

[0059] The second variable type is determined as the target variable type corresponding to the current input content.

[0060] Optionally, the code position determination unit further includes:

[0061] The third position determination module is configured as follows:

[0062] When the parent node corresponding to the current tree node is not a left bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;

[0063] In the case where an assignment symbol exists in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the assignment statement.

[0064] Optionally, a variable type determination unit includes:

[0065] The third type determination module is configured as follows:

[0066] When the code position is inside an assignment statement and the current input position is located on the right side of the assignment symbol, obtaining a variable on the left side of the assignment symbol and determining a third variable type corresponding to the variable;

[0067] The third variable type is determined as the target variable type corresponding to the current input content.

[0068] Optionally, also include:

[0069] Map list building unit, configured as:

[0070] Acquire function registration data, wherein the function registration data includes an original variable type, an expected variable type, and a conversion function name;

[0071] According to the original variable type, the expected variable type and the conversion function name, a target key-value pair is constructed, and the target key-value pair is stored in a preset mapping list, wherein the original variable type and the expected variable type are the keys of the target key-value pair, and the conversion function name is the value of the target key-value pair.

[0072] In a third aspect, an embodiment of the present application further provides a code completion device, which includes: one or more processors; a storage device configured to store one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the code completion method described in the embodiment of the present application.

[0073] In a fourth aspect, an embodiment of the present application further provides a non-volatile storage medium storing computer executable instructions, wherein the computer executable instructions are configured to execute the code completion method described in the embodiment of the present application when executed by a computer processor.

[0074] In an embodiment of the present application, a corresponding current tree node is determined by querying a pre-generated fault-tolerant syntax tree based on the current input position; a code position corresponding to the current input position is determined according to an associated tree node corresponding to the current tree node; a target variable type corresponding to the current input content is determined according to the code position; a recommendation list corresponding to the current input content is obtained, and recommended variables in the recommendation list are extracted; the current variable type of the recommended variable is compared with the target variable type; when the current variable type is inconsistent with the target variable type, a preset mapping list is queried according to the current variable type and the target variable type to determine a target type conversion function; the recommended variable is combined with the target type conversion function to obtain a target expression, and the recommended variable in the recommendation list is replaced with the target expression. In the above scheme, by querying the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node, and determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged, and by extracting the recommended variables in the recommendation list and comparing the current variable type of the recommended variables with the target variable type, it can be effectively judged whether the recommended variables are consistent with the variable type required at the current input position, and by combining the recommended variables with the target type function to obtain the target expression, and replacing the recommended variables with the target expression, the recommended variables can be converted into the variable type required by the user in advance without the need for manual adjustment by the user, thereby ensuring the fluency of the user in using the code completion function and improving the user's code writing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0075] Figure 1 A flowchart of a code completion method provided in an embodiment of the present application;

[0076] Figure 2 A flowchart of a specific implementation process of determining a code position corresponding to a current input position provided in an embodiment of the present application;

[0077] Figure 3A flowchart of another specific implementation process of determining a code position corresponding to a current input position provided in an embodiment of the present application;

[0078] Figure 4 A flowchart of another specific implementation process of determining a code position corresponding to a current input position provided in an embodiment of the present application;

[0079] Figure 5 A flowchart of a specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application;

[0080] Figure 6 A flowchart of another specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application;

[0081] Figure 7 A flowchart of another specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application;

[0082] Figure 8 A flowchart of another code completion method provided in an embodiment of the present application;

[0083] Fig. 9 A structural block diagram of a code completion device provided in an embodiment of the present application;

[0084] Fig.10 A schematic diagram of the structure of a code completion device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0085] The embodiments of the present application are further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the embodiments of the present application, rather than to limit the embodiments of the present application. It should also be noted that, for ease of description, only parts related to the embodiments of the present application are shown in the accompanying drawings, rather than all structures.

[0086] The terms "first", "second", etc. in the specification and claims of this application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.

[0087] In the code completion method provided in the embodiment of the present application, the executor of each step can be a computer device, which refers to any electronic device with data calculation, processing and storage capabilities, such as terminal devices such as PC (Personal Computer), or devices such as servers, which are not limited in the embodiment of the present application.

[0088] Figure 1 This is a flowchart of a code completion method provided in an embodiment of the present application. The code completion method can be implemented with a code completion device as the execution subject. Figure 1 As shown, the code completion method specifically includes the following steps:

[0089] Step S101: query a pre-generated fault-tolerant syntax tree based on the current input position to determine a corresponding current tree node.

[0090] Wherein, the code completion method provided by the embodiment of the present application can be applied to a code editor, and the code editor can provide a code editing interface, and the user can write, view, edit and debug computer code through the code editing interface. It should be noted that in the process of the user inputting code through the code editing interface, the existing code content of the code editing interface can be parsed to generate a corresponding fault-tolerant syntax tree. Specifically, the fault-tolerant syntax tree can be generated based on lexical analysis and grammatical analysis, wherein the lexical analysis can be to convert the existing code content into lexical units one by one, corresponding to the smallest grammatical unit in the code, such as keywords, identifiers, operators, separators, etc. Developers can define lexical rules according to the programming language and development requirements of actual applications, and the lexical rules describe how to identify each lexical unit in the code content, which is not limited in this application. In addition, after lexical analysis, grammatical analysis is required to convert the sequence composed of lexical units into a syntax tree according to grammatical rules, and the grammatical rules can describe the relationship between each grammatical unit in the code content, wherein each node in the syntax tree represents a grammatical unit, such as a statement, expression, operator, etc., and the child nodes of a node represent the grammatical elements contained in the node. It is worth noting that, since the code content in the code editing interface is still in the input stage, a fault-tolerant syntax tree can be generated, and a certain degree of syntax errors are allowed in the syntax analysis stage while still being able to generate a valid syntax tree, which can make the code editing more compatible and avoid the failure of the entire parsing process due to small errors. In addition, the current input position can be the cursor position input in the code editing interface, which can be obtained by monitoring the cursor movement event of the code editing interface or determining the row number and column number of the current cursor through the status bar. Specifically, according to the current input position, traversal can be started from the root node of the fault-tolerant syntax tree. During the traversal process, the position information of each node, such as the row number and column number, is recorded. When traversing to a certain node, its position information can be compared with the current input position. If the match is consistent, the node can be regarded as the current tree node corresponding to the current input position.

[0091] Step S102, determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node;

[0092] Specifically, the code position may be a code statement, function, data structure, etc. where the current input position is located. The associated tree node may be a parent node, a sibling node, etc. corresponding to the current tree node. Since the syntax elements of the tree nodes corresponding to different code positions are different, the code position corresponding to the current input position may be determined based on the associated tree node corresponding to the current tree node and the syntax elements corresponding to the associated tree node.

[0093] In an optional embodiment, a specific implementation process of determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node is described. Please refer to Figure 2 , which is a flowchart of a specific implementation process of determining a code position corresponding to a current input position provided by an embodiment of the present application, such as Figure 2 As shown, the specific implementation steps for determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node are as follows:

[0094] Step S1021: Determine whether adjacent variable names and left parentheses exist simultaneously in the associated tree node corresponding to the current tree node.

[0095] Among them, according to the function structure characteristics, if the current input position is inside the function parameters, then the associated tree nodes corresponding to the current tree node (for example, parent nodes, brother nodes, etc.) must have adjacent variable names and left parentheses. For example, for the function call expression: GetSpeed(Speed_Scale:=Δ), if Δ represents the current input position, then its associated tree nodes include "GetSpeed", "(", "Speed_Scale" and ":=", and it is obvious that "GetSpeed" and "(" are adjacent, and "(" and "Speed_Scale". Therefore, it can be determined whether the current input position is inside the function parameters by judging whether adjacent variable names and left parentheses exist in the associated tree nodes corresponding to the current tree node at the same time.

[0096] Step S1022: When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0097] In a specific embodiment, Figure 2 Based on the embodiment provided, another specific implementation process of determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node is described. Please refer to Figure 3 , which is a flowchart of another specific implementation process of determining the code position corresponding to the current input position provided by an embodiment of the present application, such as Figure 3 As shown, the specific implementation steps for determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node are as follows:

[0098] Step S1021: Determine whether adjacent variable names and left parentheses exist simultaneously in the associated tree node corresponding to the current tree node.

[0099] Step S1022: When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0100] Step S1023: When there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket.

[0101] According to the array structure characteristics, if the current input position is inside the array index, then the parent node corresponding to the current tree node must be a left bracket. For example, for the array expression: arr[Δ], if Δ represents the current input position, then its parent node is "[". Therefore, by judging whether the parent node corresponding to the current tree node is a left bracket, it can be determined whether the current input position is inside the array index.

[0102] Step S1024: When the parent node corresponding to the current tree node is a left bracket, determine that the code position corresponding to the current input position is inside the array index.

[0103] In another specific embodiment, Figure 3 Based on the embodiment provided, another specific implementation process of determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node is described. Please refer to Figure 4 , which is a flowchart of another specific implementation process of determining the code position corresponding to the current input position provided by an embodiment of the present application, such as Figure 4 As shown, the specific implementation steps for determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node are as follows:

[0104] Step S1021: Determine whether adjacent variable names and left parentheses exist simultaneously in the associated tree node corresponding to the current tree node.

[0105] Step S1022: When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0106] Step S1023: When there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket.

[0107] Step S1024: When the parent node corresponding to the current tree node is a left bracket, determine that the code position corresponding to the current input position is inside the array index.

[0108] Step S1025: When the parent node corresponding to the current tree node is not a left bracket, determine whether there is an assignment symbol in the associated tree node corresponding to the current tree node.

[0109] Among them, according to the characteristics of the assignment statement, if the current input position is inside the assignment statement, then there must be an assignment symbol in the associated tree node corresponding to the current tree node. For example, for the assignment statement: person.name.length:=Δ, if Δ represents the current input position, then there will be ":=" in the corresponding associated tree node. Therefore, it is possible to determine whether the current input position is inside the assignment statement by judging whether there is an assignment symbol in the associated tree node corresponding to the current tree node.

[0110] Step S1026: When there is an assignment symbol in the associated tree node corresponding to the current tree node, determine that the code position corresponding to the current input position is inside the assignment statement.

[0111] As described above, by determining whether the associated tree node corresponding to the current tree node contains a preset syntax element, the code position corresponding to the current input position can be accurately determined, providing reliable position reference information for subsequent determination of the target variable type.

[0112] Step S103: Determine the target variable type corresponding to the current input content according to the code position.

[0113] In an optional embodiment, a specific implementation process of determining the target variable type corresponding to the current input content according to the code position is described. Please refer to Figure 5 , which is a flowchart of a specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application, such as Figure 5 As shown, the specific implementation steps for determining the target variable type corresponding to the current input content according to the code position are as follows:

[0114] Step S1031: When the code position is inside a function parameter, obtain a call expression corresponding to the function.

[0115] Step S1032: extract the internal parameters corresponding to the function from the calling expression, and determine the first variable type corresponding to the internal parameters.

[0116] Step S1033: determine the first variable type as the target variable type corresponding to the current input content.

[0117] It should be noted that if the code position is inside the function parameter, the recommended variable corresponding to the current input position should be consistent with the variable type of the internal parameter of the function, otherwise the user needs to manually adjust the type. Therefore, you can first obtain the call expression corresponding to the function, extract the corresponding internal parameters, and determine the first variable type corresponding to the internal parameters. The first variable type can be regarded as the target variable type corresponding to the current input content. For example, the current input position is inside the function parameter. For the function call expression: GetSpeed(Speed_Scale:=Δ), if Δ represents the current input position, it can be determined that GetSpeed ​​is the function name, and Speed_Scale is the internal parameter of GetSpeed. The variable type of Δ should be consistent with the variable type of Speed_Scale. For example, the variable type of Speed_Scale is int type, then the target variable type corresponding to the current input content should be int type.

[0118] In a specific embodiment, another specific implementation process of determining the target variable type corresponding to the current input content according to the code position is described. Please refer to Figure 6 , which is a flowchart of another specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application, such as Figure 6 As shown, the specific implementation steps for determining the target variable type corresponding to the current input content according to the code position are as follows:

[0119] Step S1034: When the code position is inside the array index, determine the second variable type corresponding to the array index.

[0120] Step S1035: determine the second variable type as the target variable type corresponding to the current input content.

[0121] It is worth noting that if the code position is inside the array index, the recommended variable corresponding to the current input position should be consistent with the variable type of the array index. Therefore, the second variable type corresponding to the array index can be determined first, and the second variable type can be regarded as the target variable type corresponding to the current input content. For example, for the array: arr[Δ], if Δ represents the current input position, the variable type corresponding to Δ should be int type, and the target variable type corresponding to the current input content should be int type.

[0122] In another specific embodiment, another specific implementation process of determining the target variable type corresponding to the current input content according to the code position is described. Please refer to Figure 7 , which is a flowchart of another specific implementation process of determining the target variable type corresponding to the current input content provided by an embodiment of the present application, such as Figure 7As shown, the specific implementation steps for determining the target variable type corresponding to the current input content according to the code position are as follows:

[0123] Step S1036: When the code position is inside an assignment statement and the current input position is located on the right side of the assignment symbol, obtain the variable on the left side of the assignment symbol and determine the third variable type corresponding to the variable.

[0124] Step S1037: determine the third variable type as the target variable type corresponding to the current input content.

[0125] It should be noted that if the code position is inside an assignment statement and the current input position is on the right side of the assignment symbol, the recommended variable corresponding to the current input position should be consistent with the type of the variable on the left side of the assignment symbol. Therefore, the third variable type corresponding to the variable on the left side of the assignment symbol can be determined first, and the third variable type can be regarded as the target variable type corresponding to the current input content. Among them, the variable on the left side of the assignment symbol may be a simple variable or a member variable. For example, for the assignment statement: person.name.length:=Δ, the type of each variable in the variable expression person.name.length can be analyzed from left to right, where the variable person is of person type, the member variable name is of string type, and the member variable length is of int type. Then the variable type corresponding to Δ should be int type, that is, the target variable type corresponding to the current input content should be int type.

[0126] As described above, by determining the target variable type corresponding to the current input content according to the code position, reference information can be provided for subsequent judgment of whether the variable type of the recommended variable meets expectations, which is conducive to the subsequent accurate addition of type conversion functions for the recommended variables.

[0127] Step S104: Obtain a recommendation list corresponding to the current input content, and extract the recommendation variables in the recommendation list.

[0128] Among them, the current input content is an incomplete input character text. Based on the pre-generated fault-tolerant syntax tree, the nodes related to the input character text can be traversed to obtain a recommendation list corresponding to the current input content. The recommendation list may include recommended variables, recommended functions, recommended classes, recommended code blocks, etc. In the embodiment of the present application, for the specific needs of the user, it is not necessary to perform type conversion on other recommended content other than the recommended variables. Therefore, the recommended variables in the recommendation list can be extracted for subsequent type comparison.

[0129] Step S105: Compare the current variable type of the recommended variable with the target variable type.

[0130] It should be noted that since the recommended variables may be local variables or global variables of existing code content, their variable types may be different from the target variable types, so a comparison is required to confirm whether the variable types are consistent.

[0131] Step S106: When the current variable type is inconsistent with the target variable type, a preset mapping list is queried according to the current variable type and the target variable type to determine a target type conversion function.

[0132] It is worth noting that if the current variable type is inconsistent with the target variable type, it means that the recommended variable cannot be directly applied to the current input position and needs to be converted. For example, the current input content is "len", and the corresponding recommended variable is "length", while "length" is a local variable of real type, and the target variable type is int type. Therefore, based on the current variable type: real type and the target variable type: int type, the preset mapping list can be searched to determine that the target type conversion function is real_to_int(). It can be understood that real_to_int() converts the input real type variable to an int type variable.

[0133] Step S107: Combine the recommended variable with the target type conversion function to obtain a target expression, and replace the recommended variable in the recommendation list with the target expression.

[0134] For example, the current input content is "len", the corresponding recommended variable is "length", and the corresponding target type conversion function is real_to_int(), then the combined target expression is real_to_int(length), and the original recommended variable "length" in the recommendation list will be replaced by "real_to_int(length)", then the user can directly select "real_to_int(length)" for code completion, without manually adjusting the variable type of "length", thereby improving code writing efficiency and reducing code errors. It should be noted that compared with the related art, the code completion method provided in the embodiment of the present application can determine the target variable type corresponding to the current input position, and further complete the type conversion function for the recommended variable corresponding to the current input content, without the need for subsequent manual adjustment by the user.

[0135] In the above, the corresponding current tree node is determined by querying the pre-generated fault-tolerant syntax tree based on the current input position; the code position corresponding to the current input position is determined according to the associated tree node corresponding to the current tree node; the target variable type corresponding to the current input content is determined according to the code position; the recommendation list corresponding to the current input content is obtained, and the recommended variables in the recommendation list are extracted; the current variable type of the recommended variable is compared with the target variable type; when the current variable type is inconsistent with the target variable type, the preset mapping list is queried according to the current variable type and the target variable type to determine the target type conversion function; the recommended variable is combined with the target type conversion function to obtain the target expression, and the recommended variable in the recommendation list is replaced with the target expression. In the above scheme, by querying the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node, and determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged, and by extracting the recommended variables in the recommendation list and comparing the current variable type of the recommended variables with the target variable type, it can be effectively judged whether the recommended variables are consistent with the variable type required at the current input position, and by combining the recommended variables with the target type function to obtain the target expression, and replacing the recommended variables with the target expression, the recommended variables can be converted into the variable type required by the user in advance without the need for manual adjustment by the user, thereby ensuring the fluency of the user in using the code completion function and improving the user's code writing efficiency.

[0136] Figure 8 A flowchart of another code completion method provided in an embodiment of the present application. Figure 8 As shown, the code completion method specifically includes the following steps:

[0137] Step S201: Acquire function registration data, where the function registration data includes original variable type, expected variable type and conversion function name.

[0138] For example, the function registration data may be as shown in Table 1:

[0139] Table 1

[0140] Primitive variable types Expected variable type Conversion function name int real int_to_real real string real_to_string byte word word_from_byte char wchar char_to_wchar

[0141] Step S202: construct a target key-value pair according to the original variable type, the expected variable type and the conversion function name, and store the target key-value pair in a preset mapping list, wherein the original variable type and the expected variable type are the keys of the target key-value pair, and the conversion function name is the value of the target key-value pair.

[0142] Step S203: query the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node.

[0143] Step S204: Determine the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node.

[0144] Step S205: Determine the target variable type corresponding to the current input content according to the code position.

[0145] Step S206: Obtain a recommendation list corresponding to the current input content, and extract the recommendation variables in the recommendation list.

[0146] Step S207: Compare the current variable type of the recommended variable with the target variable type.

[0147] Step S208: When the current variable type is inconsistent with the target variable type, a preset mapping list is queried according to the current variable type and the target variable type to determine a target type conversion function.

[0148] Step S209: Combine the recommended variable with the target type conversion function to obtain a target expression, and replace the recommended variable in the recommendation list with the target expression.

[0149] As described above, by pre-building a preset mapping list corresponding to the original variable type, the expected variable type and the conversion function name, it is possible to quickly query the corresponding type conversion function when performing type conversion on the recommended variable, thereby improving conversion efficiency.

[0150] Fig. 9 This is a structural block diagram of a code completion device provided in an embodiment of the present application. The device is configured to execute the code completion method provided in the above embodiment and has the corresponding functional modules and beneficial effects of the execution method. Fig. 9 As shown, the code completion device specifically includes:

[0151] A tree node determination unit 101 is configured to query a pre-generated fault-tolerant syntax tree based on a current input position to determine a corresponding current tree node;

[0152] A code position determining unit 102, configured to determine a code position corresponding to the current input position according to an associated tree node corresponding to the current tree node;

[0153] A variable type determination unit 103 is configured to determine a target variable type corresponding to the current input content according to the code position;

[0154] A recommendation variable determination unit 104 is configured to obtain a recommendation list corresponding to the current input content and extract a recommendation variable from the recommendation list;

[0155] A variable type comparison unit 105, configured to compare the current variable type of the recommended variable with the target variable type;

[0156] A type conversion function determining unit 106 is configured to query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function when the current variable type is inconsistent with the target variable type;

[0157] The type conversion function completion unit 107 is configured to combine the recommended variable with a target type conversion function to obtain a target expression, and replace the recommended variable in the recommendation list with the target expression.

[0158] In the above, the corresponding current tree node is determined by querying the pre-generated fault-tolerant syntax tree based on the current input position; the code position corresponding to the current input position is determined according to the associated tree node corresponding to the current tree node; the target variable type corresponding to the current input content is determined according to the code position; the recommendation list corresponding to the current input content is obtained, and the recommended variables in the recommendation list are extracted; the current variable type of the recommended variable is compared with the target variable type; when the current variable type is inconsistent with the target variable type, the preset mapping list is queried according to the current variable type and the target variable type to determine the target type conversion function; the recommended variable is combined with the target type conversion function to obtain the target expression, and the recommended variable in the recommendation list is replaced with the target expression. In the above scheme, by querying the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node, and determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged, and by extracting the recommended variables in the recommendation list and comparing the current variable type of the recommended variables with the target variable type, it can be effectively judged whether the recommended variables are consistent with the variable type required at the current input position, and by combining the recommended variables with the target type function to obtain the target expression, and replacing the recommended variables with the target expression, the recommended variables can be converted into the variable type required by the user in advance without the need for manual adjustment by the user, thereby ensuring the fluency of the user in using the code completion function and improving the user's code writing efficiency.

[0159] In a possible embodiment, the code position determination unit 102 includes:

[0160] The first position determination module is configured as follows:

[0161] Determine whether there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node;

[0162] When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

[0163] In a possible embodiment, the variable type determination unit 103 includes:

[0164] The first type determination module is configured as follows:

[0165] When the code position is inside a function parameter, obtaining a call expression corresponding to the function;

[0166] Extracting an internal parameter corresponding to the function from the calling expression, and determining a first variable type corresponding to the internal parameter;

[0167] The first variable type is determined as the target variable type corresponding to the current input content.

[0168] In a possible embodiment, the code position determining unit 102 further includes:

[0169] The second position determination module is configured as follows:

[0170] If there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket;

[0171] In the case where the parent node corresponding to the current tree node is a left square bracket, it is determined that the code position corresponding to the current input position is inside the array index.

[0172] In a possible embodiment, the variable type determination unit 103 includes:

[0173] The second type determination module is configured as follows:

[0174] In a case where the code position is inside an array index, determining a second variable type corresponding to the array index;

[0175] The second variable type is determined as the target variable type corresponding to the current input content.

[0176] In a possible embodiment, the code position determining unit 102 further includes:

[0177] The third position determination module is configured as follows:

[0178] When the parent node corresponding to the current tree node is not a left bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;

[0179] In the case where an assignment symbol exists in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the assignment statement.

[0180] In a possible embodiment, the variable type determination unit 103 includes:

[0181] The third type determination module is configured as follows:

[0182] When the code position is inside an assignment statement and the current input position is located on the right side of the assignment symbol, obtaining a variable on the left side of the assignment symbol and determining a third variable type corresponding to the variable;

[0183] The third variable type is determined as the target variable type corresponding to the current input content.

[0184] In a possible embodiment, it further includes:

[0185] Map list building unit, configured as:

[0186] Acquire function registration data, wherein the function registration data includes an original variable type, an expected variable type, and a conversion function name;

[0187] According to the original variable type, the expected variable type and the conversion function name, a target key-value pair is constructed, and the target key-value pair is stored in a preset mapping list, wherein the original variable type and the expected variable type are the keys of the target key-value pair, and the conversion function name is the value of the target key-value pair.

[0188] Fig.10 A schematic diagram of the structure of a code completion device provided in an embodiment of the present application is shown in FIG. Fig.10 As shown, the device includes a processor 201, a memory 202, an input device 203 and an output device 204; the number of processors 201 in the device can be one or more. Fig.10 A processor 201 is taken as an example; the processor 201, memory 202, input device 203 and output device 204 in the device can be connected by a bus or other means. Fig.10 The example of connecting through a bus is taken. The memory 202, as a computer-readable storage medium, can be configured to store software programs, computer executable programs and modules, such as program instructions / modules corresponding to the code completion method in the embodiment of the present application. The processor 201 executes various functional applications and data processing of the device by running the software programs, instructions and modules stored in the memory 202, that is, implements the above-mentioned code completion method. The input device 203 can be configured to receive input digital or character information, and generate key signal input related to user settings and function control of the device. The output device 204 may include a display device such as a display screen.

[0189] The code completion device provided above can be used to execute the code completion method provided in any of the above embodiments, and has corresponding functions and beneficial effects.

[0190] An embodiment of the present application also provides a non-volatile storage medium containing computer executable instructions, which, when executed by a computer processor, is configured to execute a code completion method described in the above embodiment, wherein the code completion method includes: querying a pre-generated fault-tolerant syntax tree based on a current input position to determine the corresponding current tree node; determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node; determining the target variable type corresponding to the current input content according to the code position; obtaining a recommendation list corresponding to the current input content, and extracting the recommended variables in the recommendation list; comparing the current variable type of the recommended variable with the target variable type; when the current variable type is inconsistent with the target variable type, querying a preset mapping list according to the current variable type and the target variable type to determine the target type conversion function; combining the recommended variable with the target type conversion function to obtain a target expression, and replacing the recommended variable in the recommendation list with the target expression.

[0191] Storage medium - any of various types of memory devices or storage devices. The term "storage medium" is intended to include: installation media, such as CD-ROM, floppy disk or tape devices; computer system memory or random access memory, such as DRAM, DDR RAM, SRAM, EDO RAM, Rambus RAM, etc.; non-volatile memory, such as flash memory, magnetic media and optical storage; registers or other similar types of memory elements, etc. Storage media may also include other types of memory or combinations thereof. In addition, the storage medium may be located in a first computer system in which the program is executed, or may be located in a different second computer system, which is connected to the first computer system via a network (such as the Internet). The second computer system can provide program instructions to the first computer for execution. The term "storage medium" may include two or more storage media residing in different locations (e.g., in different computer systems connected via a network). The storage medium may store program instructions (e.g., embodied as a computer program) that can be executed by one or more processors.

[0192] Of course, the storage medium containing computer executable instructions provided in the embodiment of the present application, whose computer executable instructions are not limited to the above code completion method, can also execute related operations in the code completion method provided in any embodiment of the present application.

[0193] It is worth noting that in the embodiment of the above-mentioned code completion device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not configured to limit the protection scope of the embodiments of the present application.

[0194] It should be noted that the numbering of each step in this scheme is only used to describe the overall design framework of this scheme, and does not represent the necessary order relationship between the steps. On the basis that the overall implementation process conforms to the overall design framework of this scheme, it belongs to the protection scope of this scheme, and the order of precedence in the form of text during description is not an exclusive limitation on the specific implementation process of this scheme. It should be understood by those skilled in the art that the embodiments of the present application can be provided as methods, systems, or computer program products. In a typical configuration, a computing device includes one or more processors (CPU), an input / output interface, a network interface, and a memory. The memory may include non-permanent memory in a computer-readable medium, a random access memory (RAM) and / or a non-volatile memory in the form of a read-only memory (ROM) or a flash memory (flash RAM). The memory is an example of a computer-readable medium.

[0195] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0196] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.

Claims

1. A code completion method, characterized in that: include: Based on the current input position, a pre-generated fault-tolerant syntax tree is queried to determine the corresponding current tree node; Determine the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node; Determine the target variable type corresponding to the current input content according to the code position; Obtaining a recommendation list corresponding to the current input content, and extracting a recommendation variable in the recommendation list; Comparing the current variable type of the recommended variable with the target variable type; In the case where the current variable type is inconsistent with the target variable type, querying a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function; The recommended variables are combined with a target type conversion function to obtain a target expression, and the recommended variables in the recommendation list are replaced with the target expression.

2. The code completion method according to claim 1, characterized in that: The determining the code position corresponding to the current input position according to the associated tree node corresponding to the current tree node includes: Determine whether there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node; When there are adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the function parameter.

3. The code completion method according to claim 2, characterized in that: The determining the target variable type corresponding to the current input content according to the code position includes: When the code position is inside a function parameter, obtaining a call expression corresponding to the function; Extracting an internal parameter corresponding to the function from the calling expression, and determining a first variable type corresponding to the internal parameter; The first variable type is determined as the target variable type corresponding to the current input content.

4. The code completion method according to claim 2, characterized in that: Also includes: If there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determine whether the parent node corresponding to the current tree node is a left bracket; In the case where the parent node corresponding to the current tree node is a left square bracket, it is determined that the code position corresponding to the current input position is inside the array index.

5. The code completion method according to claim 4, characterized in that: The determining the target variable type corresponding to the current input content according to the code position includes: In a case where the code position is inside an array index, determining a second variable type corresponding to the array index; The second variable type is determined as the target variable type corresponding to the current input content.

6. The code completion method according to claim 4, characterized in that: Also includes: When the parent node corresponding to the current tree node is not a left bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node; In the case where an assignment symbol exists in the associated tree node corresponding to the current tree node, it is determined that the code position corresponding to the current input position is inside the assignment statement.

7. The code completion method according to claim 6, characterized in that: The determining the target variable type corresponding to the current input content according to the code position includes: When the code position is inside an assignment statement and the current input position is located on the right side of the assignment symbol, obtaining a variable on the left side of the assignment symbol and determining a third variable type corresponding to the variable; The third variable type is determined as the target variable type corresponding to the current input content.

8. The code completion method according to claim 1, characterized in that: Before querying the pre-generated fault-tolerant syntax tree based on the current input position to determine the corresponding current tree node, the method further includes: Acquire function registration data, wherein the function registration data includes an original variable type, an expected variable type, and a conversion function name; According to the original variable type, the expected variable type and the conversion function name, a target key-value pair is constructed, and the target key-value pair is stored in a preset mapping list, wherein the original variable type and the expected variable type are the keys of the target key-value pair, and the conversion function name is the value of the target key-value pair.

9. A code completion device, characterized in that: include: A tree node determination unit, configured to query a pre-generated fault-tolerant syntax tree based on a current input position to determine a corresponding current tree node; A code position determining unit, configured to determine a code position corresponding to the current input position according to an associated tree node corresponding to the current tree node; a variable type determination unit, configured to determine a target variable type corresponding to the current input content according to the code position; a recommendation variable determining unit, configured to obtain a recommendation list corresponding to the current input content, and extract a recommendation variable from the recommendation list; a variable type comparison unit, configured to compare the current variable type of the recommended variable with the target variable type; a type conversion function determining unit, configured to query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function when the current variable type is inconsistent with the target variable type; The type conversion function completion unit is configured to combine the recommended variable with a target type conversion function to obtain a target expression, and replace the recommended variable in the recommendation list with the target expression.

10. A code completion device, comprising: one or more processors; A storage device configured to store one or more programs, when the one or more programs are executed by the one or more processors, enables the one or more processors to implement the code completion method described in any one of claims 1-8.

11. A non-volatile storage medium storing computer executable instructions, wherein the computer executable instructions are configured to execute the code completion method according to any one of claims 1 to 8 when executed by a computer processor.

Citation Information

Patent Citations

  • A method and a device for realizing code completion based on ace expansion

    CN109918064A

  • Code editing method and device, server, user equipment and storage medium

    CN112817583A

  • Code completion method and device, equipment and storage medium

    CN117632106A

  • Character recommending method and apparatus, and computer device and storage medium

    US20210294432A1