Code completion method, device, equipment and storage medium
By determining the variable type based on the fault-tolerant syntax tree in the code editor and automatically converting it, the problem of variable type mismatch in the code editor is solved, and code writing efficiency is improved.
Patent Information
- Application Number
- CN202411548025.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-01
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-11-01
AI Technical Summary
Existing code editors may have type mismatch issues when recommending variables, requiring users to make manual adjustments, affecting code writing efficiency.
By determining the current tree node based on the fault-tolerant syntax tree query, judging the code position corresponding to the input position, and determining the target variable type based on the code position, the variables in the recommended list are obtained, and the type conversion function is queried using the preset mapping list. The recommended variables are combined with the target type conversion function to automatically adjust the variable type.
It automatically converts recommended variables into the variable types required by users without the need for manual adjustment by users, ensuring the smoothness of the code completion function and improving code writing efficiency.
Smart Images

Figure CN119668578B_ABST
Abstract
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 essential 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 typically provides features such as syntax highlighting, auto-completion, and code folding to improve programming efficiency and code readability. Code completion can automatically display possible options as the user enters a portion of code, eliminating the need for the user to type the entire code.
[0003] Existing code editors can recommend variables based on user input. However, the recommended variables may have type mismatches, requiring users to manually adjust them, affecting the user's smooth use of 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 user's fluency 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 user's fluency 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, comprising:
[0006] Based on the current input position, the 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 from the recommendation list;
[0010] Comparing the current variable type of the recommended variable with the target variable type;
[0011] In the case 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;
[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 adjacent variable names and left parentheses exist in the associated tree node corresponding to the current tree node;
[0015] When adjacent variable names and left parentheses exist simultaneously 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 a 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 call 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, determining whether the parent node corresponding to the current tree node is a left square bracket;
[0022] In a 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] If the parent node corresponding to the current tree node is not a left square bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;
[0028] In a 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 within 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] Based on the original variable type, the expected variable type, and the conversion function name, a target key-value pair is constructed and stored in a preset mapping list, where 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, when the current variable type is inconsistent with the target variable type, query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function;
[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 position determination unit includes:
[0044] The first position determination module is configured as follows:
[0045] Determine whether adjacent variable names and left parentheses exist in the associated tree node corresponding to the current tree node;
[0046] When adjacent variable names and left parentheses exist simultaneously 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 a function parameter.
[0047] Optionally, a variable type determination unit includes:
[0048] The first type determination module is configured to:
[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 call 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, determining whether the parent node corresponding to the current tree node is a left square bracket;
[0055] In a 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] If the parent node corresponding to the current tree node is not a left square bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;
[0063] In a 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 within 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 based on the associated tree node corresponding to the current tree node; a target variable type corresponding to the current input content is determined based on 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 based on the current variable type and the target variable type to determine a target type conversion function; the recommended variable and the target type conversion function are combined 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 based on the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged. 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 by the current input position. 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 smoothness of the user's use of 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 the code position corresponding to the current input position provided in an embodiment of the present application;
[0078] Figure 4 A flowchart of another specific implementation process of determining the code position corresponding to the 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 in 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 in 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 in 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] Figure 9 A structural block diagram of a code completion device provided in an embodiment of the present application;
[0084] Figure 10 A schematic diagram of the structure of a code completion device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0085] The following is a further detailed description of the embodiments of the present application in conjunction with the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely for the purpose of explaining the embodiments of the present application, and are not intended to limit the embodiments of the present application. It should also be noted that, for ease of description, the accompanying drawings only illustrate portions of the embodiments of the present application, rather than all structures.
[0086] The terms "first," "second," and the like in the specification and claims of this application are used to distinguish similar objects, and are not used to describe a specific order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate, so that the embodiments of this application can be implemented in an order other than that illustrated or described herein, and that the objects distinguished by "first," "second," and the like are generally of the same type, and do not limit the number of objects; for example, the first object can be one or more. In addition, the term "and / or" in the specification and claims refers to at least one of the connected objects, and the character " / " generally indicates that the objects connected are in an "or" relationship.
[0087] In the code completion method provided in the embodiment of the present application, the execution entity of each step can be a computer device, which refers to any electronic device with data calculation, processing and storage capabilities, such as a terminal device such as a PC (Personal Computer), or a server and other devices. The embodiment of the present application does not limit this.
[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 the corresponding current tree node.
[0090] Among them, the code completion method provided in 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 lexical analysis can be to convert the existing code content into lexical units, corresponding to the smallest grammatical unit in the code, such as keywords, identifiers, operators, delimiters, etc. Developers can define lexical rules based on the programming language and development requirements of actual applications. 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 of lexical units into a syntax tree according to the grammatical rules. 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 location can be the code statement, function, data structure, etc., where the current input location is located. The associated tree node can be the parent node, sibling node, etc., corresponding to the current tree node. Because the syntax elements of the tree nodes corresponding to different code locations are different, the code location corresponding to the current input location can be determined based on the associated tree nodes corresponding to the current tree node and the syntax elements corresponding to the associated tree nodes.
[0093] In an optional embodiment, a specific implementation process of determining the code position corresponding to the current input position based on 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 the code position corresponding to the 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 node, brother node, 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 ":=". Obviously, "GetSpeed" and "(" are adjacent, and "(" and "Speed_Scale". Therefore, it is possible to determine whether the current input position is inside the function parameters by judging whether adjacent variable names and left parentheses exist at the same time in the associated tree nodes corresponding to the current tree node.
[0096] Step S1022: When adjacent variable names and left parentheses exist simultaneously 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 adjacent variable names and left parentheses exist simultaneously 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: 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 square bracket.
[0101] According to array structure characteristics, if the current input position is within the array index, the parent node corresponding to the current tree node must be a left square bracket. For example, for the array expression arr[Δ], if Δ represents the current input position, its parent node is "[". Therefore, by determining whether the parent node corresponding to the current tree node is a left square bracket, we can determine whether the current input position is within the array index.
[0102] Step S1024: When the parent node corresponding to the current tree node is a left square 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 adjacent variable names and left parentheses exist simultaneously 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: 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 square bracket.
[0107] Step S1024: When the parent node corresponding to the current tree node is a left square 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 square bracket, determine whether there is an assignment symbol in the associated tree node corresponding to the current tree node.
[0109] According to the characteristics of assignment statements, if the current input position is within an assignment statement, then the associated tree node corresponding to the current tree node must contain an assignment symbol. For example, for the assignment statement: person.name.length:=Δ, if Δ represents the current input position, then the corresponding associated tree node will contain ":=". Therefore, by determining whether the current input position is within an assignment statement, we can determine whether the current input position is within the assignment statement.
[0110] Step S1026: When 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.
[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 based on 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 call 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 parameters, then 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 calling 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 parameters. 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 the 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 based on 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's worth noting that if the code position is within an array index, the recommended variable corresponding to the current input position should be of the same type as the array index. Therefore, you can first determine the second variable type corresponding to the array index, which can be considered the target variable type for the current input content. For example, for the array arr[Δ], if Δ represents the current input position, then the variable type corresponding to Δ should be int, and the target variable type for the current input content should also be int.
[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 the 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 based on the code position are as follows:
[0123] Step S1036: When the code position is inside an assignment statement and the current input position is 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 the same type as the variable on the left side of the assignment symbol. Therefore, you can first determine the third variable type corresponding to the variable on the left side of the assignment symbol. This third variable type can be considered the target variable type corresponding to the current input content. The variable on the left side of the assignment symbol can be a simple variable or a member variable. For example, for the assignment statement: person.name.length:=Δ, you can analyze the type of each variable in the variable expression person.name.length from left to right. If the variable person is of type person, the member variable name is of type string, and the member variable length is of type int, then the variable type corresponding to Δ should be int, that is, the target variable type corresponding to the current input content should be int.
[0126] As described above, by determining the target variable type corresponding to the current input content based on 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] Where the current input content is incomplete input character text, based on a pre-generated fault-tolerant syntax tree, 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, based on the specific needs of the user, it is not necessary to perform type conversion on the 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's worth noting that if the current variable type doesn't match the target variable type, the recommended variable can't be directly applied to the current input position and requires type conversion. For example, if the current input is "len," the recommended variable is "length." "Length" is a local variable of type real, and the target variable type is int. Therefore, based on the current variable type (real) and the target variable type (int), a preset mapping list is searched to determine the target type conversion function is real_to_int(). It's understandable that real_to_int() converts the input real variable to an int variable.
[0133] Step S107: Combine the recommended variables with the target type conversion function to obtain a target expression, and replace the recommended variables 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 target expression obtained by the combination 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 based on the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged. 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 by the current input position. 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 smoothness of the user's use of the code completion function and improving the user's code writing efficiency.
[0136] Figure 8 This is 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 the original variable type, the expected variable type, and the 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 based on 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: Based on the current input position, a pre-generated fault-tolerant syntax tree is queried 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 variables with the target type conversion function to obtain a target expression, and replace the recommended variables 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 convenient to quickly query the corresponding type conversion function when performing type conversion on the recommended variable, thereby improving conversion efficiency.
[0150] Figure 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. Figure 9 As shown, the code completion device specifically includes:
[0151] The 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 is 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 determining unit 103 is configured to determine a target variable type corresponding to the current input content according to the code position;
[0154] The recommendation variable determining unit 104 is configured to obtain a recommendation list corresponding to the current input content and extract the recommendation variables in 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 configured to, when the current variable type is inconsistent with the target variable type, query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function;
[0157] The type conversion function completing 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 based on the associated tree node corresponding to the current tree node, the target variable type corresponding to the current input content can be accurately judged. 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 by the current input position. 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 smoothness of the user's use of 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 adjacent variable names and left parentheses exist in the associated tree node corresponding to the current tree node;
[0162] When adjacent variable names and left parentheses exist simultaneously 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 a function parameter.
[0163] In a possible embodiment, the variable type determination unit 103 includes:
[0164] The first type determination module is configured to:
[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 call 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, determining whether the parent node corresponding to the current tree node is a left square bracket;
[0171] In a 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] If the parent node corresponding to the current tree node is not a left square bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node;
[0179] In a 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 within 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] Figure 10 A schematic diagram of the structure of a code completion device provided in an embodiment of the present application is shown as follows: Figure 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. Figure 10 In the embodiment, a processor 201 is used as an example; the processor 201, the memory 202, the input device 203 and the output device 204 in the device can be connected by a bus or other means. Figure 10 The example of the connection via bus is taken as an example. 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, are 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 the current input position to determine the corresponding current tree node; determining the code position corresponding to the current input position based on the associated tree node corresponding to the current tree node; determining the target variable type corresponding to the current input content based on 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 based on 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-ROMs, floppy disks, 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 the first computer system in which the program is executed, or may be located in a different second computer system that 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 an embodiment of the present application is not limited to the above code completion method, and its computer-executable instructions 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 scope of protection of the embodiments of this application.
[0194] It should be noted that the numbering of each step in this solution is only used to describe the overall design framework of this solution, 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 solution, it belongs to the protection scope of this solution, and the order of precedence in the form of text when describing is not an exclusive limitation on the specific implementation process of this solution. Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. In a typical configuration, the computing device includes one or more processors (CPU), input / output interface, network interface and memory. The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (flash RAM). The memory is an example of a computer-readable medium.
[0195] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0196] Note that the above are only preferred embodiments of the present invention and the technical principles employed. Those skilled in the art will appreciate that the present invention is not limited to the specific embodiments herein, and that various obvious changes, readjustments, and substitutions are possible for 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 detail through the above embodiments, the present invention is not limited to the above embodiments and may include many other equivalent embodiments without departing from the scope of the present invention. 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, the 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 from the recommendation list; Comparing the current variable type of the recommended variable with the target variable type; In the case 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; Combining the recommended variables with a target type conversion function to obtain a target expression, and replacing the recommended variables in the recommendation list with the target expression; The step of 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 adjacent variable names and left parentheses exist in the associated tree node corresponding to the current tree node; When an adjacent variable name and a left parenthesis simultaneously exist in the associated tree node corresponding to the current tree node, determining that the code position corresponding to the current input position is inside a function parameter; The step of 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 call expression, and determining a first variable type corresponding to the internal parameter; Determining the first variable type as the target variable type corresponding to the current input content; 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.
2. The code completion method according to claim 1, wherein: Also includes: If there are no adjacent variable names and left parentheses in the associated tree node corresponding to the current tree node, determining whether the parent node corresponding to the current tree node is a left square bracket; In a 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.
3. The code completion method according to claim 2, wherein: The determining of 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.
4. The code completion method according to claim 2, wherein: Also includes: If the parent node corresponding to the current tree node is not a left square bracket, determining whether there is an assignment symbol in the associated tree node corresponding to the current tree node; In a 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.
5. The code completion method according to claim 4, characterized in that: The determining of the target variable type corresponding to the current input content according to the code position includes: When the code position is within 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.
6. 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, when the current variable type is inconsistent with the target variable type, query a preset mapping list according to the current variable type and the target variable type to determine a target type conversion function; a type conversion function completion unit, 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; The code position determination unit includes: The first position determination module is configured as follows: Determine whether adjacent variable names and left parentheses exist in the associated tree node corresponding to the current tree node; When an adjacent variable name and a left parenthesis simultaneously exist in the associated tree node corresponding to the current tree node, determining that the code position corresponding to the current input position is inside a function parameter; The variable type determination unit includes: The first type determination module is configured to: 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 call expression, and determining a first variable type corresponding to the internal parameter; Determining the first variable type as the target variable type corresponding to the current input content; Among them, also include: Map list building unit, configured as: 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.
7. A code completion device, comprising: one or more processors; A storage device configured to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the code completion method according to any one of claims 1 to 5.
8. 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 5 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