banner



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.

blank

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:

  1. All custom style names (dotted line to Styles)
    to be shown in the preferences under Code 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.
  2. 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):

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.

blank

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:

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.
  • 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 previous PlSqlLogger query you can access pkg, node, pkg^, pkg^^ via the go method of HashMap. Basically these are the result columns shown when you lot execute a query in the Arbori editor.
  • 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)

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 is to-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

blank

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:

blank

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.

blank

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.

blank

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:

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.

blank

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

blank

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:

blank

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:

Add information technology to the Arbori program in the preference dialog as shown hither:

blank

Press OK and restart SQL Developer.

Configure Fashion

After restarting SQL Developer the new style UnusedIdentifier is shown in the preferences:

blank

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.

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel