CN-121996527-A - Code positioning method, device, equipment and medium based on multi-level structured annotation
Abstract
The invention relates to the technical field of computers, and provides a code positioning method, device, equipment and medium based on multi-level structured annotation, which can uniformly configure multi-level structured annotation specifications, solve the problem of difficulty in machine analysis, identify annotation information of a source code file and position association information of a corresponding code line, ensure the position binding accuracy of the annotation and the code, align the annotation information to corresponding code structure nodes in an abstract syntax tree according to the position association information, realize accurate alignment of the annotation and the code structure nodes, merge the annotation information corresponding to each code structure node, avoid coarse annotation granularity, respond to positioning requests according to a line-level semantic mark object set and a multi-dimensional index library, and therefore realize accurate and rapid positioning of the code line.
Inventors
- ZHANG ERQING
- SHEN SIYUE
Assignees
- 杭州浩联智能科技有限公司
- 杭州新中大科技股份有限公司
Dates
- Publication Date
- 20260508
- Application Date
- 20260409
Claims (10)
- 1. A code positioning method based on multi-level structured annotation, characterized in that the code positioning method based on multi-level structured annotation comprises: acquiring a source code file generated based on a multi-level structured annotation specification, and identifying annotation information of the source code file and position association information of a corresponding code line; Performing structure analysis on the source code file to obtain an abstract syntax tree, and aligning the annotation information to a corresponding code structure node in the abstract syntax tree according to the position association information to obtain a code structure model; combining annotation information corresponding to each code structure node in the code structure model according to the multi-level structured annotation specification to obtain multi-level annotation hierarchical information; Generating a row-level semantic mark object set according to the multi-level annotation hierarchical information and the code structure model; Responding to a positioning request based on target code triggering, and responding to the positioning request according to the line-level semantic mark object set and the multidimensional index base to obtain a positioned target code line.
- 2. The multi-level structured annotation based code positioning method of claim 1, wherein before the source code file generated based on the multi-level structured annotation specification is obtained, the method further comprises: and acquiring the predefined multi-level structured annotation specification.
- 3. The method for code location based on multi-level structured annotations according to claim 2, wherein the aligning the annotation information to corresponding code structure nodes in the abstract syntax tree according to the location association information, obtaining a code structure model comprises: Traversing each code structure node in the abstract syntax tree, and recording a line number interval of a corresponding code structure in the source code file on each code structure node; determining annotation information corresponding to each code structure node according to the recorded line number interval corresponding to each code structure node and the position association information; Adding annotation information corresponding to each code structure node to the corresponding code structure node to obtain the code structure model; The code structure nodes comprise class nodes, method nodes and row nodes.
- 4. The code positioning method based on multi-level structured annotation according to claim 1, wherein merging annotation information corresponding to each code structure node in the code structure model according to the multi-level structured annotation specification to obtain multi-level annotation hierarchy information comprises: Generating class-level annotation objects, method-level annotation objects and line-level annotation objects corresponding to each code structure node according to the multi-level structured annotation specification; Combining annotation objects corresponding to any row of nodes in the code structure model according to a combination strategy to obtain the multi-level annotation level information; The merging strategy comprises the steps of calculating a union of all the requirement numbers included in each annotation, taking the union as a merged requirement number, taking a module or a domain included in the corresponding method-level annotation object or the line-level annotation object as a merged module or a domain, taking the module or the domain included in the corresponding method-level annotation object or the line-level annotation object as a merged module or a domain when the corresponding method-level annotation object and the line-level annotation object are missing, taking the module or the domain included in the corresponding class-level annotation object as a merged module or a domain, taking the variable list included in the corresponding line-level annotation object as a merged variable list for the variable list included in each annotation, and adding the important declaration variable included in the corresponding method-level annotation object to the merged variable list.
- 5. The multi-level structured annotation-based code positioning method of claim 1, wherein generating a set of line-level semantic mark objects from the multi-level annotation hierarchy information and the code structure model comprises: extracting a basic position field from the code structure model, and extracting a business semantic field from the multi-level annotation hierarchical information; generating a row-level semantic mark object corresponding to each row of codes according to the basic position field and the business semantic field; and generating the row-level semantic mark object set according to the row-level semantic mark object corresponding to each row of codes.
- 6. The multi-level structured annotation-based code positioning method of claim 1, wherein before the responding to the positioning request according to the row-level semantic mark object set and multi-dimensional index library, the method further comprises: configuring a plurality of index dimensions; Constructing the multidimensional index base according to the row-level semantic mark object set and the plurality of index dimensions; The method comprises the steps of establishing an enumeration multi-value field corresponding to each index dimension in a row-level semantic mark object set, establishing an inverted index supporting quick retrieval according to values for the enumeration multi-value field, establishing a key value index supporting equivalent query for a unique mapping field corresponding to each index dimension in the row-level semantic mark object set, establishing a vector index supporting semantic similarity retrieval for a natural language field corresponding to each index dimension in the row-level semantic mark object set; and the multidimensional index base is updated in an increment mode at preset time intervals or when the code change is detected.
- 7. The multi-level structured annotation-based code positioning method of claim 6, wherein responding to the positioning request according to the row-level semantic mark object set and the multi-dimensional index library, and obtaining the positioned object code row comprises: analyzing the positioning request to obtain a request parameter; acquiring a target index dimension from the plurality of index dimensions according to the request parameters; Inquiring in the multi-dimensional index library according to the target index dimension to obtain a target line-level semantic mark object; Extracting code line position information of the target line level semantic mark object; locating the target code line in the target code according to the code line position information; and displaying the target code row according to a visibility display strategy, and synchronously displaying annotation information corresponding to the target code row at a preset position.
- 8. A code positioning device based on multi-level structured annotations, the code positioning device based on multi-level structured annotations comprising: the identifying unit is used for acquiring a source code file generated based on the multi-level structured annotation specification and identifying annotation information of the source code file and position association information of a corresponding code row; The alignment unit is used for performing structure analysis on the source code file to obtain an abstract syntax tree, and aligning the annotation information to a corresponding code structure node in the abstract syntax tree according to the position association information to obtain a code structure model; The merging unit is used for merging annotation information corresponding to each code structure node in the code structure model according to the multi-level structured annotation specification to obtain multi-level annotation hierarchical information; The generation unit is used for generating a row-level semantic mark object set according to the multi-level annotation hierarchical information and the code structure model; and the response unit is used for responding to the positioning request triggered based on the target code, responding to the positioning request according to the row-level semantic mark object set and the multidimensional index base, and obtaining the positioned target code row.
- 9. A computer device, the computer device comprising: And a processor executing the instructions stored in the memory to implement the multi-level structured annotation-based code location method of any one of claims 1 to 7.
- 10. A computer-readable storage medium having stored therein at least one instruction for execution by a processor in a computer device to implement the multi-level structured annotation-based code location method of any of claims 1 to 7.
Description
Code positioning method, device, equipment and medium based on multi-level structured annotation Technical Field The present invention relates to the field of computer technologies, and in particular, to a code positioning method, apparatus, device, and medium based on multi-level structured annotation. Background In the existing software development process, code positioning is a very basic and time-consuming task in debugging, demand changing and code review. Developers typically rely on locating code in the following manner: 1. Based on the positioning of full text retrieval, keywords (such as interface paths, field names, error information, etc.) are matched by utilizing full text search functions provided by IDE (INTEGRATED DEVELOPMENT ENVIRONMENT ) or code hosting platform. The common problems of the method include that keywords appear in a large number of files, the result list is huge, certain key business logics have no obvious keywords and are difficult to find through simple searching, and the accurate corresponding relation between a certain row of codes and business requirements and business scenes cannot be directly established; 2. Empirical localization based on functions, class names and comments a business logic location is inferred by reading class names, method names and natural language comments written by the developer. However, the method has the problems that the format is not uniform, the semantics are unstructured and difficult to be reliably analyzed by a machine, the granularity of the annotation is usually remained at class or method level and difficult to be accurate to a certain line or a certain code statement block, and the information such as a business scene, variable semantics, a demand number and the like corresponding to the line of code is difficult to be directly searched from the annotation; 3. The method is characterized by lacking a unified 'multi-level semantic tag' system, wherein the existing document generation tools support adding notes on classes and methods to generate documents, but the method has the following problems that the method is mainly oriented to document presentation and is not accurate in code positioning, and generally only focuses on class and method levels, does not form multi-level inheritable semantic tag structures from class to method to code lines, and lacks fine granularity marking capability on variables, conditional branches, local logic blocks and the like; 4. Business logic is scattered-in large systems, business logic tends to be scattered in multiple classes, multiple methods, or even multiple lines of code, and variable naming does not always correspond intuitively to business meaning. For example, a homonymous variable status may have a completely different meaning in different modules (e.g., including order status, payment status, approval status, etc.). The real business semantics of a certain line of codes are difficult to accurately judge only by searching through variable names or method names. Disclosure of Invention In view of the foregoing, it is desirable to provide a code positioning method, apparatus, device, and medium based on multi-level structured annotation that can accurately position any line of code. A code location method based on multi-level structured annotations, the code location method based on multi-level structured annotations comprising: acquiring a source code file generated based on a multi-level structured annotation specification, and identifying annotation information of the source code file and position association information of a corresponding code line; Performing structure analysis on the source code file to obtain an abstract syntax tree, and aligning the annotation information to a corresponding code structure node in the abstract syntax tree according to the position association information to obtain a code structure model; combining annotation information corresponding to each code structure node in the code structure model according to the multi-level structured annotation specification to obtain multi-level annotation hierarchical information; Generating a row-level semantic mark object set according to the multi-level annotation hierarchical information and the code structure model; Responding to a positioning request based on target code triggering, and responding to the positioning request according to the line-level semantic mark object set and the multidimensional index base to obtain a positioned target code line. A multi-level structured annotation-based code positioning apparatus, the multi-level structured annotation-based code positioning apparatus comprising: the identifying unit is used for acquiring a source code file generated based on the multi-level structured annotation specification and identifying annotation information of the source code file and position association information of a corresponding code row; The alignment unit is used for performing structure analysi