This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrivadis-custom-syntax.arbori
127 lines (112 loc) · 4.85 KB
/
trivadis-custom-syntax.arbori
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2021 Philipp Salvisberg <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Custom syntax code highlighter for SQL Developer.
*
* The Arbori program is processed from top to bottom. Therefore the order of the Arbori queries is essential.
*
* Sections are used to structure the program. A section starts with a comment like this:
*
* -- ---------------------
* -- <Section description>
* -- ---------------------
*/
-- --------------------------------------------------------------------------------------------------------------------
-- Highlight identifiers
-- --------------------------------------------------------------------------------------------------------------------
-- Treat keywords used as identifiers as identifiers
-- Use built-in style named "PL/SQL identifier"
plsql_identifiers:
[node) identifier
-> {
var node = tuple.get("node");
struct.addStyle(target, node, "plsql-identifier-style");
}
-- --------------------------------------------------------------------------------------------------------------------
-- Highlight column and table aliases
-- --------------------------------------------------------------------------------------------------------------------
-- Creates a new style named "PlSqlColTabAlases", provided by SQLDev.
-- This Arbori query is provided by SQLDev 21.2.1. Reformatted, but otherwise identical.
PlSqlColTabAlases:
[node) c_alias
| [node) identifier & [node-1) query_table_expression
->
;
-- --------------------------------------------------------------------------------------------------------------------
-- Highlight logging code
-- --------------------------------------------------------------------------------------------------------------------
-- Logging calls are not essential for understanding the code. It's considered noise.
-- Therefore a dimmed color should be used to reduce the noise in the code.
-- Creates a new style named "PlSqlLogger", provided by SQLDev.
-- This Arbori query is provided by SQLDev 21.2.1. Extended to handle fully qualified packages and final semicolon.
PlSqlLogger:
[pkg) identifier
& (
?pkg = 'DBMS_OUTPUT'
| ?pkg = 'APEX_DEBUG'
| ?pkg = 'LOG'
| ?pkg = 'LOGGER'
)
& [node) stmt
& [pcall) procedure_call
& (pkg^ = pcall | pkg^^ = pcall | pkg^^^ = pcall)
& pcall^ = node
->
;
-- --------------------------------------------------------------------------------------------------------------------
-- Highlight optimizer hints
-- --------------------------------------------------------------------------------------------------------------------
-- Creates a new style named "Hint"
-- Only the first comment starting with a "+" is considered a hint.
Hints:
[node) sql_statements
-> {
var getHints = function() {
var LexerToken = Java.type('oracle.dbtools.parser.LexerToken');
var Token = Java.type('oracle.dbtools.parser.Token');
var tokens = LexerToken.parse(target.input, true);
var hints = [];
var prevToken = tokens[0];
for (var i=1; i<tokens.size(); i++) {
if ((tokens[i].type == Token.LINE_COMMENT || tokens[i].type == Token.COMMENT) && tokens[i].content.length > 3) {
if (tokens[i].content.substring(2, 3) == "+") {
var prev = prevToken.content.toLowerCase();
if (prev == "select" || prev == "insert" || prev == "update" || prev == "delete" || prev == "merge") {
hints[hints.length] = tokens[i];
prevToken = tokens[i]
}
}
}
if (tokens[i].type != Token.WS && tokens[i].type != Token.LINE_COMMENT && tokens[i].type != Token.COMMENT) {
prevToken = tokens[i];
}
}
return hints;
}
var styleHints = function(hints) {
var Service = Java.type('oracle.dbtools.util.Service');
var Long = Java.type('java.lang.Long');
var stylesField = struct.getClass().getDeclaredField("styles");
stylesField.setAccessible(true);
var styles = stylesField.get(struct);
for (var i in hints) {
var pos = new Long(Service.lPair(hints[i].begin, hints[i].end));
styles.put(pos, "Hints");
}
}
// main
styleHints(getHints());
}