A method and device for obtaining code auxiliary information
By virtualizing the object files and establishing the target abstract syntax tree, mutual reference between multiple ipynb files and python files is realized, solving the problem of mutual reference in the existing technology, and improving the efficiency and flexibility of code writing.
Patent Information
- Application Number
- CN202210311467.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-28
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2042-03-28
AI Technical Summary
Existing code assistance tools cannot implement mutual references between multiple ipynb files, as well as mutual references between python files and ipynb files, limiting the efficiency and flexibility of code writing.
By virtualizing the target file, generating virtual files, and establishing a target abstract syntax tree, we realize mutual references between target files in different file formats and obtaining code-assisted information.
It realizes the acquisition of target auxiliary information corresponding to the object code from files of different formats, solves the problem that multiple ipynb files cannot be referenced and between python files and ipynb files cannot be referenced, and improves the efficiency and flexibility of code writing.
Smart Images

Figure CN114661341B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method and device for obtaining code auxiliary information. Background Art
[0002] With the rapid development of the software industry and the growing demand for software, how to quickly write standardized and stable code has become a difficult problem faced by all R&D personnel. Quickly writing code is not only conducive to quickly meeting user needs and shortening the R&D cycle, but also reduces R&D costs. In order to increase the speed of writing code, code assistance tools are usually used to assist developers in writing code, giving prompts for possible words, jumping to definitions, and prompting for refactoring code.
[0003] However, for ipynb files, existing code assistance tools can only support definition jumps and refactoring prompts within a single ipynb file. Multiple ipynb files cannot reference each other, and python files and ipynb files cannot reference each other. Summary of the invention
[0004] In view of this, the purpose of the present application is to provide a method and device for obtaining code auxiliary information, which solves the problem that multiple ipynb files cannot reference each other and that python files and ipynb files cannot reference each other.
[0005] In a first aspect, an embodiment of the present application provides a method for obtaining code auxiliary information, including:
[0006] Get the code information of the target code currently selected by the user;
[0007] Based on the code information, determine the virtual line number of the target code in the virtual file, where the virtual file is obtained by virtualizing the target file, and the target file has multiple file formats;
[0008] Based on the virtual line number, obtaining target auxiliary information corresponding to the target code on a target abstract syntax tree corresponding to a definition type of the target code, the target abstract syntax tree being obtained by parsing a plurality of virtual files;
[0009] Output target auxiliary information.
[0010] Optionally, the code information includes a cell identifier and a line number where the target code is located; based on the code information, determining the virtual line number of the target code in the virtual file includes: determining whether the file where the target code is located is an ipynb file; if it is an ipynb file, obtaining the cell initial line number of the code cell corresponding to the cell identifier; based on the cell initial line number and the line number where the target code is located, determining the virtual line number of the target code in the virtual file.
[0011] Optionally, the target file is virtualized in the following manner: determine whether the target file is a python file or an ipynb file; if it is a python file, directly obtain the virtual file of the target file; if it is an ipynb file, obtain multiple code cells in the target file, and sequentially splice the multiple code cells together to obtain the virtual file of the target file.
[0012] Optionally, multiple code cells are spliced together in sequence to obtain a virtual file of the target file, including: for each code cell, setting the cell initial row number and the cell index; for each line of code in each code cell, using the cell initial row number of the code cell and the code line number of the line of code, determining the virtual row number of the line of code; arranging the codes in multiple code cells together in ascending order of the virtual row numbers to obtain the virtual file of the target file.
[0013] Optionally, for each code cell, the initial row number and the cell index of the cell are set, including: setting the parameter value of the first parameter and the parameter value of the second parameter to 0; selecting the first code cell as the target code cell according to the order of the code cells in the target file; determining whether the target code cell has been set with splicing information; if the splicing information is not set, setting the value of the initial row number of the target code cell using the parameter value of the first parameter, setting the value of the cell index of the target code cell using the parameter value of the second parameter, and marking the target code cell as having been set with splicing information; selecting the next code cell as the target code cell, and returning to execute the step of determining whether the target code cell has been set with splicing information.
[0014] Optionally, the value of the initial cell row number of the target code cell is set using the parameter value of the first parameter, including: using the parameter value of the first parameter as the value of the initial cell row number of the target code cell; obtaining the number of code lines of the target code cell, and updating the parameter value of the first parameter using the result of adding the number of code lines to the parameter value of the first parameter.
[0015] Optionally, the value of the cell index of the target code cell is set using the parameter value of the second parameter, including: using the parameter value of the second parameter as the value of the cell index of the target code cell; and updating the parameter value of the second parameter using the result of adding one to the parameter value of the second parameter.
[0016] Optionally, the target auxiliary information includes a virtual row number; outputting the target auxiliary information includes: arranging multiple code cells in ascending order of cell index; selecting the first-ranked code cell as the target code cell; determining whether the cell initial row number of the target code cell is greater than the virtual row number; if it is greater than the virtual row number, taking the cell initial row number of the previous code cell of the target code cell as the target cell initial row number; taking the difference between the virtual row number and the target cell initial row number as the target row number; outputting the target row number and the cell identifier of the previous code cell of the target code cell.
[0017] Optionally, after determining whether the initial cell row number of the target code cell is greater than the virtual row number, the method further includes: if it is not greater than the virtual row number, taking the next code cell of the target code cell as the target code cell; and returning to execute the step of determining whether the initial cell row number of the target code cell is greater than the virtual row number.
[0018] In a second aspect, an embodiment of the present application further provides a device for acquiring code auxiliary information, the device comprising:
[0019] The acquisition module is used to obtain the code information of the target code currently selected by the user;
[0020] A calculation module, used for determining a virtual line number of a target code in a virtual file based on code information, wherein the virtual file is obtained by virtualizing the target file, and the target file has multiple file formats;
[0021] A determination module, configured to obtain target auxiliary information corresponding to the target code on a target abstract syntax tree corresponding to a definition type of the target code based on a virtual line number, wherein the target abstract syntax tree is obtained by parsing a plurality of virtual files;
[0022] The output module is used to output target auxiliary information.
[0023] The embodiments of the present application bring the following beneficial effects:
[0024] An embodiment of the present application provides a method and device for obtaining code auxiliary information, which can obtain target auxiliary information corresponding to a target code from a target abstract syntax tree according to the code information of the target code, and the target abstract syntax tree is obtained by parsing virtual files corresponding to target files of multiple different file formats, so that the target abstract syntax tree includes codes in target files of multiple different file formats, that is, it is possible to obtain target auxiliary information corresponding to the target code from files of different formats. Compared with the method for obtaining code auxiliary information in the prior art, the problem that multiple ipynb files cannot reference each other and that python files and ipynb files cannot reference each other is solved.
[0025] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0027] Figure 1 A flowchart showing a method for obtaining code auxiliary information provided in an embodiment of the present application is shown;
[0028] Figure 2 A schematic diagram showing a construction process of a code-assisted engineering provided in an embodiment of the present application;
[0029] Figure 3 A schematic diagram showing a file virtualization processing process provided by an embodiment of the present application;
[0030] Figure 4 A schematic diagram of the structure of a device for acquiring code auxiliary information provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0031] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application usually described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, each other embodiment obtained by those skilled in the art without making creative work belongs to the scope of protection of the present application.
[0032] It is worth noting that before this application was proposed, with the rapid development of the software industry and the growing demand for software, how to quickly write standardized and stable code has become a difficult problem faced by all R&D personnel. Quickly writing code is not only conducive to quickly meeting user needs and shortening the R&D cycle, but also can reduce R&D costs. In order to increase the speed of writing code, code assistance tools are usually used to assist developers in writing code, give prompts for words that may be used, jump to definitions, and prompt refactoring codes. However, for ipynb files, the current solutions for solving code assistance in ipynb files are as follows: the first is jupyter, which itself provides plug-ins related to code completion, and opens the development interface of the plug-in, and other people can write related plug-ins for it, but the plug-in speed is relatively slow, and there is no auxiliary functions such as jumping to definitions, automatic code modification, and code error prompts. The second is VS Code, which provides complete code assistance support for ipynb files through the pylance plug-in, but is limited to a single ipynb file, that is, two ipynb files are completely unrelated and cannot be referenced to each other. The third is PyCharm and DataSpell, which also provide complete code assistance support for ipynb, but only support a single ipynb file. It can be seen that the existing code assistance tools can only support definition jumps and refactoring prompts within a single ipynb file. Multiple ipynb files cannot reference each other, and Python files and ipynb files cannot reference each other.
[0033] Based on this, an embodiment of the present application provides a method for obtaining code auxiliary information to solve the problem that multiple ipynb files cannot reference each other and that python files and ipynb files cannot reference each other, improve the efficiency of code writing, and meet the engineering scenarios where python and ipynb are used together.
[0034] The terms involved in this application are explained below.
[0035] Language Server Protocol (LSP) is a communication protocol proposed by Microsoft for coding assistance, which standardizes the format of message transmission.
[0036] LSP server: A server that complies with LSP protocol requirements and provides code assistance. It is an application.
[0037] Ipynb file: a proprietary format file that can contain Python code, Markdown source code, pictures, etc. It is essentially a special json text file that can be parsed by multiple systems and platforms.
[0038] Code assistance: Assist programmers in the process of writing code, give prompts for possible words, issue warnings and modification suggestions for potential errors, and help programmers improve the completed code.
[0039] See also Figure 1 , Figure 1 This is a flow chart of a method for obtaining code auxiliary information provided by an embodiment of the present application. Figure 1 As shown, the method for obtaining code auxiliary information provided in the embodiment of the present application includes:
[0040] Step S101, obtaining the code information of the target code currently selected by the user.
[0041] In this step, the code information may refer to relevant information of the target code, and the code information includes character information and position information of the target code.
[0042] The character information may refer to all characters contained in the target code.
[0043] The location information may specify information about the location of the target code. If it is code in a python file, the location information includes the file identifier, cursor position, line number, and column number. If it is code in an ipynb file, the location information includes the file identifier, cursor position, line number, and column number as well as the cell identifier. The file identifier may refer to the file name.
[0044] In an embodiment of the present application, a user can issue different auxiliary requests through shortcut keys, for example: a user can issue an auxiliary request to jump to the definition through the shortcut key Ctrl+B, and can issue an auxiliary request for reconstruction through the shortcut key Ctrl+T. The auxiliary request for code prompts is automatically issued. After issuing an auxiliary request, the complete string near the cursor is selected as the target code according to the position of the cursor. Taking the code snippet "int apple" as an example, when the cursor is between a and p, apple is used as the target code. At the same time, the file format of the file where the target code is located is determined. If it is a python file, the file identifier, cursor position, line number and column number of the file where the target code is located are obtained. If it is an ipynb file, the file identifier of the file where the target code is located, the cell identifier of the code cell, the cursor position, the line number and column number in the file are obtained. When the target code is multiple characters, the column number is also multiple, that is, the column numbers corresponding to the multiple characters.
[0045] An assistance request may refer to a request for different types of assistance information, and the assistance request is used to determine which assistance information is returned.
[0046] As examples, the assistance request may be a code hint, a jump to definition, or a refactoring.
[0047] If the auxiliary request is a code hint, the specific code of the hint needs to be returned; if the auxiliary request is to jump to the definition or refactor, the file path, line number and column number of the definition or refactored object need to be returned, so as to locate the specific file where the definition or refactored object is located and the specific position in the file according to the file path, line number and column number.
[0048] The following is an introduction to ipynb files.
[0049] The ipynb file includes basic file information and multiple cells, which form a cell list.
[0050] The basic file information may refer to the basic information of the ipynb file. The basic file information includes file metadata, file format version number, and file format sub-version number. The file metadata may be the file name.
[0051] Each cell includes basic information, including cell type, code execution times, and metadata.
[0052] Cell types include code cells, image cells, and markdown cells.
[0053] If the cell is a code cell, the source code field in the cell stores Python code. Since each line of code is separated by '\\n', the line number of each line of code in the code cell can be determined.
[0054] If the cell is a picture cell, no content is stored in the source code field of the cell.
[0055] If the cell is a markdown cell, the source code field in the cell stores the markdown language, which is a lightweight markup language.
[0056] The code execution count may refer to the number of times the code is executed. For image cells, the code execution count may be empty.
[0057] The metadata may refer to data describing the cell, and the metadata includes a cell identifier, which may be a cell number.
[0058] If the cell is a code cell or a markdown cell, the metadata in the cell is used to record the programming language used and the dependent programming library.
[0059] If the cell is a picture cell, the metadata in the cell is used to record the author of the picture.
[0060] Step S102: determining the virtual line number of the target code in the virtual file based on the code information.
[0061] In this step, the virtual line number may refer to a line number in the virtual file, and the virtual line number is used to determine the position of the target code in the virtual file.
[0062] A virtual file may refer to a file that exists only in the computer memory. A virtual file is a file obtained by virtualizing a target file. The target file has multiple file formats.
[0063] Exemplarily, the file format of the target file includes but is not limited to any one of the following: python, ipynb.
[0064] In an embodiment of the present application, the line number of the target code in the current file is taken as the real line number, and the virtual line number of the target code can be determined according to the corresponding relationship between the real line number and the virtual line number.
[0065] In an optional embodiment, the code information includes a cell identifier and a line number where the target code is located; executing step S102 includes: determining whether the file where the target code is located is an ipynb file; if it is an ipynb file, obtaining the cell initial line number of the code cell corresponding to the cell identifier; based on the cell initial line number and the line number where the target code is located, determining the virtual line number of the target code in the virtual file.
[0066] Here, the cell initial line number may refer to the virtual line number of the first line of code of the code cell in the virtual file, and the cell initial line number is used to determine the position of the code cell in the virtual file.
[0067] The methods for determining the virtual line number of the target code are different for files of different formats. If the file where the target code is located is a Python file, the real line number of the target code in the file is directly used as the virtual line number. If the file where the target code is located is an ipynb file, the initial line number of the code cell where the target code is located and the real line number of the target code are obtained, and the sum of the initial line number of the cell and the real line number is used as the virtual line number of the target code in the virtual file.
[0068] It should be noted that before acquiring the code auxiliary information, a code auxiliary project needs to be constructed to complete the acquisition of the target auxiliary information based on the code auxiliary project.
[0069] Refer to the following Figure 2 To introduce the process of building a code-assisted project using multiple files in different formats.
[0070] Figure 2 A schematic diagram of the construction process of the code-assisted engineering provided in an embodiment of the present application is shown.
[0071] like Figure 2 As shown in the figure, before file virtualization, there are three files, namely a.ipynb, b.ipynb, and c.py, of which a.ipynb and b.ipynb are ipynb files, and c.py is a python file. After file virtualization, the file format of c.py remains unchanged and is still in python format, while the file names of a.ipynb and b.ipynb remain unchanged after file virtualization, but have become python format files. Then, the three files after file virtualization are sent to the LSP server through the code assistance service, and the LSP server treats these three files as python files, thereby realizing unified code assistance for ipynb files and python files.
[0072] Among them, the code assistance service is used to organize the internal logical relationship between multiple virtual files, combine multiple virtual files into a project, and realize mutual reference between virtual files. Then, the code assistance service sends the file path and file content inside the project to the LSP server.
[0073] The LSP server receives the file path and file content sent by the code assistance service, parses the virtual file, constructs an abstract syntax tree, and uses the constructed abstract syntax tree to process the assistance request of the target code to obtain the target auxiliary information corresponding to the assistance request. Since the method of obtaining the target auxiliary information through the abstract syntax tree is a prior art, this application will not elaborate on it here.
[0074] Refer to the following Figure 3 To introduce the process of file virtualization.
[0075] Figure 3 A schematic diagram of the file virtualization processing process provided by an embodiment of the present application is shown.
[0076] like Figure 3 As shown in the figure, for the ipynb file, the code cells in the file are extracted to obtain code cell 1 and code cell 2, and then file virtualization is performed on code cell 1 and code cell 2. Other types of cells, such as image cells and Markdown cells, are ignored and not subjected to file virtualization processing. After file virtualization processing, the virtualized file on the right side of the figure is obtained, which splices the source code in code cell 1 and the source code in code cell 2 together.
[0077] Among them, "0, xxx1" in code cell 1 represents the first line of code, the line number of the first line of code is 0, and the content of the first line of code is xxx1; "1, xxx2" represents the second line of code, the line number of the second line of code is 1, and the content of the second line of code is xxx2; "0, xxx3" represents the third line of code, the line number of the third line of code is 2, and the content of the third line of code is xxx3; "3, xxx4" represents the fourth line of code, the line number of the fourth line of code is 3, and the content of the fourth line of code is xxx4.
[0078] "0, yyy1" in code cell 2 represents the first line of code. The line number of the first line of code is 0, and the content of the first line of code is yyy1; "1, yyy2" represents the second line of code. The line number of the second line of code is 1, and the content of the second line of code is yyy2; "0, yyy3" represents the third line of code. The line number of the third line of code is 2, and the content of the third line of code is yyy3.
[0079] Figure 3 In the example, the starting virtual line number of the virtual file is 0 and the ending virtual line number is 6.
[0080] In an optional embodiment, the target file is virtualized in the following manner: determine whether the target file is a python file or an ipynb file; if it is a python file, directly obtain the virtual file of the target file; if it is an ipynb file, obtain multiple code cells in the target file, and splice the multiple code cells together in sequence to obtain the virtual file of the target file.
[0081] Here, when virtualizing the target file, first determine the file type of the target file. If the target file is a Python file, directly convert the file into a virtual file without changing the file format and file content. If the target file is an ipynb file, first select multiple code cells in the target file to Figure 3 For example, code cell 1 and code cell 2 are selected, and then the source code in code cell 1 is concatenated with the source code in code cell 2 to obtain a virtual file of the target file.
[0082] It can be seen that since the source code of the code cell in the ipynb file is Python code, after file virtualization, whether it is the virtual file corresponding to the ipynb file or the virtual file corresponding to the Python file, the code in the virtual file is Python code, which provides a basis for code references between files of different formats.
[0083] In an optional embodiment, multiple code cells are spliced together in sequence to obtain a virtual file of the target file, including: for each code cell, setting the cell initial row number and the cell index; for each line of code in each code cell, using the cell initial row number of the code cell and the code row number of the line of code, determining the virtual row number of the line of code; arranging the codes in the multiple code cells together in ascending order of the virtual row numbers to obtain the virtual file of the target file.
[0084] Here, still Figure 3 For example, the cell initial row number and cell index are set for code cell 1 and code cell 2 respectively. The cell initial row number is used to represent the position of the first line of code of the code cell in the virtual file. For example, the cell initial row number of code cell 1 is set to 0. Since there are 4 lines of code in code cell 1, the cell initial row number of code cell 2 is set to 4.
[0085] The real line number of the first line of code in each code cell is 0, so the virtual line number of the first line of code of code cell 1 in the virtual file is the sum of the cell's initial line number 0 and the real line number 0, that is, the virtual line number of the first line of code of code cell 1 in the virtual file is 0, and so on, the virtual line numbers of other lines of code in code cell 1 in the virtual file are 1, 2, and 3 respectively. Similarly, since the virtual line number of the first line of code of code cell 2 in the virtual file is the sum of the cell's initial line number 4 and the real line number 0, that is, the virtual line number of the first line of code of code cell 2 in the virtual file is 4, and so on, the virtual line numbers of other lines of code in code cell 2 in the virtual file are 5 and 6 respectively.
[0086] In this way, by arranging the codes in a plurality of code cells together in the order of virtual line numbers from small to large, a virtual file including the codes in all code cells in the target file can be obtained.
[0087] In an optional embodiment, for each code cell, the initial row number of the cell and the cell index are set, including: setting the parameter value of the first parameter and the parameter value of the second parameter to 0; selecting the first code cell as the target code cell according to the order of the code cells in the target file; determining whether the target code cell has been set with splicing information; if the splicing information is not set, setting the value of the initial row number of the target code cell using the parameter value of the first parameter, setting the value of the cell index of the target code cell using the parameter value of the second parameter, and marking the target code cell as having been set with splicing information; selecting the next code cell as the target code cell, and returning to execute the step of determining whether the target code cell has been set with splicing information.
[0088] Here, the initial cell row number and cell index can be set for each code cell according to the order of multiple cells in the target file. In specific implementation, first select a code cell as the target code cell, set the initial cell row number and cell index of the target code cell, and after the setting is completed, select the next code cell as the target code cell, and then set the initial cell row number and cell index of the newly selected target code cell.
[0089] When setting the initial row number and cell index of the target code cell, two parameters can be defined first, namely the first parameter and the second parameter, and the parameter values of the two parameters are respectively assigned to the initial row number and cell index of the target code cell. After the assignment, the parameter values of the two parameters are changed respectively, and then the changed parameter values are used to assign the initial row number and cell index of the newly selected target code cell.
[0090] Among them, the splicing information may refer to the initial row number and cell index of the code cell. Whether the target code cell has been set with the splicing information can be determined by determining whether the initial row number of the target code cell and / or the value of the cell index is empty. If the value of the initial row number of the target code cell and / or the value of the cell index is not empty, it is determined that the target code cell has been set with the splicing information.
[0091] In an optional embodiment, the value of the initial cell row number of the target code cell is set using the parameter value of the first parameter, including: using the parameter value of the first parameter as the value of the initial cell row number of the target code cell; obtaining the number of code lines of the target code cell, and updating the parameter value of the first parameter using the result of adding the number of code lines to the parameter value of the first parameter.
[0092] Here, the example in which the number of code lines in the target code cells is 4 is used for explanation. When the initial cell row number of the first target code cell is set, since the parameter value of the first parameter is initially 0, the initial cell row number of the first target code cell is 0. Then, the parameter value of the first parameter is updated by adding the number of code lines 4 to the parameter value 0 of the first parameter. The parameter value of the first parameter after the update is 4. The parameter value of the first parameter at this time is used as the value of the initial cell row number of the second target code cell. In this way, the initial cell row number of the second target code cell is 4. Similarly, if the number of code lines of the second target code cell is also 4, the initial cell row number of the third target code cell is 8.
[0093] In an optional embodiment, the value of the cell index of the target code cell is set using the parameter value of the second parameter, including: using the parameter value of the second parameter as the value of the cell index of the target code cell; and updating the parameter value of the second parameter using the result of adding one to the parameter value of the second parameter.
[0094] Here, when setting the cell index for the first target code cell, since the parameter value of the second parameter is initially 0, the cell index of the first target code cell is 0. Then, the parameter value of the second parameter is updated by adding 1 to the parameter value of the second parameter. After the update, the parameter value of the second parameter is 1. The parameter value of the second parameter at this time is used as the value of the cell index of the second target code cell, so that the cell index of the second target code cell is 1. Similarly, the cell index of the third target code cell is 2, and the cell index of the fourth target code cell is 3.
[0095] Step S103: based on the virtual line number, obtaining target auxiliary information corresponding to the target code on the target abstract syntax tree corresponding to the definition type of the target code.
[0096] In this step, the definition type may refer to the type of a function or a parameter. For example, the definition type may be an integer type or a floating point type.
[0097] An Abstract Syntax Tree (AST) may refer to an abstract representation of a grammatical structure of a source code, and an Abstract Syntax Tree has different types.
[0098] The target abstract syntax tree may refer to an abstract syntax tree corresponding to the type of the target code. Taking the target code as an integer as an example, the target abstract syntax tree refers to an integer abstract syntax tree. The target abstract syntax tree is obtained by parsing multiple virtual files.
[0099] The target assistance information may refer to code assistance information, and the target assistance information is used to provide corresponding assistance support to the target code according to the type of assistance request.
[0100] If the auxiliary request is to jump to a definition, the target auxiliary information includes the file identifier of the file where the definition to be jumped to is located, as well as the line number and column number of the definition in the file; if the auxiliary request is a code prompt, the target auxiliary information includes the code to be prompted.
[0101] In the embodiment of the present application, since the target abstract syntax tree is created according to the virtual file, the codes in the virtual file correspond to the nodes on the target abstract syntax tree. Therefore, the virtual line number and column number of the target code can be determined according to the code information of the target code. According to the virtual line number and column number, the position of the target code on the target abstract syntax tree can be determined, and then the target abstract syntax tree is traversed from the position to obtain the target auxiliary information corresponding to the auxiliary request. Since obtaining the target auxiliary information from the target abstract syntax tree according to the target code is a prior art, it will not be repeated here.
[0102] Step S104: output target auxiliary information.
[0103] In this step, after obtaining the target auxiliary information, the target auxiliary information can be output so that the user can quickly write code according to the target auxiliary information. If the auxiliary request is a code prompt, the code assist tool will display the prompted code in the code prompt box according to the target auxiliary information. If the auxiliary request is to jump to the definition, the code assist tool will jump from the current editing position to the corresponding definition location according to the target auxiliary information.
[0104] In an optional embodiment, the target auxiliary information includes a virtual row number; outputting the target auxiliary information includes: arranging multiple code cells in ascending order of cell index; selecting the first-ranked code cell as the target code cell; determining whether the cell initial row number of the target code cell is greater than the virtual row number; if it is greater than the virtual row number, using the cell initial row number of the previous code cell of the target code cell as the target cell initial row number; using the difference between the virtual row number and the target cell initial row number as the target row number; outputting the target row number and the cell identifier of the previous code cell of the target code cell.
[0105] Here, taking the auxiliary request to jump to the definition as an example, after the LSP server determines the target auxiliary information, the target auxiliary information includes the node position of the definition to be returned on the target abstract syntax tree, and performs code auxiliary service on the target auxiliary information, converting the node position on the target abstract syntax tree into a virtual line number, which is the line number of the definition to be returned in the virtual file. The virtual line number needs to be converted into a real line number to perform a definition jump according to the real line number.
[0106] Specifically, since the positions of code cells in the ipynb file are not necessarily arranged in order of the size of the cell index, the code cells need to be sorted first, and then the frontmost code cells that have not been compared are selected in order of the cell index from small to large as the target code cells, and the target code cells are compared to see whether the initial cell row number is greater than the virtual row number; if it is greater than the virtual row number, it means that the real row number corresponding to the virtual row number is in the previous code cell of the target code cell, then the virtual row number is subtracted from the initial cell row number of the previous code cell, and the result is the real row number, which is the target row number. The real row number and the cell identifier of the previous code cell are output to determine the location of the definition to be jumped.
[0107] It should be noted that the returned target auxiliary information also includes the column number. The column number in the virtual file is the same as the column number of the file before the file virtualization process is performed, so no conversion is required.
[0108] In an optional embodiment, after determining whether the initial cell row number of the target code cell is greater than the virtual row number, it also includes: if it is not greater than the virtual row number, taking the next code cell of the target code cell as the target code cell; returning to execute the step of determining whether the initial cell row number of the target code cell is greater than the virtual row number.
[0109] Here, if the cell initial row number of the target code cell is not greater than the virtual row number, it means that the definition to be jumped to is in the current target code cell or in the code cell after the target code cell. In order to determine which specific cell the definition to jump to is in, it is necessary to continue to select the next code cell of the target code cell as the target cell, and determine whether the cell initial row number of the newly selected target code cell is greater than the virtual row number, until the cell initial row number of the most recently selected target code cell is greater than the virtual row number, indicating that the definition to be jumped to is in the previous code cell of the current target code cell, and then determine the actual row number of the definition to jump to through the cell initial row number of the previous code cell of the current target code cell and the virtual row number.
[0110] Compared with the method for obtaining code auxiliary information in the prior art, the present application can obtain the target auxiliary information corresponding to the target code from the target abstract syntax tree according to the code information of the target code, and the target abstract syntax tree is obtained by parsing virtual files corresponding to multiple target files of different file formats, so that the target abstract syntax tree includes the codes in multiple target files of different file formats, that is, the target auxiliary information corresponding to the target code can be obtained from files of different formats. It can be seen that the ipynb file can be used as an ordinary python file after file virtualization processing, and can be reversely converted, which meets the engineering scenarios of mixed use of ipynb files and python files, and solves the problem that multiple ipynb files cannot reference each other and python files and ipynb files cannot reference each other.
[0111] Based on the same inventive concept, an embodiment of the present application also provides a device for obtaining code auxiliary information corresponding to the method for obtaining code auxiliary information. Since the principle of solving the problem by the device in the embodiment of the present application is similar to the method for obtaining code auxiliary information in the embodiment of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0112] See also Figure 4 , Figure 4 This is a schematic diagram of the structure of a device for obtaining code auxiliary information provided in an embodiment of the present application. Figure 4 As shown in , the device 200 for acquiring code auxiliary information includes:
[0113] Acquisition module 201, used to acquire code information of the target code currently selected by the user;
[0114] A calculation module 202 is used to determine the virtual line number of the target code in the virtual file based on the code information, where the virtual file is a file obtained by virtualizing the target file, and the file format of the target file includes multiple types;
[0115] A determination module 203 is used to obtain target auxiliary information corresponding to the target code on a target abstract syntax tree corresponding to a definition type of the target code based on the virtual line number, where the target abstract syntax tree is obtained by parsing multiple virtual files;
[0116] The output module 204 is used to output target auxiliary information.
[0117] Optionally, the code information includes a cell identifier and a line number where the target code is located; the calculation module 202 is specifically used to: determine whether the file where the target code is located is an ipynb file; if it is an ipynb file, obtain the cell initial line number of the code cell corresponding to the cell identifier; based on the cell initial line number and the line number where the target code is located, determine the virtual line number of the target code in the virtual file.
[0118] Optionally, the device 200 for obtaining code auxiliary information also includes a file virtualization module (not shown in the figure), and the file virtualization module includes: a determination submodule, used to determine whether the target file is a python file or an ipynb file; a first virtualization submodule, used to directly obtain the virtual file of the target file if it is a python file; a second virtualization submodule, used to obtain multiple code cells in the target file if it is an ipynb file, and splice the multiple code cells together in sequence to obtain the virtual file of the target file.
[0119] Optionally, the second virtualization submodule includes: a setting submodule, used to set the cell initial row number and the cell index for each code cell; a virtual row number determination submodule, used to determine the virtual row number of each line of code in each code cell using the cell initial row number of the code cell and the code row number of the line of code; and a splicing submodule, used to arrange the codes in multiple code cells together in order of the virtual row numbers from small to large, to obtain a virtual file of the target file.
[0120] Optionally, the setting submodule is specifically used to: set the parameter value of the first parameter and the parameter value of the second parameter to 0; select the first code cell as the target code cell according to the order of the code cells in the target file; determine whether the target code cell has been set with splicing information; if the splicing information is not set, use the parameter value of the first parameter to set the value of the initial row number of the target code cell, use the parameter value of the second parameter to set the value of the cell index of the target code cell, and mark the target code cell as having been set with splicing information; select the next code cell as the target code cell, and return to execute the step of determining whether the target code cell has been set with splicing information.
[0121] Optionally, the setting submodule is also specifically used to: use the parameter value of the first parameter as the value of the initial row number of the target code cell; obtain the number of code lines of the target code cell, and update the parameter value of the first parameter using the result of adding the number of code lines to the parameter value of the first parameter.
[0122] Optionally, the setting submodule is further specifically used to: use the parameter value of the second parameter as the value of the cell index of the target code cell; and update the parameter value of the second parameter using the result of adding one to the parameter value of the second parameter.
[0123] Optionally, the target auxiliary information includes a virtual row number; the output module is specifically used to: arrange multiple code cells in order of cell index from small to large; select the first code cell as the target code cell; determine whether the cell initial row number of the target code cell is greater than the virtual row number; if it is greater than the virtual row number, use the cell initial row number of the previous code cell of the target code cell as the target cell initial row number; use the difference between the virtual row number and the target cell initial row number as the target row number; output the target row number and the cell identifier of the previous code cell of the target code cell.
[0124] Optionally, the output module is further specifically used to: if it is not greater than the virtual row number, use the next code cell of the target code cell as the target code cell; return to execute the step of determining whether the cell initial row number of the target code cell is greater than the virtual row number.
[0125] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0126] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0127] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0128] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0129] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application can essentially be embodied in the form of a software product, or in other words, the part that contributes to the prior art or the part of the technical solution. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0130] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present application, which are used to illustrate the technical solutions of the present application, rather than to limit them. The protection scope of the present application is not limited thereto. Although the present application is described in detail with reference to the above-mentioned embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-mentioned embodiments within the technical scope disclosed in the present application, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method for obtaining code auxiliary information, characterized in that: include: Get the code information of the target code currently selected by the user; Based on the code information, determining a virtual line number of the target code in a virtual file, wherein the virtual file is a file obtained by virtualizing the target file, and the file format of the target file includes multiple types; Based on the virtual line number, obtaining target auxiliary information corresponding to the target code on a target abstract syntax tree corresponding to a definition type of the target code, wherein the target abstract syntax tree is obtained by parsing a plurality of virtual files; Outputting the target auxiliary information; The code information includes a cell identifier and a row number where the target code is located; The step of determining the virtual line number of the target code in the virtual file based on the code information includes: Determine whether the file where the target code is located is an ipynb file; If it is an ipynb file, obtain the cell initial row number of the code cell corresponding to the cell identifier; Determine the virtual line number of the target code in the virtual file based on the initial line number of the cell and the line number where the target code is located; Virtualize the target file in the following ways: Determine whether the target file is a python file or an ipynb file; If it is a python file, directly obtain the virtual file of the target file; If it is an ipynb file, multiple code cells in the target file are obtained, and the multiple code cells are spliced together in sequence to obtain a virtual file of the target file.
2. The method according to claim 1, characterized in that The step of sequentially splicing the plurality of code cells together to obtain a virtual file of the target file includes: For each code cell, set the cell initial row number and cell index; For each line of code in each code cell, a virtual line number of the line of code is determined using the cell initial line number of the code cell and the code line number of the line of code; Arrange the codes in the plurality of code cells together in ascending order of virtual line numbers to obtain a virtual file of the target file.
3. The method according to claim 2, characterized in that For each code cell, setting the cell initial row number and cell index includes: Set the parameter value of the first parameter and the parameter value of the second parameter to 0; According to the order of code cells in the target file, select the first code cell as the target code cell; Determine whether the target code cell has been set with splicing information; If the splicing information is not set, the value of the cell initial row number of the target code cell is set using the parameter value of the first parameter, the value of the cell index of the target code cell is set using the parameter value of the second parameter, and the target code cell is marked as having the splicing information set; The next code cell is selected as the target code cell, and the step of determining whether the splicing information has been set for the target code cell is returned to be executed.
4. The method according to claim 3, characterized in that: The step of setting the value of the initial row number of the target code cell by using the parameter value of the first parameter includes: Using the parameter value of the first parameter as the value of the initial row number of the target code cell; The number of code lines of the target code cell is obtained, and the parameter value of the first parameter is updated by using the result of adding the number of code lines to the parameter value of the first parameter.
5. The method according to claim 3, characterized in that: The step of setting the value of the cell index of the target code cell by using the parameter value of the second parameter includes: Using the parameter value of the second parameter as the value of the cell index of the target code cell; The parameter value of the second parameter is updated using the result of adding 1 to the parameter value of the second parameter.
6. The method according to claim 2, characterized in that The target auxiliary information includes a virtual line number; The outputting the target auxiliary information comprises: Arrange multiple code cells in ascending order of cell index; Select the first code cell as the target code cell; Determine whether the cell initial row number of the target code cell is greater than the virtual row number; If it is greater than the virtual row number, the cell initial row number of the previous code cell of the target code cell is used as the target cell initial row number; The difference between the virtual row number and the initial row number of the target cell is used as the target row number; The target row number and the cell identifier of the previous code cell of the target code cell are output.
7. The method according to claim 6, characterized in that After determining whether the initial row number of the target code cell is greater than the virtual row number, the method further includes: If it is not greater than the virtual row number, taking the next code cell of the target code cell as the target code cell; Return to the step of determining whether the cell initial row number of the target code cell is greater than the virtual row number.
8. A device for acquiring code auxiliary information, characterized in that: include: The acquisition module is used to obtain the code information of the target code currently selected by the user; A calculation module, used for determining a virtual line number of the target code in a virtual file based on the code information, wherein the virtual file is a file obtained by virtualizing the target file, and the file format of the target file includes multiple types; a determination module, configured to obtain target auxiliary information corresponding to the target code on a target abstract syntax tree corresponding to a definition type of the target code based on the virtual line number, wherein the target abstract syntax tree is obtained by parsing a plurality of virtual files; An output module, used for outputting the target auxiliary information; The code information includes a cell identifier and a row number where the target code is located; The computing module is specifically used for: Determine whether the file where the target code is located is an ipynb file; If it is an ipynb file, obtain the cell initial row number of the code cell corresponding to the cell identifier; Determine the virtual line number of the target code in the virtual file based on the initial line number of the cell and the line number where the target code is located; Determine whether the target file is a python file or an ipynb file; If it is a python file, directly obtain the virtual file of the target file; If it is an ipynb file, multiple code cells in the target file are obtained, and the multiple code cells are spliced together in sequence to obtain a virtual file of the target file.
Citation Information
Patent Citations
An apparatus for processing an abstract syntax tree being associated with a source code of a source program
CN110149800A
Code processing method and device thereof
CN111249736A