How To Get Piazza Code Editor To Show Syntax Colors
Introduction
A client asked me if it is possible to testify unused identifiers in SQL Developer. Since there is no PL/SQL compile alert for that, you lot might be tempted to say no. But you can always use PL/SQL Cop for static code analysis. Guideline One thousand-1030 deals with variables and constants and guideline G-7140 with procedures and functions. However, in this case information technology's also possible to accomplish the same result by tweaking SQL Programmer's preferences for PL/SQL Syntax Colors.
In this web log post I explain how custom syntax highlighting works in SQL Developer. I utilise a simple example outset so show how to highlight unused identifiers in SQL Developer.
What Is Syntax Highlighting?
Wikipedia defines syntax highlighting as follows
Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, peculiarly source code, in unlike colors and fonts co-ordinate to the category of terms.(…)
How Does Syntax Highlighting Work?
The side by side figure illustrates the highlighting process in SQL Developer. The similarities to the formatting process are no accident.
I will explain each stride and component in the adjacent chapters.
Please annotation that these are conceptual components, the actual implementation might look dissimilar.
1. Parser
This step is identical to formatting procedure. The parser reads the unformatted plainly SQL or PL/SQL input and generates a parse-tree. The parse-tree is a hierarchical representation of the significant tokens of the input. In other words, at that place are neither whitespace nor comments in a parse-tree.
Each node in the parse-tree includes the showtime and end position within the plainly SQL input.
2. Custom Styler
The custom styler needs the parse-tree and the Arbori program as input.
Arbori is a query language for parse-trees. Encounter my previous post to learn more about it. The Arbori program is configured in the SQL Programmer'due south preferences under Code Editor
-> PL/SQL Syntax Colors
-> PL/SQL Custom Syntax Rules
.
2.1. The Results
The custom styler basically but runs the Arbori plan. It is responsible for producing two results:
- All custom style names (dotted line to Styles)
to be shown in the preferences underCode Editor
->PL/SQL Syntax Colors
. Allows the user to configure the foreground and background colors as well equally the font style (normal, bold, italic). SQL Programmer discovers styles during startup. For changes (new or renamed styles) to take effect you need to restart SQL Developer. - A list of node-style pairs (solid line to Node Style)
to be rendered according the configured style properties (foreground color, background color and font way).
ii.two. The Default Arbori Program
SQL Developer xix.4.0 provides the following default (I removed all multiline comments):
PlSqlColTabAlases : -- [ node ) c_alias -- Search all the nodes in the parse tree which are cavalcade aliases | [ node ) identifier -- Or nodes with identifier payload , & [ node - 1 ) query_table_expression -- which younger siblings are labeled with table names -> -- The semantic action symbol ( to trigger syntax highlighting ) . ; -- End of the rule PlSqlLogger : [ pkg ) name & ( ? pkg = 'DBMS_OUTPUT' | ? pkg = 'APEX_DEBUG' | ? pkg = 'LOG' | ? pkg = 'logger' -- pattern match is case insensitive ) & ( pkg ^ = node | pkg ^ ^ = node ) & [ node ) procedure_call -> ; |
The query names divers on line i (PlsqlColTabAlases
) and line 8 (PlSqlLogger
) define the mode name used in the preference dialog under Lawmaking Editor
-> PL/SQL Syntax Colors
.
two.three. Configuring Styles
This screenshot shows how the PlSqlLogger
style is configured.
2.4 JavaScript Callback Functions
The default Arbori program uses predefined Java callback functions in the CustomSyntaxStyle
class.
Since SQL Developer 19.ii.0 you can utilise embedded JavaScript callback functions. As a result Java callback functions are not necessary anymore.
Here's an example how to modify PlSqlLogger
query to use a JavaScript callback part:
PlSqlLogger : [ pkg ) name & ( ? pkg = 'DBMS_OUTPUT' | ? pkg = 'APEX_DEBUG' | ? pkg = 'LOG' | ? pkg = 'logger' -- pattern lucifer is case insensitive ) & ( pkg ^ = node | pkg ^ ^ = node ) & [ node ) procedure_call -> { var node = tuple . get ( "node" ) ; struct . addStyle ( target , node , "PlSqlLogger" ) ; } ; |
Important is line 10. Information technology shows how to add together a fashion for a node in the parse-tree.
2.five JavaScript Global Variables
The post-obit variables are provided. You lot should know them when writing JavaScript callback functions.
-
target
- instance of
oracle.dbtools.parser.Parsed
, that's the consummate parse-tree. The following properties and methods are helpful:- src or getSrc() – list of
orace.dbtools.parser.LexerToken
. Indexed past node number. - root or getRoot() – the root node.
- input or getInput() – the source text.
- src or getSrc() – list of
- instance of
-
tuple
- case of
HashMap<Cord, oracle.dbtools.parser.ParseNode>
. It contains an Arbori query result row. The structure is indexed past the query node names. Eastward.g. for the previousPlSqlLogger
query you can accesspkg
,node
,pkg^
,pkg^^
via thego
method ofHashMap
. Basically these are the result columns shown when you lot execute a query in the Arbori editor.
- case of
-
struct
- instance of
oracle.dbtools.raptor.plsql.language.CustomSyntaxStyle
, that's the custom styler. You need only this method:-
addStyle(Parsed target, ParseNode node, Cord styleName)
-
- instance of
two.six Important Classes
Two classes are actually important. I've listed them with some properties and methods that you lot might need:
-
ParseNode
-
from
– start position of the node in the parse-tree. -
to
– end position of the node (one-half-open up interval, this means the last including position isto-1
). -
parent
– parent node. -
descendants()
– list of all kid nodes (including their children, recursively). -
intermediates(int from, int to)
– list of all nodes in the half-open interval. -
toString()
– string representation of the node including all symbols. -
printTree()
– prints a nicely formatted parse-tree on the panel. This textual format is used in the Arbori documentation.
-
-
LexerToken
-
content
– the token represented every bit string. -
begin
– start position in characters of the token in the input string. -
end
– finish position in characters of the token in the input string (half-open interval). -
blazon
– type of the token. -
toString()
– string representation of the token.
-
ii.7 Overriding Queries for Internal Styles
The custom styler is not designed to override queries for built-in styles such as PL/SQL Cord
.
Still, y'all can define additional styles. Custom styles are applied at the very end of the procedure. As a result, you tin can override previously practical styles.
three. Renderer
The renderer is attached to the PL/SQL editor. It runs in the background and needs access to the plain text, the parse tree, the list of node-style pairs and the settings (foreground color, background color, font way) for each way.
Now, the renderer can loop through the internal and custom listing of node-mode pairs and apply the requested style to all tokens within the node. The consequence is a nicely highlighted certificate.
Instance 1 – Extending PlSqlLogger
Setup
I utilise the default configuration of SQL Developer 19.4.0 including the standard Arbori program.
Default Highlighting Result
The result looks good. Await, no, the line ii should be displayed in grayish color, as divers for the PlSqlLogger
style.
Expected Highlighting Outcome
That's what we expect:
In fact, it works as expected if I omit the sys
prefix. However, information technology is good practice to employ information technology. Why? See the Trivadis PL/SQL & SQL Coding Guidelines for Yard-7510.
Code Outline
SQL Developer's lawmaking outline is a representation of the full parse-tree. Disable all filters to testify all nodes.
Arbori Editor
Blazonarbori
in the search field and press enter to open the Arbori editor. Copy the PlSqlLogger
query from the preferences into the Arbori editor and press run.
The query returns no result.
Why? Because [pkg)
expects a node of type name
. Merely when you await in the outline you see the that DBMS_OUTPUT
is a decl_id
and also an identifier
. But it is not a proper noun
.
Set up Arbori Program
Hither's the fix:
PlSqlLogger : [ pkg ) identifier & ( ? pkg = 'DBMS_OUTPUT' | ? pkg = 'APEX_DEBUG' | ? pkg = 'LOG' | ? pkg = 'logger' -- design match is case insensitive ) & ( pkg ^ = node | pkg ^ ^ = node | ( pkg ^ ^ ^ = node & ? pkg - 1 - 1 = 'SYS' ) ) & [ node ) procedure_call -> ; |
On line 2 I expect an identifier
for [pkg)
.
And on line 6 I added the or condition (pkg^^^ = node & ?pkg-i-one = 'SYS')
. This means that the k-grandparent of the node named pkg
must be the same as the node named node
. node
must exist of blazon procedure_call
(see line 7). Furthermore I said that the proper name of the previous-previous node of pkg
must be SYS
. That's it.
At present you can copy & paste this alter into the Arbori editor to test it. Afterwards you lot can copy the alter to the Arbori plan in the preferences to utilize the change.
Example ii – New UnusedIdentifier
Setup
I employ the default configuration of SQL Programmer 19.iv.0 including the changes made in the previous case.
Default Highlighting Result
The result looks good. Simply SQL Developer does not provide information almost unused variables, constants, functions and procedures. The right solution would exist to use the mechanism every bit for SQL injection detection. But that's not (yet) possible.
Expected Highlighting Result
Therefore I'd similar to highlight the unused variables and procedures like this:
The comments in the code example explicate the highlighting result.
Register Style (Requires Restart)
This Arbori query finds all PL/SQL blocks to be examined:
UnusedIdentifier : [ node ) seq_of_stmts & [ node - 1 ) 'Begin' & ( [ node + 1 ) exception_handlers_opt | [ node + 1 ) 'End' ) ; |
Add information technology to the Arbori program in the preference dialog as shown hither:
Press OK
and restart SQL Developer.
Configure Fashion
After restarting SQL Developer the new style UnusedIdentifier
is shown in the preferences:
Configure the font way and the foreground and background colors the mode you like it.
Now nosotros need to tell SQL Developer where to apply this manner.
Arbori Program
This program does all the magic. Please note that the query is the same as earlier. I've simply added the JavaScript callback office. In fact, it's more a JavaScript plan at present.
1 2 iii four 5 6 7 8 9 ten 11 12 13 xiv 15 16 17 18 19 twenty 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | UnusedIdentifier : [ node ) seq_of_stmts & [ node - 1 ) 'BEGIN' & ( [ node + one ) exception_handlers_opt | [ node + i ) 'END' ) -> { var countParentSymbol = part ( inParentNode , inChildNode , symbol ) { var count = 0 ; var parents = inParentNode . intermediates ( inChildNode . from , inChildNode . to ) ; for ( j = 0 ; j < parents . size ( ) ; j ++ ) { var parent = parents . go ( j ) ; if ( parent . toString ( ) . contains ( symbol ) ) { count ++ ; } } return count ; } var populateMaps = function ( inNode ) { if ( inNode != nix ) { var children = inNode . descendants ( ) ; for ( i = 0 ; i < children . size ( ) ; i ++ ) { var child = children . become ( i ) ; if ( child . toString ( ) . contains ( "decl_id" ) ) { if ( countParentSymbol ( inNode , child , "decl_list" ) == ane ) { if ( countParentSymbol ( inNode , child , "seq_of_stmts" ) == 0 ) { var token = target . src [ child . from ] . content . toLowerCase ( ) usageMap . put ( token , 0 ) ; nodeMap . put ( token , child ) ; } } } } } } var checkStatements = role ( inNode ) { var children = inNode . descendants ( ) ; for ( i = 0 ; i < children . size ( ) ; i ++ ) { var child = children . go ( i ) ; if ( child . toString ( ) . contains ( "identifier" ) ) { var token = target . src [ child . from ] . content . toLowerCase ( ) ; var usages = usageMap . get ( token ) ; if ( usages != null ) { usageMap . put ( token , usages + one ) ; } } } } var checkExceptions = function ( inNode ) { if ( inNode != null ) { if ( inNode . toString ( ) . contains ( "exception_handlers_opt" ) ) { checkStatements ( inNode ) ; } } } var reportUnusedIdentifiers = office ( ) { var iterator = usageMap . keySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { var key = iterator . side by side ( ) ; var usages = usageMap . get ( central ) ; if ( usages == 0 ) { struct . addStyle ( target , nodeMap . get ( key ) , "UnusedIdentifier" ) ; } } } var usageMap = new java . util . HashMap ( ) ; var nodeMap = new coffee . util . HashMap ( ) ; var node = tuple . become ( "node" ) ; populateMaps ( node . parent ) ; checkStatements ( node ) ; checkExceptions ( tuple . get ( "node+1" ) ) ; reportUnusedIdentifiers ( node ) ; } ; |
I've divided the logic into 5 local functions. I call them at the end of the program. They should be more or less self-explanatory. Only surfing through a parse-tree has a certain basic complexity. You lot find the important call for the code styler on line 58.
At present yous can replace the UnusedIdentifier
query in the PL/SQL Custom Syntax Rules
preferences dialog with the code in a higher place. Press OK
and unused identifiers are highlighted in the PL/SQL editor.
Summary
The features provided by SQL Developer for syntax highlighting are really all-encompassing and very skillful. I accept never seen anything like this in any other IDE. My second case is probably not a good use example for syntax highlighting. But it clearly shows that custom syntax highlighting in SQL Developer is almost limitless.
Personally I would similar to see comment nodes in the parse-tree likewise. This would allow to distinguish between hints and comments, for case.
Thanks SQL Developer team. Well done!
Source: https://www.salvis.com/blog/2020/04/26/syntax-highlighting-with-sql-developer/
Posted by: hobbsnevered1981.blogspot.com
0 Response to "How To Get Piazza Code Editor To Show Syntax Colors"
Post a Comment