Code file processing method and device, electronic equipment and storage medium
By constructing an abstract syntax tree and generating code information indicator objects, the problem of users having difficulty understanding complex code structures is solved, achieving accurate function-level annotation and efficient code processing.
Patent Information
- Application Number
- CN202511101343.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-06
- Publication Date
- 2025-11-21
AI Technical Summary
In existing technologies, when integrated development environments (IDEs) handle complex code structures, users often find it difficult to understand function content and contextual logic, resulting in low code processing efficiency.
By acquiring the initial code file, constructing an abstract syntax tree, filtering out target function nodes that meet preset conditions, extracting function attribute information from these nodes and storing it in a custom data structure, generating code information indicator objects, and setting response instructions to achieve an intuitive association between the function-level structure and the editor interface.
It enables in-depth analysis of complex code structures and real-time structure backtracking, improving code processing efficiency and maintenance convenience. Users can quickly understand function content and contextual logic, reducing errors.
Smart Images

Figure CN120994204A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, specifically to the field of code compilation technology for software development, and particularly to a code file processing method, processing device, electronic device, and storage medium. Background Technology
[0002] As development projects grow in scale and system structures become increasingly complex, code often involves intricate relationships such as interactions between multiple modules, nested hierarchies, and cross-file calls. Maintaining such code requires users not only to quickly identify the definitions and calling relationships of functions, classes, and variables, but also to accurately grasp the functional boundaries, execution order, and interaction logic between these elements in order to promptly identify and fix potential defects, thereby ensuring development efficiency and software quality.
[0003] In existing technologies, integrated development environments (IDEs) typically use a parsing approach where the entire file is the smallest processing unit. When a user adds, modifies, or deletes a source file in the editor, the file must be reloaded, and analysis steps such as lexical analysis, syntax analysis, structure recognition, and symbol table reconstruction must be performed sequentially. However, the analysis results of this approach mainly remain at the symbolic level. Users cannot understand the internal logic of the current function and its interaction with other code units based on the existing parsing results. This leads to insufficient understanding of the function's intent and scope of influence, easily causing misunderstandings and resulting in reduced code processing efficiency.
[0004] Therefore, there is an urgent need for a code file processing method to solve the technical problem of low code processing efficiency caused by users' difficulty in understanding function content and context logic when faced with complex code structures. Summary of the Invention
[0005] This application provides a code file processing method, processing device, electronic device, and storage medium to improve code processing efficiency.
[0006] In a first aspect, embodiments of this application provide a code file processing method, including:
[0007] Obtain the initial code file;
[0008] Based on the initial code file and the preset parser, construct an abstract syntax tree;
[0009] A traversal operation is performed on the abstract syntax tree to determine multiple target function nodes that meet preset conditions, and function attribute information is extracted from the target function nodes and stored in a custom data structure;
[0010] Create a corresponding code information indicator object for each target function node, and set a corresponding code display range for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure.
[0011] In response to a user's click on a response instruction in the code information indicator object, the associated function attribute information is extracted from the custom data structure corresponding to the response instruction and then displayed.
[0012] In one possible implementation, constructing the abstract syntax tree based on the initial code file and the preset parser includes:
[0013] Based on the initial code file, determine the corresponding syntax parsing rules;
[0014] The initial code file is input into a preset parser to parse the initial code file according to the syntax parsing rules, thereby obtaining multiple function nodes and the syntactic hierarchy relationship of each function node;
[0015] An abstract syntax tree is constructed based on each function node and its corresponding syntactic hierarchy.
[0016] In one possible implementation, the abstract syntax tree includes multiple function nodes;
[0017] Accordingly, the step of performing a traversal operation on the abstract syntax tree to determine multiple target function nodes that meet preset conditions, and extracting function attribute information from the target function nodes and storing it in a custom data structure, includes:
[0018] The abstract syntax tree is traversed by a recursive traversal function to select function nodes from multiple function nodes that have all function types in a preset function type whitelist and are defined in at least two lines, and determine them as target function nodes;
[0019] Extract function attribute information from the target function node and store it in a custom data structure.
[0020] In one possible implementation, the step of creating corresponding code information indicator objects based on each target function node includes:
[0021] Each target function node is registered with a code information indicator object through a preset code compilation tool;
[0022] Based on the initial code file, an array of code information indicator objects corresponding to each target function node is generated in the code information indicator providing object. The code information indicator object array includes multiple code information indicator objects, and each code information indicator corresponds to a target function node.
[0023] In one possible implementation, the step of responding to a user's click operation on a response instruction in the code information indicator object, extracting associated function attribute information from a custom data structure corresponding to the response instruction, and displaying the function attribute information includes:
[0024] In response to a user's click on a response instruction in the code information indicator object, the response instruction is triggered to extract associated function attribute information from the custom data structure corresponding to the response instruction;
[0025] The code display area is determined based on the function attribute information;
[0026] The function's attribute information is displayed in the code display area.
[0027] In one possible implementation, it also includes:
[0028] In response to the user's deletion operation on the initial code file, the corresponding function node to be deleted in the initial code file is used to obtain the first adjusted code file, and the code information indicator object corresponding to the function node is deleted simultaneously.
[0029] Based on the function node to be deleted, the abstract syntax tree is processed to delete nodes, resulting in a first adjusted abstract syntax tree.
[0030] In one possible implementation, it also includes:
[0031] In response to the user's code addition operation on the initial code file, a new function node is added at the corresponding position in the initial code file to obtain a second adjusted code file;
[0032] The abstract syntax tree is modified by adding nodes based on the new function nodes to obtain a second adjusted abstract syntax tree.
[0033] Secondly, embodiments of this application provide a code file processing apparatus, including:
[0034] The code retrieval module is used to obtain the initial code file;
[0035] The code parsing module is used to construct an abstract syntax tree based on the initial code file and the preset syntax analyzer;
[0036] The function collection module is used to perform traversal operations on the abstract syntax tree, determine multiple target function nodes that meet preset conditions, and extract function attribute information from the target function nodes and store it into a custom data structure;
[0037] The instruction generation module is used to create corresponding code information indicator objects based on each target function node, and to set corresponding code display ranges for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure.
[0038] The code display module is used to respond to the user's click operation on the response command in the code information indicator object, to extract the associated function attribute information from the custom data structure corresponding to the response command, and to display the function attribute information.
[0039] Thirdly, embodiments of this application provide an electronic device, including: a memory and a processor;
[0040] The memory stores computer-executed instructions;
[0041] The processor executes computer execution instructions stored in the memory, causing the processor to perform the first aspect and / or various possible implementations of the first aspect as described above.
[0042] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the first aspect and / or various possible implementations of the first aspect.
[0043] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the first aspect and / or various possible implementations of the first aspect.
[0044] This application provides a code file processing method, processing device, electronic device, and storage medium. By acquiring an initial code file and constructing an abstract syntax tree, this application achieves a comprehensive understanding of the source code structure. It then performs a traversal operation on the abstract syntax tree, filtering multiple target function nodes based on preset conditions. This ensures accurate identification of function units corresponding to the preset conditions from the complete code, reducing processing redundancy and improving the efficiency and accuracy of structure extraction. By extracting function attribute information from the target function nodes and storing it in a custom data structure, a structured mapping relationship between function structure and meta-information is established, providing a unified access interface for subsequent operations. Based on this, corresponding code information indicator objects are generated according to the target function nodes, and corresponding code display ranges are set, realizing an intuitive association between the function-level structure and the editor interface. Each indicator object contains response instruction parameters associated with function attributes, ensuring that user operations accurately correspond to specific function structures, reducing misoperations or context errors. When the user clicks an indicator, the structural information of the target function is quickly extracted from the custom data structure and displayed, achieving a closed loop of real-time structure backtracking and information feedback. As can be seen, by embedding structure-aware indicators in the editor, accurate function-level annotations are achieved, solving the problem of low code processing efficiency caused by users' difficulty in understanding function content and context when faced with complex code structures, thus improving code processing efficiency and maintenance convenience. Attached Figure Description
[0045] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0046] Figure 1 A flowchart illustrating the code file processing method provided in this application embodiment;
[0047] Figure 2 This is a schematic diagram illustrating the interface provided in an embodiment of this application;
[0048] Figure 3 This is a schematic flowchart of a method for deleting code provided in an embodiment of this application;
[0049] Figure 4 This is a flowchart illustrating the method for adding code as provided in an embodiment of this application.
[0050] Figure 5 A schematic diagram of the code file processing device provided in this application;
[0051] Figure 6 A schematic diagram of the structure of the electronic device provided in this application.
[0052] The accompanying drawings have illustrated specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to specific embodiments. Detailed Implementation
[0053] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0054] In existing technologies, development tools typically rely on overall file parsing mechanisms to achieve code structure analysis and editing assistance. Traditional editors primarily analyze at the symbolic level, requiring users to frequently search and switch contexts when reading, understanding, or manipulating functions. This leads to insufficient understanding of function intent and scope of impact, potentially causing misunderstandings and ultimately reducing code processing efficiency.
[0055] Based on the aforementioned technical problems and needs, this application proposes a code file processing method. By acquiring the initial code file and constructing an abstract syntax tree using a preset parser, a deep analysis of the code structure is achieved. On this basis, by traversing the abstract syntax tree, target function nodes that meet preset conditions are selected, and function attribute information is extracted and stored in a unified custom data structure, thereby establishing an accurate mapping relationship between function structure and semantic information. Furthermore, based on these target function nodes, visual code information indicator objects are generated, and a display range corresponding to the function code area is set, allowing the editor to intuitively present operable command entry points near the function structure. Each indicator is bound to a response command, with command parameters associated with function attribute information. When a user triggers a command, the information of the target function is quickly located and extracted based on the parameters and displayed on the interface in a preset format. It is evident that by constructing a function-centric structure recognition and response mechanism, the integration of code structure and user interaction is achieved, improving the efficiency of function-level information access, providing users with a more efficient and flexible code reading and understanding experience, enabling users to quickly grasp function-related information, and thus improving code processing efficiency.
[0056] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0057] Figure 1 This is a flowchart illustrating the code file processing method provided in the embodiments of this application, such as... Figure 1 As shown, the method includes:
[0058] S11, Obtain the initial code file.
[0059] In this embodiment, the purpose of obtaining the initial code file is to read the source code content to be processed from the development environment for subsequent structure parsing and function identification. The initial code file can be the active file currently open in the user's editor, or it can be automatically loaded from a preset path. The type of the initial code file is not limited to a single programming language and can include source files in multiple language formats. Obtaining the initial code file ensures the completeness and accuracy of the input data for subsequent structure analysis, providing a foundation for syntax parsing and function identification.
[0060] S12 constructs an abstract syntax tree based on the initial code file and the preset parser.
[0061] In this embodiment, the preset parser refers to a component used for syntax recognition and structural modeling of source code. This component analyzes the source text according to the syntax rules of the target programming language and outputs a structured representation. The parser can be initialized through static or dynamic loading, adapted to different languages, and can automatically complete language binding at runtime. By inputting the initial code file into the parser and performing lexical segmentation, syntax structure matching, and node classification according to syntax rules, an abstract syntax tree is generated. This abstract syntax tree is an ordered tree-shaped data structure describing the logical structure of the code, with attributes such as node type, hierarchical relationship, and start and end positions. This embodiment achieves a hierarchical representation of structural elements such as functions, statement blocks, and variable declarations in the initial code file by constructing an abstract syntax tree, ensuring accurate identification of the syntax range and structural boundaries without executing the code, providing data support for subsequent function node filtering, attribute extraction, and code information indicator generation. By constructing an abstract syntax tree, a unified structural abstraction of multi-language code can be achieved, enhancing overall versatility and scalability.
[0062] S13, perform a traversal operation on the abstract syntax tree, determine multiple target function nodes that meet the preset conditions, and extract function attribute information from the target function nodes and store it into a custom data structure.
[0063] In this embodiment, the abstract syntax tree (AST) serves as a hierarchical representation of the source code structure, containing multiple function nodes. For example, nodes may include function definitions, method declarations, variable initializations, control flow structures, etc. Traversal operations can be performed recursively to ensure access to all child nodes and their nested structures. During traversal, each node is filtered based on its type information, syntax attributes, and position range to identify function nodes that meet preset conditions as target function nodes. These preset conditions can be adapted and configured according to the syntax features of different languages and expanded as dynamic parameters. This rule-based filtering method accurately locates the desired function structure, reduces invalid nodes from entering subsequent processes, and improves processing efficiency and the relevance of instruction generation. For example, preset conditions may include, but are not limited to: the node type must belong to the function type whitelist (e.g., function definition, method declaration, arrow function, etc.); the source code fragment corresponding to the node must span at least two lines to exclude inline expressions or single-line anonymous functions; and the function must have a resolvable function name and complete structural boundaries.
[0064] After identifying the target function nodes, function attribute information is extracted from each node and stored in a custom data structure to form the basis for subsequent interactions and calls. Function attribute information refers to key metadata related to the function's structure, such as function source code snippets, start and end row and column positions, file path, language type, function name, and whether it is an arrow function. This information can be obtained by reading the node's position information, text range, and semantic tags, and can be supplemented by information such as the source file path and language context. The extraction process must maintain the recognition of the original indentation and syntax structure to ensure the integrity and reusability of the function code text. This custom data structure can be organized using key-value mapping, object arrays, or nested structures to facilitate subsequent on-demand querying, rapid transfer, and reuse. In terms of structure, each target function node must correspond to an independent data unit and be indexed and bound by the function name or a unique identifier. This embodiment, by structuring and storing the target function nodes, provides an accurate and structured data foundation for subsequent code information indicator object generation and user interaction processing. Meanwhile, the unified abstraction of function attribute information also provides a universal interface for cross-language and cross-platform code function indexing and instruction generation, improving overall extensibility and adaptability.
[0065] S14. Create corresponding code information indicator objects for each target function node, and set corresponding code display ranges for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure.
[0066] In this embodiment, a corresponding code information indicator object is created based on each target function node. The code information indicator object is an interactive unit visually displayed in the code editing interface and bound to the function structure. Its function is to provide a clickable entry point at the function's location to trigger subsequent operations, such as code analysis, comment generation, logic explanation, optimization suggestions, or unit test writing. For example, this object is typically presented above or adjacent to the function as a floating label, inline comment, or button, and is loaded and displayed through the interface rendering mechanism provided by the development environment. The process of creating the code information indicator object requires determining its code display range based on the position information extracted from each target function node. The code display range refers to the visible area of the code information indicator in the editor, usually represented by a line range consisting of the start and end line numbers, or identified by the specific cursor position and character offset. When setting the code display range, a strategy of aligning with the function declaration syntax node position can be adopted to ensure that the code information indicator is close to the function structure, reducing misalignment or redundant coverage. This display range will be used for the visual binding of the indicator to the function code, and also for parameter positioning and execution feedback when triggering response commands.
[0067] Each code information indicator object contains a corresponding response instruction, representing a specific action that the user can perform. This response instruction can be defined through the command registration interface of the integrated development environment (IDE) and can be bound to buttons, menu items, or shortcuts in the editor. When the user clicks the code information indicator, the instruction is triggered, and associated parameters are passed to the command processing function. The parameters of the response instruction are associated with function attribute information in a custom data structure; that is, each instruction carries a structured description of its corresponding function, ensuring that the instruction execution has complete context information. Through this parameter association mechanism, an accurate mapping from visual indicators to code structure is achieved, allowing user operations to directly affect the current function scope. This embodiment, by converting the target function node into a code information indicator object and injecting response instructions and parameters, realizes the transformation from static structure recognition to interactive instruction generation, establishes a linkage channel between code structure and user operations, enables function-level adjustments, and enhances automation in the development process.
[0068] S15, in response to the user's click operation on the response instruction in the code information indicator object, extract the associated function attribute information from the custom data structure corresponding to the response instruction, and display the function attribute information.
[0069] In this embodiment, clicking on any code information indicator object triggers the bound response command, immediately executing the corresponding command processing flow. Clicking can be initiated via mouse click, keyboard shortcuts, or command panel selection, essentially activating the editor's command channel. Each response command is bound to its corresponding target function node, thus possessing a clear parameter source and function context. After the response command is triggered, based on the parameter identifier bound to the command, the function attribute information associated with that command is extracted from the previously constructed and stored custom data structure. For example, the extracted function attribute information includes the function source code text, start and end rows / columns, file path, language type, and function structure type, which can be trimmed, assembled, or formatted according to the specific command requirements.
[0070] After extracting function attribute information, the appropriate display method is selected and output based on the instruction type and execution context. Display methods may include, but are not limited to, the following: displaying the function source code as a read-only code block in the editor's embedded area; displaying function summaries, call relationships, or analysis results in a structured format in the sidebar or pop-up window; and directly displaying function header comments in the prompt box. The position and style of the display area can be preset by the instruction template or dynamically adjusted according to user configuration, ensuring that the displayed information does not obscure the original code or interfere with editing operations, thus improving the interactive experience. It is evident that by responding to user clicks, the rapid extraction and display of function attribute information is driven, achieving real-time interaction between the user, function structure, and data content, forming visual feedback at the function level. This allows users to directly obtain the structural information and content overview of the current function without jumping to or searching for the source code, laying the foundation for subsequent logical understanding, manual review, and collaborative communication. It also supports code readability, interactive coherence, and information acquisition efficiency.
[0071] In one embodiment, the construction of the abstract syntax tree in step S12 above is implemented in one way. Based on the above embodiment, it includes:
[0072] S121, Determine the corresponding syntax parsing rules based on the initial code file;
[0073] S122, Input the initial code file into the preset parser to parse the initial code file according to the syntax parsing rules, and obtain multiple function nodes and the syntax hierarchy relationship of each function node;
[0074] S123, construct an abstract syntax tree based on each function node and its corresponding grammatical hierarchy.
[0075] In this embodiment, the programming language used is determined based on the initial code file, and a matching parsing rule is selected accordingly. The parsing rule defines the syntactic characteristics of the language, such as function structure, keywords, statement boundaries, and nesting rules. After determining the parsing rule, the initial code file is submitted as input to a preset parser to perform parsing processing based on the selected rule. The parser is responsible for converting the source code content into a node structure with syntactic meaning. Each line or statement unit is parsed into a syntactic node, along with its corresponding semantic type, start and end positions, and parent-child relationships. At the function level, attention is paid to syntactic units such as function definitions, method declarations, and arrow functions, which are marked as function nodes. During parsing, the parser also identifies the hierarchical position of these function nodes within the entire code, such as whether a function is nested within a class body, scope, or other function body, thereby establishing a syntactic hierarchy.
[0076] Based on obtaining multiple function nodes and their syntactic hierarchical relationships, an abstract syntax tree is further constructed using this structural information. The construction process uses function nodes as the basic unit and syntactic hierarchies as parent-child references, piecing together a hierarchical tree structure layer by layer. Each function node is treated as a branch node in the tree, and its child nodes include structural information such as parameter lists, function bodies, and return types. The root node is typically a code file-level node, representing the structural entry point of the entire file. The abstract syntax tree constructed in this way not only fully preserves the syntactic composition and nesting hierarchy of functions but also provides an efficient data organization form for subsequent traversal, structure recognition, and code scope location. This embodiment achieves structured parsing of source code by constructing a function structure syntax tree, ensuring a function-level structure awareness capability.
[0077] In one specific embodiment, a particular implementation method for obtaining the syntax parsing rules described above is provided. Based on the above embodiment, it includes:
[0078] S1211, Based on the file path of the initial code file, extract the file extension of the initial code file, and search in the preset language lookup table according to the file extension to obtain the normalized name corresponding to the file extension;
[0079] S1212, If a syntax parsing rule corresponding to the specification name is found in the memory cache, then the syntax parsing rule is obtained;
[0080] S1213 If no syntax parsing rule corresponding to the standard name is found in the memory cache, then according to the standard name, the assembly language file path corresponding to the standard name is obtained using a preset concatenation format, the assembly language file in the assembly language file path is loaded, and the syntax parsing rule is generated according to the language assembly file.
[0081] In this embodiment, to achieve unified processing of multiple programming languages, the file extension of the initial code file is first extracted based on its file path. The file extension is a fundamental characteristic identifying the code language type and is usually located at the end of the filename, such as ".js", ".py", or ".cpp". Through string parsing, the last-level filename is extracted from the file path, and its prefix characters are removed to obtain the suffix extension. This step achieves a preliminary mapping between the path information and the code language type through semantic parsing. After obtaining the file extension, its corresponding canonical name is retrieved from a preset language lookup table based on the extension. Obtaining the canonical name ensures the consistency of subsequent syntax parsing rules (i.e., language definitions) in the caching and loading mechanisms, and also provides a foundation for the cross-language processing framework. Next, the memory cache is checked to see if a language definition object corresponding to the canonical name already exists. If the cache is hit, meaning a corresponding language definition object exists, that object is directly reused.
[0082] If a cache miss occurs (i.e., the target language definition object does not yet exist in memory), the corresponding assembly language file path will be located based on the canonical name using a preset path concatenation format. For example, the concatenation format follows the rule "<base directory> / tree-sitter-<canonical name>.wasm", pointing to the syntax module file of a specific language. The corresponding assembly language file will then be loaded from this path. After loading, a new language definition object will be constructed based on the loaded assembly language file and written to the memory cache for subsequent reuse.
[0083] Next, a corresponding parser is created using a language definition object, thereby constructing an abstract syntax tree based on the initial code file and the parser. The parser (such as a tree-sitter incremental parser) identifies syntax nodes, constructs the structure, and performs hierarchical parsing according to the syntax rules provided by the language definition object. This embodiment completes the language binding configuration of the parser, laying the parsing foundation for processing the initial code file and subsequent calls. By combining path mapping, cache reuse, and lazy loading mechanisms, efficient syntax module management and dynamic parser generation are achieved in multi-language processing scenarios, improving overall versatility and response performance.
[0084] Specifically, the abstract syntax tree includes multiple function nodes.
[0085] In this implementation, the abstract syntax tree (API) is a structured representation of the initial code file, composed of multiple nodes with different syntactic functions, including multiple function nodes. Function nodes are the building blocks of the API, typically representing independent executable units such as functions, methods, or arrow functions defined in the code. Each function node is a semantically independent substructure within the API, possessing a complete syntactic scope, typically including the function name, parameter list, function body, return type (if syntactically permissible), and its start and end coordinates. During API construction, all syntactic units satisfying the function structure characteristics are identified and extracted based on syntax parsing rules, and then organized as function nodes and inserted into the tree structure. These function nodes may be at different syntactic levels, such as top-level functions, class methods, nested functions, etc., thus their distribution in the API is hierarchical and diverse. By recording meta-attributes such as parent-child relationships, level depth, and node type for each function node, efficient identification and traversal are achieved in subsequent analysis. As can be seen, the existence of function nodes enables the abstract syntax tree to perform structural analysis based on functions as the basic unit. This facilitates semantic processing, range labeling, or injection of auxiliary information into a specific function segment without affecting the overall code structure. By explicitly including function nodes as components of the abstract syntax tree, function-level grammatical modeling is achieved.
[0086] Accordingly, step S13 above will be further explained here. Based on the above embodiment, it includes:
[0087] S131, the abstract syntax tree is traversed by recursively traversing the function to select all function nodes whose function types are in the preset function type whitelist and are defined in at least two lines from multiple function nodes, and determine them as target function nodes;
[0088] S132, extract function attribute information from the target function node and store it into a custom data structure.
[0089] In this embodiment, a recursive traversal function is used to traverse the abstract syntax tree (API). The purpose is to identify and filter function nodes that meet preset conditions from the API, and further determine them as target function nodes. The recursive traversal function is a depth-first tree structure scanning method that starts from the root node of the API and sequentially visits all its child nodes, performing type checks and condition verifications on each node during the traversal. This traversal process can be implemented using a stack-based call structure, ensuring that all function nodes in the tree can still be fully accessed even in the presence of nested functions or multi-level syntax blocks.
[0090] During the traversal, the first step is to determine whether the function type of the function node appears in the preset function type whitelist. The function type whitelist is a pre-defined set of node types that can identify function structures, typically including but not limited to function definitions, method declarations, arrow functions, and class constructors. This whitelist can be adapted to the programming language being processed and dynamically updated through configuration, thus ensuring compatibility with differences in function structures across multi-language development environments. This setting reduces the misidentification of non-function structure nodes (such as variable declarations and expression statements) as candidate functions, improving the accuracy of target function selection. After identifying a node as a function type, it is also necessary to determine whether the node's code structure meets the condition of at least two lines of definition. The criterion is whether the difference between the start and end line numbers of the function node is greater than or equal to 1. If the function definition occupies only one line, it is discarded. This judgment is used to filter out anonymous functions, inline function expressions, or simple arrow functions, ensuring that subsequent processed nodes have sufficient semantic content and structural context.
[0091] Function nodes that meet the above two conditions are marked as target function nodes. After determining the target function nodes, their function attribute information is extracted and stored in a custom data structure. The extracted content includes the function's original code text, start and end row and column numbers, file path, function name, language type, and indentation structure of the function body. Attribute information extraction can be achieved by mapping the original position information of syntax nodes to the source code, ensuring accuracy and completeness. The custom data structure can be constructed in the form of object arrays, key-value maps, or nested structures. Each target function node corresponds to a data unit and is indexed and bound by a unique identifier. This data structure will be used in subsequent processes to generate code information indicator objects, construct response command parameters, and drive user interaction operations. This embodiment uses preset rules to filter functions and stores function information in a structured form, realizing the extraction of target function fragments from the code structure.
[0092] In one embodiment, the creation of the code information indicator object in step S14 above is specifically implemented as follows. Based on the above embodiment, it includes:
[0093] S141, Register a code information indicator object for each target function node using a preset code compilation tool;
[0094] S142, In the code information indicator providing object, generate an array of code information indicator objects corresponding to each target function node according to the initial code file. The code information indicator object array includes multiple code information indicator objects, and each code information indicator corresponds to a target function node.
[0095] In this embodiment, a code information indicator provider object is registered using a preset code compilation tool based on the target function node. The preset code compilation tool refers to a plugin framework or extension interface provided in the integrated development environment (IDE) to dynamically bind code structures to interactive components. This tool supports registering structure-aware view components on a function node basis, enabling the visualization of operation entry points corresponding to the function structure within the code editor. The code information indicator provider object is a service entity within the preset code compilation tool, used to receive function structure information and generate corresponding indicator instances in the editor interface. Registering the code information indicator provider object can be done using the plugin API; for example, in the VS Code environment, the provider object can be bound to a specific language file type using "languages.registerCodeLensProvider". This registration process occurs after the target function node is identified and processed, dynamically generating function-level interactive indicators, which are then rendered by the editor based on the output of the provider object. During registration, parameters such as the configuration language type, responsive command names, and display strategies can be configured to ensure that the code information indicator (such as CodeLens) display behavior is compatible with the target language and its structural characteristics.
[0096] After registration, the code information indicator provider object generates an array of code information indicator objects corresponding to each target function node based on the initial code file and its function structure information. This process is actively executed by the indicator provider object via callback, with the input being the currently opened initial code file and the previously identified set of target function nodes. A code information indicator object is constructed for each target function node, including but not limited to: the function's position range in the code (start and end line and column numbers), the bound response instruction name, command parameters (such as function attribute information), and optional display title or descriptive text. The generated code information indicator objects are organized into an array structure and returned uniformly to the editor interface. The editor then renders these code information indicator objects as inline interactive tags at the corresponding function structure locations. Each code information indicator object corresponds one-to-one with a target function node, ensuring that each function has an independent operation entry point, supporting separate logic interpretation, comment generation, optimized calls, and other behaviors. This array can be arranged according to the order in which the functions appear in the file to improve the continuity of user browsing and operation. This embodiment establishes a bridge between function structures and interface instruction components by employing a registration mechanism of a pre-defined code compilation tool, achieving the mapping and transformation from target function nodes to editor interactive elements. By organizing these elements into object arrays for batch generation and postback, not only is the efficiency of indicator creation improved, but the interactive performance and consistency between the plugin and the editor are also enhanced, providing users with a well-structured and intuitive code interaction experience.
[0097] In one embodiment, step S15 is further described below. Based on the above embodiment, it includes:
[0098] S151, in response to the user's click operation on the response instruction in the code information indicator object, trigger the response instruction to extract the associated function attribute information from the custom data structure corresponding to the response instruction;
[0099] S152, Determine the code display area based on function attribute information;
[0100] S153 displays function attribute information in the code display area.
[0101] In this embodiment, when a user clicks on any code information indicator object in the code editor, the click event is immediately responded to, triggering the response command bound to that indicator. The response command is a pre-defined operation command of the code information indicator object, used to drive subsequent processing logic when user interaction occurs. This triggering operation can be implemented through mouse clicks, keyboard shortcuts, or command panel invocation. The corresponding indicator object is located based on the event source, and the bound command handler is invoked. Simultaneously with the command being triggered, identification information is extracted from the parameters carried in the response command to locate the target function node corresponding to the command in a custom data structure. The custom data structure stores the function attribute information of all target function nodes, using a unique identifier (such as function name, line number, or internal ID) as the search key. During the search process, the function identifier passed in the command is compared with the entries in the custom data structure to locate the corresponding function entry and extract its complete attribute information.
[0102] After extracting the function attribute information, the code display area is determined based on this information. The code display area refers to the visual area in the editor used to display relevant function information; its position and extent can be calculated based on the function's start line number, end line number, and column position. The display area can take the form of a floating window above the code line, a floating panel in the sidebar, an embedded code block, or a pop-up tooltip, etc., with the specific display method determined by command configuration or strategy. Determining the display area requires considering the editor's current visible range, code folding status, and user operation context to ensure that the displayed information closely corresponds to the original function content and does not obscure the main editing content.
[0103] Once the display area is determined, the system loads and displays the extracted function attribute information within that area. The displayed content may include a read-only view of the function source code snippet, a function structure summary, AI-generated natural language explanations, call graphs, or analysis results. The display format can employ syntax highlighting, folded code blocks, key-value pairs, etc., to improve readability. The display process can support a dynamic refresh mechanism; when the user clicks multiple indicators consecutively, the display area can automatically update and focus on the information of the current target function. This embodiment demonstrates the process from user operation to function information extraction and then to visual display, ensuring that users can instantly obtain detailed information about the function structure and content with minimal interaction cost, improving function-level understanding efficiency, maintenance convenience, and code readability, thereby enhancing code processing efficiency.
[0104] For example, Figure 2 This is a schematic diagram illustrating the interface provided in an embodiment of this application. In the VS Code environment, when a user opens a .js file, the plugin first calls the tree-sitter incremental parser to parse the entire file and locate three functions: A, B, and C. The parsing results are filtered using a whitelist and then written into a custom data structure. Subsequently, the plugin inserts a row of Code Lens-style buttons above the declaration line for each function block, including: inline comments, function comments, code explanations, generate unit tests, and code optimization. The left and right distances of the buttons automatically adapt to the editor's character width, ensuring alignment with the function body at any indentation level.
[0105] When a user wants to obtain specific information about a function, they can directly click the corresponding command. This will extract the relevant attribute information from the custom data structure of the target function node, making it easier for the user to view and understand, especially for code blocks they are unfamiliar with. For example, when a user clicks the code explanation button above function A, the plugin first reads the unique function identifier carried in the button parameters and retrieves the attribute entries of function A in the custom data structure, quickly obtaining the complete source code, start and end line numbers, file path, language type, and call context summary. Subsequently, the plugin assembles the request in a unified JSON format, including metadata such as source code text, function signature, dependency list, and the current editor cursor position, and forwards it to the backend preset function parsing model (such as an AI model) through a preset local proxy. After receiving the request, the model performs code analysis and generates a natural language explanation covering the function's main logic, inputs and outputs, key branches, and boundary conditions. After the response is returned, the plugin pops up an explanation panel on the right side of the editor based on the function's starting line number: the top displays the function signature, the middle presents a step-by-step logical explanation in list form, and the bottom lists the improvement suggestions and reference links provided by the model. Meanwhile, to keep the workspace tidy, the plugin provides two options in the upper right corner of the panel: "Copy to Comment" and "Pin to Sidebar." Selecting "Copy to Comment" inserts a formatted comment block above the function A declaration line; selecting "Pin to Sidebar" moves the explanation content to the sidebar and anchors it to the file path and line number, allowing for synchronized highlighting of the corresponding lines when scrolling through the code. Throughout the entire interaction, the abstract syntax tree only updates the rows and columns of the function A node, without requiring a complete reconstruction.
[0106] Figure 3 This is a schematic flowchart illustrating a method for deleting code as provided in an embodiment of this application. Based on the above embodiments, as follows... Figure 3 As shown, it includes:
[0107] S31, in response to the user's deletion operation on the initial code file, the corresponding function node to be deleted in the initial code file is obtained to get the first adjusted code file, and the code information indicator object corresponding to the function node is deleted simultaneously;
[0108] S32, perform node deletion processing on the abstract syntax tree according to the function node to be deleted, and obtain the first adjusted abstract syntax tree.
[0109] In this embodiment, when a user performs a deletion operation on the initial code file in the editor, the system automatically detects whether the operation involves the deletion of a complete function structure. Specifically, by listening to the editing event stream and combining the start and end positions of the function nodes, it determines whether the deleted text range covers an identified target function node. If the deleted code range is found to be consistent with or highly overlapping with the position information of a function node in a custom data structure, the function is identified as the node to be deleted. This identification process can be implemented using syntax range comparison to ensure accurate positioning and reduce false positives for non-structured deletion operations. Once it is confirmed that the deletion operation involves the target function node, the content of the initial code file is updated to remove the corresponding function code segment. This operation can be completed through the editor's document modification interface. The deletion range is limited by the start and end line numbers of the function node, ensuring that the entire function is cleared without leaving any residual fragments. After deletion, the first adjusted code file is obtained, indicating that the current version no longer contains the source code content of the target function.
[0110] Simultaneously, the visualization components associated with the function node will also be updated, i.e., the corresponding code information indicator object will be deleted. Since each code information indicator object is bound to a specific function node, the indicator instance matching the function can be quickly located through the mapping relationship in the custom data structure and removed from the current editor interface. This ensures that when a function is deleted, its related interactive entry points are also cleared synchronously, reducing the appearance of residual interface elements pointing to non-existent code. After completing the file content and indicator updates, a node deletion operation will be performed on the original abstract syntax tree to obtain the first adjusted abstract syntax tree. This operation can be achieved through a partial update in the original tree structure. In the scenario of partial updates, by marking the function node to be deleted and removing its reference relationship in the tree, the node and its substructure are removed from the syntax tree, ensuring that the adjusted syntax tree can accurately reflect the structural state of the current code file. This embodiment achieves structured perception and response to function-level deletion operations by synchronously processing code content, structural indexes, and interactive components, ensuring consistency between the user's visual layer and the semantic analysis layer in editing operations. This not only improves the accuracy and automation of function management, but also provides a reliable structural basis for subsequent function-level updates.
[0111] Figure 4 This is a schematic flowchart illustrating the method for adding code as provided in an embodiment of this application. Based on the above embodiments, as follows... Figure 4 As shown, it includes:
[0112] S41, in response to the user's code addition operation on the initial code file, add a new function node at the corresponding position in the initial code file to obtain the second adjusted code file;
[0113] S42, based on the new function nodes, perform node addition processing on the abstract syntax tree to obtain the second adjusted abstract syntax tree.
[0114] In this embodiment, when a user adds code to the initial code file, the system automatically detects whether a new function structure exists and performs structural identification and update processing on the added content. This addition operation typically involves the user inserting a new function definition into a blank area of the file, after an existing function, or within a module. By listening to content change events in the editor and considering the start and end positions of the added text, the system determines whether the added area constitutes a complete function structure. The criteria include whether it contains function keywords, parameter parentheses, function body identifiers (such as curly braces or indentation blocks), and the syntax pattern of the function declaration header. If a code segment matching the characteristics of a function structure is identified, it is determined to be a new function node. After identifying the new function node, the added function structure is integrated into the current code text, forming a second adjusted code file. This adjusted file retains the structure and semantics of the original code and includes the newly added function definition, representing the latest file state after the user's editing. During the file update process, the position information of the newly added function node is recorded for subsequent syntax tree incremental processing and interface update operations.
[0115] Subsequently, based on the new function node, the existing abstract syntax tree is modified to add nodes, resulting in a second adjusted abstract syntax tree. This process can be accomplished through an incremental syntax tree construction mechanism, which inserts new syntax nodes only on top of the original abstract syntax tree without rebuilding the entire tree. Based on the starting position of the new function node, its insertion position in the syntax tree is determined, and it is correctly mounted into the tree structure according to its syntax hierarchy and scope relationship. During node mounting, meta-attributes such as function type, structure scope, and parent-child node relationships are recorded simultaneously to ensure the integrity and traversability of the syntax tree structure. To improve processing accuracy and efficiency, a syntax parser can be used to perform local parsing on the newly added function fragments, extracting their syntax node information before inserting them into the existing syntax tree. This embodiment achieves structured awareness and semantic integration of function-level new behavior by automatically identifying user-added function structures and incorporating them into the syntax tree. This ensures that new code is not only reflected at the file level but also registered and updated in the abstract syntax structure, improving responsiveness and structural consistency in incremental editing scenarios.
[0116] Building upon the above embodiments, another embodiment is provided here: the response command can not only be used to display function attribute information but also further drive integration with the AI model service to achieve automatic analysis and generation of improved function structures based on the function structure. Specifically, when a user clicks on a code information indicator object, after extracting the function attribute information, it is submitted as an input request to the pre-configured AI model service. The function attribute information is organized into standard call parameters using a unified format to ensure the integrity and consistency of the data received by the model. The AI model service can be deployed as a local inference engine or a remote API interface. Its internally configured model has code understanding and generation capabilities, such as supporting function annotation generation, logic explanation, performance optimization suggestions, refactoring code suggestions, or automatic completion of unit test code. After sending the function attribute information, the system waits for the model to return the processing result. The type and structure of the returned result can be configured according to the command type. For example, if the user triggers the "generate function annotation" command, a natural language annotation text will be obtained; if the user triggers the "function logic optimization" command, an optimized version that can replace the current function will be returned.
[0117] After the model returns the processing result, the corresponding region of the function in the source file is located based on the start and end row and column information of the original function node, and the subsequent processing method is determined according to the instruction type. If the returned content is a replacement code snippet, the original function body will be automatically replaced with the new function body returned by the model in the editor, realizing code updates; if the returned result is explanatory text or comment information, it can be inserted above the function declaration or displayed in read-only mode in the side area of the editor. The entire process builds a closed loop between user operation and model response, allowing users to obtain code enhancement suggestions with minimal interaction cost. This implementation improves the efficiency of code understanding, maintenance, and refactoring by connecting response instructions with AI model services. At the same time, the function-level structure awareness and position positioning mechanism ensures that the AI result is accurately updated to the corresponding position, reducing context mismatch and structural chaos. This has good scalability and can support multiple models, multiple languages, and multiple types of tasks.
[0118] Figure 5 A schematic diagram of the structure of the code file processing device provided in this application is shown below. Figure 5 As shown, the processing device 50 provided in this embodiment includes:
[0119] Code acquisition module 501 is used to acquire the initial code file;
[0120] The code parsing module 502 is used to construct an abstract syntax tree based on the initial code file and the preset parser;
[0121] The function collection module 503 is used to perform traversal operations on the abstract syntax tree, determine multiple target function nodes that meet preset conditions, and extract function attribute information from the target function nodes and store it into a custom data structure.
[0122] The instruction generation module 504 is used to create corresponding code information indicator objects based on each target function node, and to set the corresponding code display range for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure.
[0123] The code display module 505 is used to respond to the user's click operation on the response command in the code information indicator object, to extract the associated function attribute information from the custom data structure corresponding to the response command, and to display the function attribute information.
[0124] The processing device 50 provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0125] Figure 6 A schematic diagram of the structure of the electronic device provided in this application. Figure 6 As shown, the electronic device 60 provided in this embodiment includes at least one processor 601 and a memory 602. Optionally, the electronic device 60 further includes a communication component 603. The processor 601, memory 602, and communication component 603 are connected via a bus 604.
[0126] In a specific implementation, at least one processor 601 executes computer execution instructions stored in memory 602, causing at least one processor 601 to perform the above-described method.
[0127] The specific implementation process of processor 601 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.
[0128] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0129] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage device.
[0130] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.
[0131] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0132] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.
[0133] The aforementioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0134] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and the readable storage medium can exist as discrete components in the device.
[0135] The division of units is merely a logical functional division; in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual couplings, direct couplings, or communication connections may be indirect couplings or communication connections through interfaces, devices, or units, and may be electrical, mechanical, or other forms. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0136] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present invention.
[0137] Finally, it should be noted that other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein, and is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
Claims
1. A method for processing code files, characterized in that, include: Obtain the initial code file; Based on the initial code file and the preset parser, construct an abstract syntax tree; A traversal operation is performed on the abstract syntax tree to determine multiple target function nodes that meet preset conditions, and function attribute information is extracted from the target function nodes and stored in a custom data structure; Create a corresponding code information indicator object for each target function node, and set a corresponding code display range for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure. In response to a user's click on a response instruction in the code information indicator object, the associated function attribute information is extracted from the custom data structure corresponding to the response instruction and then displayed.
2. The method according to claim 1, characterized in that, The step of constructing an abstract syntax tree based on the initial code file and the preset parser includes: Based on the initial code file, determine the corresponding syntax parsing rules; The initial code file is input into a preset parser to parse the initial code file according to the syntax parsing rules, thereby obtaining multiple function nodes and the syntactic hierarchy relationship of each function node; An abstract syntax tree is constructed based on each function node and its corresponding syntactic hierarchy.
3. The method according to claim 2, characterized in that, The abstract syntax tree includes multiple function nodes; Accordingly, the step of performing a traversal operation on the abstract syntax tree to determine multiple target function nodes that meet preset conditions, and extracting function attribute information from the target function nodes and storing it in a custom data structure, includes: The abstract syntax tree is traversed by a recursive traversal function to select function nodes from multiple function nodes that have all function types in a preset function type whitelist and are defined in at least two lines, and determine them as target function nodes; Extract function attribute information from the target function node and store it in a custom data structure.
4. The method according to claim 1, characterized in that, The step of creating corresponding code information indicator objects based on each target function node includes: Each target function node is registered with a code information indicator object through a preset code compilation tool; Based on the initial code file, an array of code information indicator objects corresponding to each target function node is generated in the code information indicator providing object. The code information indicator object array includes multiple code information indicator objects, and each code information indicator corresponds to a target function node.
5. The method according to claim 1, characterized in that, The process of responding to a user's click on a response command in the code information indicator object, extracting associated function attribute information from a custom data structure corresponding to the response command, and displaying the function attribute information includes: In response to a user's click on a response instruction in the code information indicator object, the response instruction is triggered to extract associated function attribute information from the custom data structure corresponding to the response instruction; The code display area is determined based on the function attribute information; The function's attribute information is displayed in the code display area.
6. The method according to claim 1, characterized in that, Also includes: In response to the user's deletion operation on the initial code file, the corresponding function node to be deleted in the initial code file is used to obtain the first adjusted code file, and the code information indicator object corresponding to the function node is deleted simultaneously. Based on the function node to be deleted, the abstract syntax tree is processed to delete nodes, resulting in a first adjusted abstract syntax tree.
7. The method according to any one of claims 1 to 6, characterized in that, Also includes: In response to the user's code addition operation on the initial code file, a new function node is added at the corresponding position in the initial code file to obtain a second adjusted code file; The abstract syntax tree is modified by adding nodes based on the new function nodes to obtain a second adjusted abstract syntax tree.
8. A code file processing device, characterized in that, include: The code retrieval module is used to obtain the initial code file; The code parsing module is used to construct an abstract syntax tree based on the initial code file and the preset syntax analyzer; The function collection module is used to perform traversal operations on the abstract syntax tree, determine multiple target function nodes that meet preset conditions, and extract function attribute information from the target function nodes and store it into a custom data structure; The instruction generation module is used to create corresponding code information indicator objects based on each target function node, and to set corresponding code display ranges for each code information indicator. Each code information indicator object contains a corresponding response instruction, and the parameters of the response instruction are associated with the function attribute information in the custom data structure. The code display module is used to respond to the user's click operation on the response command in the code information indicator object, to extract the associated function attribute information from the custom data structure corresponding to the response command, and to display the function attribute information.
9. An electronic device, characterized in that, include: Memory, processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 7.