Code highlighting method, device, equipment, storage medium and program product
By constructing a HiveQL highlighting rule system and using regular expressions to match and define highlighting styles, the problem of insufficient HiveQL highlighting support in existing editors has been solved, achieving accurate highlighting of HiveQL code and improving code readability and writing efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- INDUSTRIAL AND COMMERCIAL BANK OF CHINA
- Filing Date
- 2026-03-12
- Publication Date
- 2026-07-28
AI Technical Summary
Existing text editors lack sufficient highlighting support for HiveQL, which makes it easy for developers to make syntax and logic errors when writing HiveQL code, affecting code readability and writing efficiency.
By constructing a HiveQL highlighting rule system based on TextMate syntax, regular expressions are used to match HiveQL keywords, function names, operators, and strings, and their highlighting styles are defined to generate TextMate syntax files adapted to the HiveQL language, thus achieving accurate code highlighting.
It improves the readability and maintainability of HiveQL code, reduces syntax errors, and enhances the writing and debugging efficiency of developers.
Smart Images

Figure CN122470170A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of big data, and in particular to a code highlighting method, apparatus, device, storage medium, and program product. Background Technology
[0002] In the field of big data processing, Hive Query Language (HiveQL), as a query language similar to Structured Query Language (SQL), is widely used for the analysis and management of massive amounts of data. Developers typically need to use a text editor for code input and debugging when writing HiveQL scripts.
[0003] HiveQL syntax differs significantly from existing SQL, including unique syntax structures, proprietary functions, and mixed language embeddings. In this scenario, developers rely on code highlighting to quickly identify syntax elements such as keywords, function names, operators, and strings to improve code readability and writing efficiency.
[0004] However, existing text editors do not adequately support HiveQL highlighting, which makes it easy for developers to misjudge complex syntax, leading to syntax or logic errors. Summary of the Invention
[0005] This application provides a code highlighting method, apparatus, device, storage medium, and program product to solve the problem that existing text editors only perform highlighting processing for SQL language.
[0006] Firstly, this application provides a code highlighting method, including:
[0007] Obtain the target code and determine the target language used by the target code;
[0008] Obtain the syntax highlighting rules of the target language, wherein the syntax highlighting rules include regular expression matching rules and syntax scope;
[0009] The target code is matched using the regular expression matching rules to obtain the target syntax elements;
[0010] A highlighting style is applied to the target syntax element according to the grammatical scope, whereby the grammatical scope is used to characterize the mapping relationship between the syntax element and the highlighting style.
[0011] Secondly, this application provides a code highlighting device, comprising:
[0012] The acquisition module is used to acquire the target code and determine the target language used by the target code.
[0013] The acquisition module is used to acquire the syntax highlighting rules of the target language, wherein the syntax highlighting rules include regular expression matching rules and syntax scope;
[0014] The processing module is used to match the target code according to the regular expression matching rules to obtain the target syntax elements;
[0015] The processing module is used to apply a highlighting style to the target syntax element according to the syntax scope, wherein the syntax scope is used to characterize the mapping relationship between the syntax element and the highlighting style.
[0016] Thirdly, this application provides an electronic device, including: a processor, and a memory communicatively connected to the processor;
[0017] The memory stores computer-executed instructions;
[0018] The processor executes computer execution instructions stored in the memory to implement the code highlighting method as described in the first aspect and various possible implementations of the first aspect above.
[0019] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions thereon, which, when executed by a processor, are used to implement the code highlighting method as described in the first aspect and various possible implementations of the first aspect.
[0020] Fifthly, this application provides a program product, including a computer program, which, when executed by a processor, implements the code highlighting method described above.
[0021] The code highlighting method, apparatus, device, storage medium, and program product provided in this application obtain and determine the target language of the target code, then obtain the predefined syntax highlighting rules for the target language, next use regular expression matching rules to determine the syntax elements in the target code, and finally apply highlighting styles to the syntax elements according to the syntax scope. This method can improve code readability and maintainability, allowing developers to quickly distinguish different syntax elements, accurately locate key parts of the code, reduce errors caused by syntax confusion, and improve development efficiency and code quality. Attached Figure Description
[0022] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0023] Figure 1 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 1 ;
[0024] Figure 2 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 2 ;
[0025] Figure 3 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 3 ;
[0026] Figure 4 A schematic diagram of the structure of a code highlighting device provided in this application;
[0027] Figure 5 This is a schematic diagram of the structure of an electronic device provided in this application.
[0028] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0029] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0030] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of the relevant data all comply with relevant laws, regulations, and standards, necessary confidentiality measures have been taken, they do not violate public order and good morals, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0031] Furthermore, the technical solution involved in this application, which involves big data analysis of user information (including but not limited to personal biometrics, identity data, consumption data, asset data, electronic terminal operation data, etc.) and the use of artificial intelligence technology for automated decision-making, and makes decisions that have a significant impact on personal rights based on the results of automated decision-making, provides users with corresponding operation entry points for users to choose to agree to or reject the results of automated decision-making; if the user chooses to reject, the process will proceed to the expert decision-making process.
[0032] It should be noted that the code highlighting method, apparatus, device, storage medium, and program product provided in this application can be used in the field of big data, or in any field other than big data. The application fields of the code highlighting method, apparatus, device, storage medium, and program product in this application are not limited.
[0033] First, let me explain the terms used in this application:
[0034] TextMate syntax: TextMate syntax is a descriptive language used to specify how text should be syntax-highlighted. It identifies different parts of code using regular expressions and pattern matching rules and applies specified colors and styles to them.
[0035] HiveQL: HiveQL is short for Hive Query Language. HiveQL is a SQL-like query language used to process large-scale datasets stored in the Hadoop Distributed File System.
[0036] In the field of big data processing, HiveQL, as a data warehouse operation tool, is widely used in the querying, analysis, extraction, transformation, and loading processes of massive amounts of data. When writing HiveQL code, developers typically need to enter query statements with complex syntax structures in a text editor, such as table names, field names, function calls (e.g., LATERALVIEW, EXPLODE), special operators (e.g., CLUSTER BY, DISTRIBUTE BY), and string constants.
[0037] Because HiveQL's syntax differs from traditional SQL (for example, it supports syntax structures specific to distributed computing), HiveQL includes functions, table generation syntax, and special operators unique to distributed computing, and existing editors' SQL highlighting rules cannot cover these features. Furthermore, HiveQL code files typically have the .hql extension, but mainstream editors default to recognizing them as SQL files, leading to incorrect highlighting rule matching and resulting in issues such as misjudged keywords and unrecognized function names.
[0038] Meanwhile, HiveQL's insufficient syntax highlighting support leads to poor code readability. Developers are prone to inefficiency when debugging and maintaining code due to syntax errors or formatting issues, increasing their learning curve and debugging time. Therefore, there is an urgent need for a dedicated syntax highlighting method for HiveQL to improve coding efficiency, reduce error rates, and meet the high demands of HiveQL in complex data processing scenarios.
[0039] To address the aforementioned issues, this application proposes a code highlighting method. By constructing a HiveQL highlighting rule system based on TextMate syntax, and designing dedicated highlighting logic for HiveQL's syntactic features (such as distributed computation functions, special operators, and file extension matching), this method resolves the code recognition errors caused by existing editors' SQL highlighting rules being incompatible with HiveQL. This method uses regular expressions to match HiveQL keywords, function names, operators, and strings, defines their highlighting styles, and ultimately generates a TextMate syntax file adapted to the HiveQL language, achieving accurate code highlighting.
[0040] This application can be applied to HiveQL code writing and debugging scenarios in big data development environments. When developers use text editors to process HiveQL code, they can rely on syntax highlighting to quickly identify the code structure. This application can build a dedicated TextMate syntax file that adapts to HiveQL language features, enabling the editor to accurately identify and highlight keywords, function names, operators, and strings in HiveQL code, thereby improving code writing efficiency and reducing syntax errors.
[0041] The executing entity in this application can be a text editor or the core engine of an integrated development environment (IDE), or it can be a code highlighting tool. This application's embodiments use an editor engine as the executing entity to illustrate the code highlighting method.
[0042] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0043] Figure 1 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 1 .like Figure 1 As shown, the code highlighting method provided in this embodiment is applied to the editor engine and includes:
[0044] S101: Obtain the target code and determine the target language used by the target code.
[0045] Understandably, the target code is the code that needs to be highlighted. Users can input the target code file into the editor, or open the file containing the target code through the editor; the engine can then read the file content and load it into memory. When retrieving the target code, the editor engine needs to maintain its original text format to ensure its integrity.
[0046] The editor engine can analyze the target code and determine its target language by checking the file extension of the file containing the target code. For code without a file extension, the editor engine can determine the target language by scanning the characteristics of the first line or keywords.
[0047] S102: Obtain the syntax highlighting rules for the target language.
[0048] The syntax highlighting rules include regular expression matching rules and syntax scope.
[0049] Understandably, to achieve code highlighting, it's first necessary to obtain the target language's specific syntax highlighting rules. These rules guide the editor on how to identify and distinguish different types of syntax elements.
[0050] Syntax highlighting rules include regular expression matching rules. Regular expressions are a text pattern matching tool that can define text patterns through specific character combinations and syntax to precisely match, find, or replace strings that match the rules.
[0051] For example, in HiveQL code, to identify identifiers that conform to naming conventions, a regular expression rule can be defined. This rule requires that the identifier begin with a letter or underscore, and subsequent characters contain only letters, numbers, or underscores, and cannot be a HiveQL reserved keyword. Using such regular expressions, the editor can accurately find all identifiers that conform to the naming conventions in the target code. Different syntax elements, such as keywords, operators, and strings, have their own unique regular expression matching rules. These rules work together to comprehensively identify the syntax structure of the target language.
[0052] Syntax highlighting rules also include syntax scope, which refers to the unique identifier assigned to different syntax elements and used to associate highlighting styles (such as color and font). Syntax scope can clearly define the position and relationship of each syntax element in code execution and semantic understanding.
[0053] S103: The target code is matched and processed using regular expression matching rules to obtain the target syntax elements.
[0054] Understandably, once the syntax highlighting rules of the target language are obtained, the regular expression matching rules can be used to scan and analyze the target code to identify the various syntax elements present in the code.
[0055] The editor engine can read the target code line by line and match each line against predefined regular expressions. For each line, the editor engine can try applying different regular expression rules to see if a matching text fragment can be found. During the matching process, the editor engine can also use parallel processing and caching of matching results to improve matching efficiency.
[0056] For example, when processing HiveQL code, the editor engine can first use predefined keyword regular expression rules to match keywords in the code, such as "SELECT", "FROM", and "WHERE". If text matching the keyword regular expression is found in a line of code, then this text can be identified as a keyword syntax element. The editor engine can then use other regular expression rules, such as variable name rules, string rules, and operator rules, to further match this line of code and find all syntax elements that conform to the rules.
[0057] S104: Apply highlighting styles to the target syntax element based on its grammatical scope.
[0058] Syntactic scope is used to characterize the mapping relationship between syntactic elements and highlighting styles, that is, to clarify how each syntactic element should be displayed.
[0059] Understandably, different syntactic scopes typically correspond to different highlighting styles, which may include settings such as color, font, and background color. The mapping relationship of syntactic scopes is designed based on the syntactic characteristics and programming conventions of the target language. It considers not only the type of syntactic elements but also their position and role in the code.
[0060] By applying highlighting styles to syntax elements based on their syntactic scope, target code can be presented with a clear and readable structure in the editor. Developers can more intuitively distinguish different types of syntax elements, quickly locate and understand key parts of the code, thereby improving code reading and development efficiency.
[0061] The code highlighting method provided in this embodiment obtains the target code and determines the target language used in the target code; obtains the syntax highlighting rules of the target language, which include regular expression matching rules and syntax scope; performs matching processing on the target code using the regular expression matching rules to obtain the target syntax elements; and applies highlighting styles to the target syntax elements according to the syntax scope, which represents the mapping relationship between syntax elements and highlighting styles. This method can make the target code display a clear and readable structure, enabling developers to quickly locate and understand the key parts of the code, thereby improving code reading efficiency and development efficiency. At the same time, this highlighting also makes it easier for developers to discover syntax errors in the code.
[0062] Before obtaining the syntax highlighting rules for the target language, you first need to set the syntax highlighting rules for the target language.
[0063] Understandably, in practical applications, syntax highlighting rules are usually defined in advance by professional developers based on the syntax rules and characteristics of the target language. Developers can transform complex grammatical knowledge into regular expression matching rules and syntax scope definitions by studying the grammatical structure, semantic rules, and common programming practices of the target language. These defined rules can be encapsulated in a specific format, such as a text file, configuration file, or database record, so that subsequent editors or syntax highlighting tools can read and use them.
[0064] When setting up syntax highlighting, it's essential to comprehensively consider the target language's syntactic characteristics, from basic syntactic elements to complex syntactic structures, clearly defining the corresponding regular expression matching rules and syntactic scope to construct a complete, accurate, and efficient set of syntax highlighting rules. This method of pre-defining rules can improve development efficiency.
[0065] Specifically, Figure 2 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 2 .like Figure 2 As shown, in Figure 1 Based on the examples, the definition process of syntax highlighting rules is explained in detail, including:
[0066] S201: Define regular expression matching rules based on the syntactic characteristics of the target language.
[0067] The regular expression matching rules include keyword matching rules, function name matching rules, operator matching rules, and string matching rules.
[0068] Understandably, the syntactic features of a target language refer to the syntactic structure and rules of a specific programming language (such as HiveQL; the following examples all use HiveQL as the target language for illustration). Examples include the definitions of keywords, functions, and operators. For instance, the CLUSTER BY operator or EXPLODE function, which are unique to HiveQL.
[0069] Keyword matching rules refer to matching keywords in a target language using regular expressions. Keywords are words in a programming language that have special meanings and fixed usages. For example, keywords in HiveQL include CREATE (create table / database), SELECT (query data), JOIN (join table), and INSERT (insert data). These keywords are used to construct query statements and control the data retrieval and processing flow.
[0070] When defining regular expression matching rules, it's crucial to match these keywords precisely to ensure they are correctly identified. These keywords can be used as matching patterns in regular expressions, and to ensure accuracy, word boundaries (\b) can be incorporated to avoid partial matches. For example, the regular expression for matching the keyword "SELECT" can be written as `\bSELECT\b`, which avoids incorrectly matching other words containing the "SELECT" substring.
[0071] Function name matching rules refer to matching function names in a target language using regular expressions. HiveQL function names have their own naming conventions and characteristics. HiveQL has a rich set of built-in functions, and function names are typically lowercase and may contain underscores. Examples include aggregate functions (COUNT(), used to determine the number of rows); mathematical functions (ROUND(), used for rounding), etc.
[0072] When defining regular expressions, it's essential to match function names that conform to these naming conventions. Simultaneously, the context in which function names might appear must be considered to avoid incorrect matches. This can be achieved by incorporating contextual syntactic structures, such as parentheses and periods. For example, in an expression, the part that appears before and immediately after parentheses is very likely a function name; regular expressions combined with this contextual information can be used for more precise matching.
[0073] Operator matching rules refer to matching operators in the target language using regular expressions. Operator matching rules must include all operator types in HiveQL. HiveQL operators include arithmetic operators (such as +, -), comparison operators (such as =, >, <), and logical operators (such as AND, OR, NOT).
[0074] When defining regular expressions, it's crucial to ensure accurate matching of operators and to consider the impact of operator precedence and associativity on the match. For example, complex expressions may contain multiple operators appearing consecutively or nested, requiring the regular expression to correctly identify each operator.
[0075] String matching rules refer to matching string constants in a target language using regular expressions. In HiveQL, strings can be defined directly using single quotes or double quotes. For example, 'hello' and "world" are both strings. When defining a regular expression, it needs to be able to match strings with these different representations and handle escape characters that may be present in the string. For example, escape characters such as \" and \' should be correctly recognized by the regular expression as part of the string. Special character classes in regular expressions can be used to handle escape characters and different representations in strings.
[0076] Optionally, the priority of regular expression matching rules can be adjusted based on context information of the target code, including statement type or syntax level.
[0077] Understandably, editors can also optimize matching by adjusting the priority of regular expression matching rules. Contextual information can provide more clues to the matching process, helping to accurately identify different syntax elements and avoid incorrect matches or matching conflicts.
[0078] In HiveQL, different statement types differ in their syntax structure and purpose, which can affect the matching priority of their syntax elements. For example, in `SELECT id AS name`, `AS` is matched as an alias identifier; while in `CREATE TABLE table AS SELECT...`, `AS` is matched as a table structure definer.
[0079] During the matching process, the target code can first be preliminarily identified by the parser, and then the result of the identification can be passed to the regular expression matching module. The priority of the matching rules can be adjusted according to different statement types to avoid misjudgment of syntax elements caused by static rules.
[0080] HiveQL code typically has a hierarchical structure. For example, within a subquery, expressions inside and outside the subquery exist at different syntax levels. When matching function names, their syntax level must be considered. In practice, the syntax tree structure of the code can be analyzed to determine the level of each syntax element, and then the priority of regular expression matching rules can be adjusted based on this level. Syntax elements at higher levels can be given higher matching priority to ensure they are recognized more readily.
[0081] In summary, the above process significantly improves the matching accuracy of regular expressions through context-aware rule optimization, adapting to the highlighting requirements of complex syntactic structures.
[0082] S202: Define the grammatical scope based on the grammatical elements of the target language.
[0083] Syntax scope includes keyword scope, function name scope, operator scope, and string scope. Syntax scope is an important way to classify and organize different syntax elements in the target code. By assigning a unique identifier to each syntax element, it allows the syntax element to be associated with a corresponding highlighting style, achieving syntax highlighting and improving code readability and maintainability.
[0084] Understandably, keyword scope can be defined based on the characteristics and usage range of keywords in HiveQL. When defining the scope of keywords, it's necessary to consider their effective range and how they function throughout the entire query. Typically, keyword scope is either global or limited to a specific syntactic structure; for example, the scope of the WHERE keyword extends from the beginning of the WHERE statement to the end of its corresponding conditional expression.
[0085] When assigning unique identifiers to keywords, they can be categorized based on their type and function. For example, query structure keywords can be assigned to one category, while aggregate function-related keywords can be assigned to another. This categorization allows for different colors and styles to be set for keywords in different categories when associated with highlighting styles, making the keywords in the code clearer and easier to identify. For instance, query structure keywords can be set to blue, and aggregate function-related keywords to green.
[0086] The definition of function name scope is related to the definition and use of functions in HiveQL queries. In HiveQL, function names are primarily invoked within query expressions. When assigning unique identifiers to function names, consider the function's type and functionality. For example, string manipulation functions can be assigned to one category, and mathematical functions to another. By assigning different identifiers to function names, different highlighting styles can be set for them based on their type and functionality; for example, string manipulation functions could be highlighted in yellow, and mathematical functions in purple.
[0087] Defining operator scope requires considering the operator's role and precedence within a HiveQL expression. Different operators have different precedence and associativity. For example, in arithmetic expressions, multiplication and division have higher precedence than addition and subtraction. Operator scope can be understood as its effective range and mode of operation within an expression.
[0088] When assigning unique identifiers to operators, they can be categorized based on their type and precedence. For example, arithmetic operators can be assigned to the first category, comparison operators to the second, and logical operators to the third. This way, when displaying code, the structure and order of operations in the expression can be shown according to the operator category and precedence, helping developers better understand the query logic. For example, arithmetic operators can be set to red, comparison operators to orange, and logical operators to brown.
[0089] The scope of a string can include its definition location and usage range. When assigning a unique identifier to a string, consider its representation and content type. For example, single-quoted strings and double-quoted strings can be assigned to different categories based on their representation, or they can be categorized according to their content type. Different highlighting styles can then be set for each category, such as setting single-quoted strings to gray and double-quoted strings to light gray.
[0090] Optionally, before defining the syntax highlighting rules for the target language, it is necessary to first define the file type identifier for the target language. The file type identifier refers to metadata used to identify the language type of a file, including the file extension or file header identifier.
[0091] Understandably, file type identifiers are the fundamental data for editors to identify the language type of code, typically consisting of the file extension and a file header identifier. The file extension is the most direct identification method; for example, HiveQL files usually use the .hql extension. Editors can quickly associate the filename extension with the corresponding syntax highlighting rules. For instance, when a user opens a file named query.hql, the editor engine can detect the .hql extension and look for a match in its internal language mapping table, confirming that the file belongs to the HiveQL language type.
[0092] File header identifiers can be used as a supplementary method, suitable for scenarios with no file extension or conflicting extensions. The editor engine can use regular expressions to match specific comments (--hiveql) or keywords at the beginning of the file, thus ensuring accurate language recognition.
[0093] For example, taking HiveQL files as an example, if a user creates a file without a file extension, but the first line of its content is `--hiveql` followed by HiveQL code, the editor engine can first try to identify the target language by the file extension when determining the target language in real time. If that fails, it can then scan the file header. If a matching identifier is found, HiveQL syntax highlighting and code completion can still be loaded correctly. This dual-identification mechanism can significantly improve the robustness of language recognition, especially suitable for mixed language scenarios or non-standard file naming scenarios.
[0094] Next, the calling conditions for the syntax highlighting rules of the target language are configured based on the file type identifier.
[0095] The invocation condition refers to the conditions that trigger the application of syntax highlighting rules, such as matching file extensions or header identifiers. In other words, when the editor detects a .hql extension, it automatically loads predefined HiveQL syntax highlighting rules.
[0096] Understandably, once configured, the editor can use the call conditions to explicitly match which syntax highlighting rule, ensuring that the editor can accurately and efficiently provide a customized development experience for different languages.
[0097] In summary, by integrating all the defined rules into a TextMate syntax file and releasing it as a plugin, precise highlighting of the target code can be achieved in the editor.
[0098] The code highlighting method provided in this embodiment predefines various highlighting rules adapted to the target language and the conditions for invoking these rules. These predefined rules are stored in the corresponding location, allowing developers to directly access these rules when syntax highlighting is needed, eliminating the need to redefine them from scratch and significantly improving development efficiency. Different tools can share the same set of syntax highlighting rules, saving considerable time and effort. Furthermore, the rules are defined by professionals, ensuring accuracy and completeness, enabling more precise identification and differentiation of various grammatical elements, thus providing superior syntax highlighting results. In addition, the predefined rules facilitate maintenance and updates. When the target language's syntax changes, only the corresponding rules need to be modified and updated, allowing all tools using these rules to adapt to the new syntax requirements promptly.
[0099] Figure 3 A flowchart illustrating a code highlighting method provided in this application embodiment. Figure 3 .like Figure 3 As shown, after applying highlighting styles to the target syntax elements based on their syntactic scope, highlighting can also be applied to other languages in the target code besides the target language, specifically:
[0100] S301: Detect the syntactic features of embedded languages other than the target language in the target code.
[0101] Understandably, the target code may consist of multiple languages; for example, shell scripts or Python UDFs may be embedded within the target code (e.g., HiveQL). When highlighting the target code, these additional embedded languages also need to be analyzed and highlighted to ensure the completeness and accuracy of the code highlighting.
[0102] Different languages have different syntactic structure identifiers. For example, shell scripts usually begin with specific commands. By recognizing these unique structure identifiers, the editor engine can initially determine the presence of an embedded language. Subsequently, the editor engine can also analyze the semantics and context of the code, and further determine the syntactic features of the embedded language by checking specific keywords and operators.
[0103] The process of determining the grammatical features of the embedded language other than the target language can be the same as that of determining the grammatical features of the target language.
[0104] S302: Determine and switch the syntax highlighting rules for the embedded language based on grammatical features.
[0105] Understandably, once the editor engine successfully detects the syntax features of other languages embedded in the target code, it can then determine the type of the embedded language based on these features and dynamically switch to the corresponding syntax highlighting rules to achieve clear and accurate code display.
[0106] Specifically, after determining the embedded language type, the editor engine can look up the corresponding rules from a predefined syntax highlighting rule library. This rule library contains syntax highlighting configurations for various common languages, each with a unique set of highlighting rules covering the display styles of different syntax elements such as keywords, data types, operators, strings, and comments, including colors, bold fonts, and underlines. The syntax highlighting rules can be defined using the same methods as those for the target language.
[0107] For example, taking HiveQL as the target language, an additional language shell script is embedded. When !hadoop fs-ls / is detected, the editor engine can temporarily load the shell syntax highlighting rules, match hadoop as a shell keyword and highlight it in blue, while the HiveQL CLUSTER BY remains orange.
[0108] The code highlighting method provided in this embodiment determines the language type by comprehensively detecting the syntactic features of the embedded language from multiple aspects such as code structure and keywords. Then, based on the detection results, it dynamically switches to the corresponding language's syntax highlighting rules in real time from a preset rule base. This method allows the editor to dynamically switch syntax highlighting rules, applying the corresponding highlighting rules only based on the detected language type, accurately distinguishing and highlighting different language codes, improving code readability and editing efficiency, and enabling developers to more easily understand and modify code content when dealing with complex multilingual code.
[0109] Figure 4 This is a schematic diagram of a code highlighting device provided in this application. Figure 4 As shown, this application provides a code highlighting device, the code highlighting device 400 including:
[0110] Module 401 is used to acquire the target code and determine the target language used by the target code.
[0111] The acquisition module 401 is used to acquire the syntax highlighting rules of the target language. The syntax highlighting rules include regular expression matching rules and syntax scope.
[0112] Processing module 402 is used to match the target code using regular expression matching rules to obtain the target syntax elements;
[0113] Processing module 402 is used to apply highlighting styles to target syntax elements according to syntax scope, where syntax scope is used to characterize the mapping relationship between syntax elements and highlighting styles.
[0114] In one possible implementation, the apparatus further includes: a definition module 403;
[0115] Define module 403 to pre-set syntax highlighting rules for the target language.
[0116] One possible implementation is to define module 403, which is specifically used to define regular expression matching rules according to the syntactic characteristics of the target language. The regular expression matching rules include keyword matching rules, function name matching rules, operator matching rules, and string matching rules.
[0117] Define module 403, which is specifically used to define the grammatical scope based on the grammatical elements of the target language. The grammatical scope includes keyword scope, function name scope, operator scope, and string scope.
[0118] In one possible implementation, the definition module 403 is further used to define a file type identifier for the target language, the file type identifier including a file extension or a file header identifier;
[0119] Module 403 is also defined to configure the calling conditions of syntax highlighting rules for the target language based on the file type identifier.
[0120] In one possible implementation, the processing module 402 is further configured to detect the grammatical features of an embedded language other than the target language in the target code; and determine and switch the syntax highlighting rules of the embedded language based on the grammatical features.
[0121] In one possible implementation, the processing module 402 is further configured to adjust the priority of the regular expression matching rules based on context information of the target code, including statement type or syntax level.
[0122] The code highlighting device provided in this application embodiment is similar in principle and technical effect to the implementation of each part of the aforementioned code highlighting method, and will not be described again here.
[0123] Figure 5 This is a schematic diagram of the structure of an electronic device provided in this application. Figure 5 This application provides an electronic device 500, which includes a receiver 501, a transmitter 502, a processor 503, and a memory 504.
[0124] Receiver 501 is used to receive instructions and data;
[0125] Transmitter 502 is used to send commands and data;
[0126] Memory 504 is used to store instructions executed by the computer;
[0127] Processor 503 is used to execute computer execution instructions stored in memory 504 to implement the various steps performed by the code highlighting method in the above embodiments. For details, please refer to the relevant descriptions in the foregoing code highlighting method embodiments.
[0128] Optionally, the memory 504 can be either standalone or integrated with the processor 503.
[0129] When the memory 504 is set up independently, the electronic device also includes a bus for connecting the memory 504 and the processor 503.
[0130] The implementation principle and technical effects of the electronic device provided in this embodiment can be found in the foregoing embodiments, and will not be repeated here.
[0131] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the method of any of the foregoing embodiments.
[0132] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the method of any of the foregoing embodiments.
[0133] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to this application.
[0134] It should be further noted that although the steps in the flowchart are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowchart may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0135] It should be understood that the above-described device embodiments are merely illustrative, and the device of this application can also be implemented in other ways. For example, the division of units / modules in the above embodiments is only a logical functional division, and there may be other division methods in actual implementation. For example, multiple units, modules, or components may be combined, or integrated into another system, or some features may be ignored or not executed.
[0136] Furthermore, unless otherwise specified, the functional units / modules in the various embodiments of this application can be integrated into one unit / module, or each unit / module can exist physically separately, or two or more units / modules can be integrated together. The integrated units / modules described above can be implemented in hardware or as software program modules.
[0137] When integrated units / modules are implemented in hardware, the hardware can be digital circuits, analog circuits, etc. The physical implementation of the hardware structure includes, but is not limited to, transistors, memristors, etc. Unless otherwise specified, the processor can be any suitable hardware processor, such as a CPU, GPU, FPGA, DSP, and ASIC, etc. Unless otherwise specified, the storage unit can be any suitable magnetic or magneto-optical storage medium, such as Resistive Random Access Memory (RRAM), Dynamic Random Access Memory (DRAM), Static Random Access Memory (SRAM), Enhanced Dynamic Random Access Memory (EDRAM), High-Bandwidth Memory (HBM), Hybrid Memory Cube (HMC), etc.
[0138] If the integrated unit / module is implemented as a software program module and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned memory includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0139] In the above embodiments, the descriptions of each embodiment have their own emphasis. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments. The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification.
[0140] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0141] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A code highlighting method, characterized in that, include: Obtain the target code and determine the target language used by the target code; Obtain the syntax highlighting rules of the target language, wherein the syntax highlighting rules include regular expression matching rules and syntax scope; The target code is matched using the regular expression matching rules to obtain the target syntax elements; A highlighting style is applied to the target syntax element according to the grammatical scope, whereby the grammatical scope is used to characterize the mapping relationship between the syntax element and the highlighting style.
2. The method according to claim 1, characterized in that, Before obtaining the syntax highlighting rules of the target language, the method further includes: The syntax highlighting rules for the target language are pre-set.
3. The method according to claim 2, characterized in that, The pre-set syntax highlighting rules for the target language include: Regular expression matching rules are defined based on the syntactic characteristics of the target language. These regular expression matching rules include keyword matching rules, function name matching rules, operator matching rules, and string matching rules. Syntactic scope is defined according to the syntactic elements of the target language, including keyword scope, function name scope, operator scope, and string scope.
4. The method according to claim 2, characterized in that, Before presetting the syntax highlighting rules for the target language, the method further includes: Define a file type identifier for the target language, wherein the file type identifier includes a file extension or a file header identifier; Configure the invocation conditions of the syntax highlighting rules for the target language based on the file type identifier.
5. The method according to claim 1, characterized in that, After applying the highlighting style to the target syntax element according to the syntactic scope, the method further includes: Detect the syntactic features of the embedded language other than the target language in the target code; Based on the grammatical features, determine and switch the syntax highlighting rules of the embedded language.
6. The method according to claim 3, characterized in that, After defining the regular expression matching rules based on the syntactic characteristics of the target language, the method further includes: The priority of the regular expression matching rules is adjusted based on the context information of the target code, including statement type or syntax level.
7. A code highlighting device, characterized in that, include: The acquisition module is used to acquire the target code and determine the target language used by the target code. The acquisition module is used to acquire the syntax highlighting rules of the target language, wherein the syntax highlighting rules include regular expression matching rules and syntax scope; The processing module is used to match the target code according to the regular expression matching rules to obtain the target syntax elements; The processing module is used to apply a highlighting style to the target syntax element according to the syntax scope, wherein the syntax scope is used to characterize the mapping relationship between the syntax element and the highlighting style.
8. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 6.
10. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 6.