Source code level location methods, electronic devices and media
By acquiring and analyzing the hierarchical structure information of code files and using different display identifiers to distinguish code levels, the problem of quickly and accurately locating code levels is solved, enabling quick jumps to the start or end lines of the target level and sub-levels, thus improving code browsing efficiency.
Patent Information
- Application Number
- CN202511250345.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-03
- Publication Date
- 2025-11-14
- Estimated Expiration
- 2045-09-03
AI Technical Summary
When viewing source code, it is difficult to quickly and accurately locate the code hierarchy that the user is interested in, especially the adjacent parent-child relationship hierarchy.
By obtaining the code line hierarchy information of the code file, the target level and range of the target code line are determined, and different display identifiers are used to distinguish the target level, parent level and child level code range, so as to achieve fast and accurate positioning.
It enables users to quickly and accurately locate the code level of interest within the visible range, and supports quick jumps to the start or end lines of the target level, parent level, and child level, thus improving code browsing efficiency.
Smart Images

Figure CN120743345B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a source code hierarchical location method, electronic device, and medium. Background Technology
[0002] In the field of computer science, it is often necessary to examine large amounts of source code, which frequently exhibits a hierarchical structure. For example, in C++, a class can contain multiple functions, and a function can contain multiple sub-blocks of statements. In SystemVerilog, a module can contain multiple functions or methods, and functions and methods can contain multiple sub-blocks of statements. These syntactic structures clearly demonstrate containment and included relationships, forming a hierarchical structure of code. Examining source code typically requires navigating between multiple code levels to understand the relationships between them. Viewing adjacent hierarchical relationships with "parent-child" connections is particularly common. Therefore, providing a way to quickly and accurately locate the code level of interest to the user is a pressing technical problem that needs to be solved. Summary of the Invention
[0003] The purpose of this invention is to provide a source code level location method, electronic device, and medium that can quickly and accurately locate the code level of interest to the user.
[0004] According to a first aspect of the present invention, a source code level location method is provided, comprising:
[0005] Step S1: Obtain the code line hierarchy information of the code file {(A1,B1),(A2,B2),...,(A... n B n ),...,(A N B N )},A n B is the line number of the nth line of the code file. n This represents the level corresponding to the nth line of code in the code file, where n ranges from 1 to N, and N is the total number of code line levels in the code file.
[0006] Step S2: Based on the code line hierarchy information of the code file, determine the target level corresponding to the target code line, the code range (BS, BE) corresponding to the target level, the code range (CS, CE) corresponding to the parent level of the target level, and a list of all target sub-level code ranges within (BS, BE) {(DS1, DE1), (DS2, DE2), ..., (DS... m DE m ),...,(DS M DS M )},(DS m DEm ) represents the m-th sub-level code range within (BS, BE), where m ranges from 1 to M, and M is the total number of sub-level code ranges within (BS, BE).
[0007] Step S3: Obtain the current visible code range (VS, VE). Initially set the display identifier of each code line to the default identifier. Update the display identifier of the code lines belonging to (BS, BE) in (VS, VE) to the first display identifier. Update the display identifier of the code lines belonging to (CS, CE) in (VS, VE) to the second display identifier. Update the display identifier of the code lines belonging to {(DS1, DE1), (DS2, DE2), ..., (DS... m DE m ),...,(DS M DS M The code line display identifier is updated to the third display identifier.
[0008] According to a second aspect of the present invention, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being configured to perform the method described in the first aspect of the present invention.
[0009] According to a third aspect of the present invention, a computer-readable storage medium is provided, storing computer-executable instructions for performing the method described in the first aspect of the present invention.
[0010] Compared with existing technologies, this invention has significant advantages and beneficial effects. Through the above technical solution, the source code hierarchical location method, electronic device, and medium provided by this invention achieve considerable technological advancement and practicality, and have broad industrial application value. It has at least the following beneficial effects:
[0011] This invention first obtains the code line hierarchy information of the code file, then determines the target level corresponding to the target code line, the code range corresponding to the target level, and the code range of all target sub-levels based on the code line hierarchy information of the code file. Then, it displays the code lines within the visible range that belong to the target level, the code range corresponding to the target level, and the code range of the target sub-levels using different display labels, thereby achieving rapid and accurate location of the code level of interest to the user. Attached Figure Description
[0012] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0013] Figure 1 A flowchart of the source code hierarchical location method provided in an embodiment of the present invention. Detailed Implementation
[0014] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0015] This invention provides a source code hierarchical location method, such as... Figure 1 As shown, it includes:
[0016] Step S1: Obtain the code line hierarchy information of the code file {(A1,B1),(A2,B2),...,(A... n B n ),...,(A N B N )},(A n B n ) represents the hierarchical structure information corresponding to the nth line of code in the code file, A n B is the line number of the nth line of the code file. n This represents the level corresponding to the nth line of code in the code file, where n ranges from 1 to N, and N is the total number of code line levels in the code file.
[0017] Specifically, the code file can be a code file written in C++, SystemVerilog, VHDL, Verilog, etc. A1,A2,...,A n ,...,A N Sort in ascending order.
[0018] Step S2: Based on the code line hierarchy information of the code file, determine the target level corresponding to the target code line, the code range (BS, BE) corresponding to the target level, the code range (CS, CE) corresponding to the parent level of the target level, and a list of all target sub-level code ranges within (BS, BE) {(DS1, DE1), (DS2, DE2), ..., (DS... mDE m ),...,(DS M DS M )},(DS m DE m ) represents the m-th sub-level code range within (BS, BE), where m ranges from 1 to M, and M is the total number of sub-level code ranges within (BS, BE).
[0019] The target code line is the currently selected line of code by the user. The target level is the B corresponding to the target code line in the code file's line hierarchy information. n .
[0020] Step S3: Obtain the current visible code range (VS, VE). Initially set the display identifier of each code line to the default identifier. Update the display identifier of the code lines belonging to (BS, BE) in (VS, VE) to the first display identifier. Update the display identifier of the code lines belonging to (CS, CE) in (VS, VE) to the second display identifier. Update the display identifier of the code lines belonging to {(DS1, DE1), (DS2, DE2), ..., (DS... m DE m ),...,(DS M DS M The code line display identifier is updated to the third display identifier.
[0021] In one embodiment, the default identifier, the first display identifier, the second display identifier, and the third display identifier are represented by different colors. In another embodiment, the default identifier, the first display identifier, the second display identifier, and the third display identifier are represented by different shapes. It should be noted that the default identifier, the first display identifier, the second display identifier, and the third display identifier are not limited to the two examples above; other implementations that can clearly distinguish the default identifier, the first display identifier, the second display identifier, and the third display identifier are also applicable.
[0022] It should be noted that when the target line of code changes, steps S2 and S3 should be re-executed based on the new target line of code. When the current visible code range changes, step S3 should be re-executed based on the new visible code range.
[0023] As one embodiment, step S1 includes:
[0024] Step S11: Set n=1, initial code line number A0=0, initial level B0=0.
[0025] Step S12: Parse the nth line of code in the code file using lexical analysis, and set A. n =A n-1+1, if the n-th line of code contains progressive information, set B n =B n-1 +1, if the n-th line of code contains exit information, set B n =B n-1 -1, otherwise, set B n =B n-1 .
[0026] Among them, the progressive information is the information for entering the next code level, and the exit information is the information for exiting the current level. The progressive information and exit information corresponding to different code languages may be different.
[0027] It should be noted that all existing lexical analysis methods fall within the protection scope of the present invention and will not be elaborated here.
[0028] Step S13: If n < N, set n = n + 1, and return to execute Step S12. If n = N, generate {(A1, B1), (A2, B2),..., (A n , B n ),...,(A N , B N )}.
[0029] It can be understood that through Steps S11 - S13, the code is analyzed line by line to obtain the hierarchical structure information of each code line, thereby generating the hierarchical structure information of the code lines of the code file.
[0030] As an embodiment, the Step S2 includes:
[0031] Step S21: Obtain the line number target A of the target code line n .
[0032] It should be noted that specifically, methods such as mouse selection and shortcut key locking can be used to select the line number of the target code line.
[0033] Step S22: Determine the target level target B corresponding to the target A n based on the hierarchical structure information of the code lines of the code file n .
[0034] It can be understood that since the hierarchical structure information of the code lines of the code file records the level corresponding to each code line number, the target B corresponding to the target A can be directly determined based on the hierarchical structure information of the code lines of the code file n . n .
[0035] Step S23: Starting from the target (A n , B n ), traverse the hierarchical structure of the code lines of the code file to the left, and use the first one that meets the current Bn Equal to target B n And the next B n Equal to target B n -1 of A n Determined as BS, from target (A) n B n Begin traversing the line-of-speech hierarchy of the code file to the right, finding the first line that satisfies the current B... n Equal to target B n And the next B n Equal to target B n -1 of A n Once the target level is identified as BE, the corresponding code scope (BS, BE) is generated.
[0036] Step S24, from target B n The corresponding minimum A n Begin traversing the code line hierarchy of the code file to the left, finding the first line that satisfies the current B. n Equal to target B n -1 and the next B n Equal to target B n -2 or empty A n Identified as CS, from target B n The corresponding maximum A n Begin traversing the line-of-speech hierarchy of the code file to the right, finding the first line that satisfies the current B. n Equal to target B n -1 and the next B n Equal to target B n -2 or empty A n Once identified as CE, the code scope (CS, CE) corresponding to the parent level of the target level is generated.
[0037] Step S25: Starting from BS, traverse the line hierarchy of the code file to the right until BE, and find the m-th line that satisfies the current B. n Equal to target B n +1 and the previous B n Equal to target B n A n Determined as DS m The m-th element that satisfies the current B n Equal to target B n +1 and the next B n Equal to target B n A n Determined as DE m Generate {(DS1,DE1),(DS2,DE2),...,(DS... m DE m ),...,(DS MDS M )}.
[0038] It should be noted that when viewing the source code, it is usually necessary to examine the hierarchical relationships that have a "parent-child" relationship. Through steps S21-S24, the code range (BS, BE) corresponding to the target level, the code range (CS, CE) corresponding to the parent level of the target level, and the code range of all target sub-levels are quickly and accurately determined.
[0039] After visually displaying the code range (BS, BE) corresponding to the target level, the code range (CS, CE) corresponding to the parent level of the target level, and the code range of all target child levels within the visible range through steps S1-S3, it is possible to further enable fast jumps between "parent-child" relationship levels. The following are some specific examples to illustrate this:
[0040] Example 1
[0041] The step S3 may be followed by:
[0042] Step S4: Lock the current target line and jump to the BS line based on the preset first trigger module or jump to the BE line based on the preset second trigger module.
[0043] Specifically, the first trigger module and the second trigger module can be different shortcut keys or different buttons set on the display interface. By operating the corresponding shortcut keys or buttons, the starting or ending line of the target level can be quickly and accurately located directly.
[0044] Example 2
[0045] The step S3 may be followed by:
[0046] Step C4: Lock the current target line and jump to the CS line based on the preset third trigger module or jump to the CE line based on the preset fourth trigger module.
[0047] The third and fourth trigger modules can be different shortcut keys or different buttons on the display interface. By operating the corresponding shortcut keys or buttons, the starting or ending line of the parent level of the target level can be quickly and accurately located.
[0048] Example 3
[0049] The step S3 is followed by:
[0050] Step D4: Select the code type of the sub-level you want to focus on based on the preset display interface. The code types include modules, functions, loop bodies, conditional bodies, and statement blocks.
[0051] Specifically, a sub-level code type selection interface can be set on the preset display interface in the form of a floating window or pop-up window. Users can select one or more code types according to their application needs, thereby filtering out the sub-levels corresponding to code types that do not need to be considered.
[0052] Step D5: Present the first line of code corresponding to the target sub-level of the currently selected type of interest in the preset display interface.
[0053] It should be noted that, in order to further improve the efficiency of users in selecting the sub-level to jump to, step D5 displays the first line of code corresponding to the target sub-level of the selected type of interest. Users can quickly and accurately select the sub-level to jump to by executing step D6 based on the first line of code corresponding to the target sub-level.
[0054] Step D6: Select the rotor level to be jumped to based on the first line of code corresponding to the target sub-level of the currently selected type of interest presented in the preset display interface.
[0055] Step D7: Based on the selected rotor level to be jumped, jump to the DS corresponding to the rotor level to be jumped. m or DE m .
[0056] Steps D4-D5 allow for quick and accurate navigation to the start or end line of the target sub-level that the user needs to focus on.
[0057] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the description of this invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. The term "and / or" as used herein includes any and all combinations of one or more of the associated listed items.
[0058] It should be noted that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe the steps as sequential processes, many of these steps can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the steps can be rearranged. A process can be terminated when its operation is complete, but it may also have additional steps not included in the figures. A process can correspond to a method, function, procedure, subroutine, subroutine, etc.
[0059] This invention first obtains the code line hierarchy information of the code file, then determines the target level corresponding to the target code line, the code range corresponding to the target level, and the code range of all target sub-levels based on the code line hierarchy information of the code file. Then, it displays the code lines within the visible range that belong to the target level, the code range corresponding to the target level, and the code range of the target sub-levels using different display labels, thereby achieving rapid and accurate location of the code level of interest to the user.
[0060] This invention also provides an electronic device, including: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being configured to perform the method described in this invention.
[0061] This invention also provides a computer-readable storage medium storing computer-executable instructions for performing the methods described in this invention.
[0062] This invention first obtains the code line hierarchy information of the code file, then determines the target level corresponding to the target code line, the code range corresponding to the target level, and the code range of all target sub-levels based on the code line hierarchy information of the code file. Then, it displays the code lines within the visible range that belong to the target level, the code range corresponding to the target level, and the code range of the target sub-levels using different display identifiers, thereby achieving rapid and accurate location of the code level of interest to the user.
[0063] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Although the present invention has been disclosed above with reference to preferred embodiments, it is not intended to limit the present invention. Any person skilled in the art can make some modifications or alterations to the above-disclosed technical content to create equivalent embodiments without departing from the scope of the present invention. Any simple modifications, equivalent changes, and alterations made to the above embodiments based on the technical essence of the present invention without departing from the scope of the present invention shall still fall within the scope of the present invention.
Claims
1. A source code hierarchical location method, characterized in that, include: Step S1: Obtain the code line hierarchy information of the code file {(A1,B1),(A2,B2),...,(A... n B n ),...,(A N B N )},A n B is the line number of the nth line of the code file. n This represents the level corresponding to the nth line of code in the code file, where n ranges from 1 to N, and N is the total number of code line levels in the code file. Step S2: Based on the code line hierarchy information of the code file, determine the target level corresponding to the target code line, the code range (BS, BE) corresponding to the target level, the code range (CS, CE) corresponding to the parent level of the target level, and a list of all target sub-level code ranges within (BS, BE) {(DS1, DE1), (DS2, DE2), ..., (DS... m DE m ),...,(DS M DS M )},(DS m DE m ) represents the m-th sub-level code range within (BS, BE), where m ranges from 1 to M, and M is the total number of sub-level code ranges within (BS, BE). Step S3: Obtain the current visible code range (VS, VE). Initially set the display identifier of each code line to the default identifier. Update the display identifier of the code lines belonging to (BS, BE) in (VS, VE) to the first display identifier. Update the display identifier of the code lines belonging to (CS, CE) in (VS, VE) to the second display identifier. Update the display identifier of the code lines belonging to {(DS1, DE1), (DS2, DE2), ..., (DS... m DE m ),...,(DS M DS M The code line display identifier is updated to the third display identifier.
2. The method according to claim 1, characterized in that, Step S1 includes: Step S11: Set n=1, initial line number A0=0, initial level B0=0; Step S12: Parse the nth line of code in the code file using lexical analysis, and set A. n =A n-1 +1, if the nth line of code contains progressive information, then set B. n =B n-1 +1, if the nth line of code contains exit information, then set B. n =B n-1 -1, otherwise, set B. n =B n-1 ; Step S13: If n < N, set n = n + 1 and return to execute Step S12. If n = N, generate {(A1, B1), (A2, B2),..., (A n , B n ),..., (A N , B N )}.
3. The method according to claim 1, characterized in that, Step S2 includes: Step S21: Obtain the target code line number target A n ; Step S22: Determine target A based on the code line hierarchy information of the code file. n Corresponding target level target B n ; Step S23, from the target (A) n B n Begin traversing the code line hierarchy of the code file to the left, finding the first line that satisfies the current B... n Equal to target B n And the next B n Equal to target B n -1 of A n Determined as BS, from target (A) n B n Begin traversing the line-of-speech hierarchy of the code file to the right, finding the first line that satisfies the current B... n Equal to target B n And the next B n Equal to target B n -1 of A n Once the target level is identified as BE, the corresponding code scope (BS, BE) is generated. Step S24, from target B n The corresponding minimum A n Begin traversing the code line hierarchy of the code file to the left, finding the first line that satisfies the current B. n Equal to target B n -1 and the next B n Equal to target B n -2 or empty A n Identified as CS, from target B n The corresponding maximum A n Begin traversing the line-of-speech hierarchy of the code file to the right, finding the first line that satisfies the current B. n Equal to target B n -1 and the next B n Equal to target B n -2 or empty A n Once identified as CE, the code scope (CS, CE) corresponding to the parent level of the target level is generated. Step S25: Starting from BS, traverse the line hierarchy of the code file to the right until BE, and find the m-th line that satisfies the current B. n Equal to target B n +1 and the previous B n Equal to target B n A n Determined as DS m The m-th element that satisfies the current B n Equal to target B n +1 and the next B n Equal to target B n A n Determined as DE m Generate {(DS1,DE1),(DS2,DE2),...,(DS... m DE m ),...,(DS M DS M )}.
4. The method according to claim 1, characterized in that, Following step S3, the following is also included: Step S4: Lock the current target line and jump to the BS line based on the preset first trigger module or jump to the BE line based on the preset second trigger module.
5. The method according to claim 1, characterized in that, Following step S3, the following is also included: Step C4: Lock the current target line and jump to the CS line based on the preset third trigger module or jump to the CE line based on the preset fourth trigger module.
6. The method according to claim 1, characterized in that, Following step S3, the following is also included: Step D4: Select the code type of the sub-level you want to focus on based on the preset display interface. The code types include modules, functions, loop bodies, conditional bodies, and statement blocks. Step D5: Present the first line of code corresponding to the target sub-level of the currently selected type of interest in the preset display interface; Step D6: Select the rotor level to be jumped to based on the first line of code corresponding to the target sub-level of the currently selected type of interest presented in the preset display interface; Step D7: Based on the selected rotor level to be jumped, jump to the DS corresponding to the rotor level to be jumped. m or DE m .
7. The method according to claim 1, characterized in that, The default identifier, the first display identifier, the second display identifier, and the third display identifier are represented by different colors.
8. The method according to claim 1, characterized in that, The default identifier, the first display identifier, the second display identifier, and the third display identifier are represented by different shapes.
9. An electronic device, characterized in that, include: At least one processor; And, a memory communicatively connected to the at least one processor; The memory stores instructions that are executed by the at least one processor, the instructions being configured to perform the method of any one of claims 1-8.
10. A computer-readable storage medium, characterized in that, The device stores computer-executable instructions for performing the method of any one of claims 1-8.
Citation Information
Patent Citations
Code table hierarchical display method and device, equipment, storage medium and product
CN116186245A
Data flow diagram generation method and device, equipment, storage medium and program product
CN119938048A