Skip to content

Commit ff3985d

Browse files
Dxuianjoshgoebel
andauthored
fix(sql) fix multi-word combos with flexible spacing (#4106)
Co-authored-by: Josh Goebel <[email protected]>
1 parent 0af0968 commit ff3985d

File tree

4 files changed

+268
-20
lines changed

4 files changed

+268
-20
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Core Grammars:
2020
- fix(c) - Fixed hex numbers with decimals [Dxuian]
2121
- fix(typescript) - Fixedoptional property not highlighted correctly [Dxuian]
2222
- fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK]
23+
- fix(sql) - Fixed sql primary key and foreign key spacing issue [Dxuian]
2324

2425
New Grammars:
2526

src/languages/sql.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ export default function(hljs) {
2424
const regex = hljs.regex;
2525
const COMMENT_MODE = hljs.COMMENT('--', '$');
2626
const STRING = {
27-
className: 'string',
27+
scope: 'string',
2828
variants: [
2929
{
3030
begin: /'/,
3131
end: /'/,
32-
contains: [ { begin: /''/ } ]
32+
contains: [ { match: /''/ } ]
3333
}
3434
]
3535
};
3636
const QUOTED_IDENTIFIER = {
3737
begin: /"/,
3838
end: /"/,
39-
contains: [ { begin: /""/ } ]
39+
contains: [ { match: /""/ } ]
4040
};
4141

4242
const LITERALS = [
@@ -606,22 +606,42 @@ export default function(hljs) {
606606
});
607607

608608
const VARIABLE = {
609-
className: "variable",
610-
begin: /@[a-z0-9][a-z0-9_]*/,
609+
scope: "variable",
610+
match: /@[a-z0-9][a-z0-9_]*/,
611611
};
612612

613613
const OPERATOR = {
614-
className: "operator",
615-
begin: /[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/,
614+
scope: "operator",
615+
match: /[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/,
616616
relevance: 0,
617617
};
618618

619619
const FUNCTION_CALL = {
620-
begin: regex.concat(/\b/, regex.either(...FUNCTIONS), /\s*\(/),
620+
match: regex.concat(/\b/, regex.either(...FUNCTIONS), /\s*\(/),
621621
relevance: 0,
622622
keywords: { built_in: FUNCTIONS }
623623
};
624624

625+
// turns a multi-word keyword combo into a regex that doesn't
626+
// care about extra whitespace etc.
627+
// input: "START QUERY"
628+
// output: /\bSTART\s+QUERY\b/
629+
function kws_to_regex(list) {
630+
return regex.concat(
631+
/\b/,
632+
regex.either(...list.map((kw) => {
633+
return kw.replace(/\s+/, "\\s+")
634+
})),
635+
/\b/
636+
)
637+
}
638+
639+
const MULTI_WORD_KEYWORDS = {
640+
scope: "keyword",
641+
match: kws_to_regex(COMBOS),
642+
relevance: 0,
643+
};
644+
625645
// keywords with less than 3 letters are reduced in relevancy
626646
function reduceRelevancy(list, {
627647
exceptions, when
@@ -654,19 +674,10 @@ export default function(hljs) {
654674
},
655675
contains: [
656676
{
657-
begin: regex.either(...COMBOS),
658-
relevance: 0,
659-
keywords: {
660-
$pattern: /[\w\.]+/,
661-
keyword: KEYWORDS.concat(COMBOS),
662-
literal: LITERALS,
663-
type: TYPES
664-
},
665-
},
666-
{
667-
className: "type",
668-
begin: regex.either(...MULTI_WORD_TYPES)
677+
scope: "type",
678+
match: kws_to_regex(MULTI_WORD_TYPES)
669679
},
680+
MULTI_WORD_KEYWORDS,
670681
FUNCTION_CALL,
671682
VARIABLE,
672683
STRING,

test/markup/sql/combos.expect.txt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
2+
<span class="hljs-keyword">CREATE TABLE</span> users (
3+
id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
4+
username <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
5+
email <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>)
6+
);
7+
8+
<span class="hljs-comment">-- Table with Composite Primary Key</span>
9+
<span class="hljs-keyword">CREATE TABLE</span> orders (
10+
order_id <span class="hljs-type">INT</span>,
11+
user_id <span class="hljs-type">INT</span>,
12+
<span class="hljs-keyword">PRIMARY KEY</span> (order_id, user_id)
13+
);
14+
15+
<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
16+
<span class="hljs-keyword">CREATE TABLE</span> products (
17+
product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span> AUTO_INCREMENT,
18+
name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
19+
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
20+
);
21+
22+
<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
23+
<span class="hljs-keyword">CREATE TABLE</span> order_items (
24+
item_id <span class="hljs-type">INT</span>,
25+
order_id <span class="hljs-type">INT</span>,
26+
product_id <span class="hljs-type">INT</span>,
27+
<span class="hljs-keyword">PRIMARY KEY</span> (item_id),
28+
<span class="hljs-keyword">FOREIGN KEY</span> (order_id) <span class="hljs-keyword">REFERENCES</span> orders(order_id)
29+
);
30+
31+
<span class="hljs-comment">-- Table with Date and Primary Key</span>
32+
<span class="hljs-keyword">CREATE TABLE</span> events (
33+
event_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
34+
event_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
35+
event_date <span class="hljs-type">DATE</span>
36+
);
37+
38+
<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
39+
<span class="hljs-keyword">CREATE
40+
TABLE</span>
41+
users
42+
(
43+
id
44+
<span class="hljs-type">INT</span>
45+
<span class="hljs-keyword">PRIMARY
46+
KEY</span>,
47+
username
48+
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
49+
email
50+
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>)
51+
);
52+
53+
<span class="hljs-comment">-- Table with Composite Primary Key</span>
54+
<span class="hljs-keyword">CREATE
55+
TABLE</span>
56+
orders
57+
(
58+
order_id
59+
<span class="hljs-type">INT</span>,
60+
user_id
61+
<span class="hljs-type">INT</span>,
62+
<span class="hljs-keyword">PRIMARY
63+
KEY</span>
64+
(order_id,
65+
user_id)
66+
);
67+
68+
<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
69+
<span class="hljs-keyword">CREATE
70+
TABLE</span>
71+
products
72+
(
73+
product_id
74+
<span class="hljs-type">INT</span>
75+
<span class="hljs-keyword">PRIMARY
76+
KEY</span>
77+
AUTO_INCREMENT,
78+
name
79+
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
80+
price
81+
<span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
82+
);
83+
84+
<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
85+
<span class="hljs-keyword">CREATE
86+
TABLE</span>
87+
order_items
88+
(
89+
item_id
90+
<span class="hljs-type">INT</span>,
91+
order_id
92+
<span class="hljs-type">INT</span>,
93+
product_id
94+
<span class="hljs-type">INT</span>,
95+
<span class="hljs-keyword">PRIMARY
96+
KEY</span>
97+
(item_id),
98+
<span class="hljs-keyword">FOREIGN
99+
KEY</span>
100+
(order_id)
101+
<span class="hljs-keyword">REFERENCES</span>
102+
orders(order_id)
103+
);
104+
105+
<span class="hljs-comment">-- Table with Date and Primary Key</span>
106+
<span class="hljs-keyword">CREATE
107+
TABLE</span>
108+
events
109+
(
110+
event_id
111+
<span class="hljs-type">INT</span>
112+
<span class="hljs-keyword">PRIMARY
113+
KEY</span>,
114+
event_name
115+
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
116+
event_date
117+
<span class="hljs-type">DATE</span> <span class="hljs-type">with timezone</span>
118+
);

test/markup/sql/combos.txt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
-- Basic Table with a Single Primary Key
2+
CREATE TABLE users (
3+
id INT PRIMARY KEY,
4+
username VARCHAR(50),
5+
email VARCHAR(100)
6+
);
7+
8+
-- Table with Composite Primary Key
9+
CREATE TABLE orders (
10+
order_id INT,
11+
user_id INT,
12+
PRIMARY KEY (order_id, user_id)
13+
);
14+
15+
-- Table with Primary Key and Auto Increment
16+
CREATE TABLE products (
17+
product_id INT PRIMARY KEY AUTO_INCREMENT,
18+
name VARCHAR(100),
19+
price DECIMAL(10, 2)
20+
);
21+
22+
-- Table with Primary Key and Foreign Key
23+
CREATE TABLE order_items (
24+
item_id INT,
25+
order_id INT,
26+
product_id INT,
27+
PRIMARY KEY (item_id),
28+
FOREIGN KEY (order_id) REFERENCES orders(order_id)
29+
);
30+
31+
-- Table with Date and Primary Key
32+
CREATE TABLE events (
33+
event_id INT PRIMARY KEY,
34+
event_name VARCHAR(100),
35+
event_date DATE
36+
);
37+
38+
-- Basic Table with a Single Primary Key
39+
CREATE
40+
TABLE
41+
users
42+
(
43+
id
44+
INT
45+
PRIMARY
46+
KEY,
47+
username
48+
VARCHAR(50),
49+
email
50+
VARCHAR(100)
51+
);
52+
53+
-- Table with Composite Primary Key
54+
CREATE
55+
TABLE
56+
orders
57+
(
58+
order_id
59+
INT,
60+
user_id
61+
INT,
62+
PRIMARY
63+
KEY
64+
(order_id,
65+
user_id)
66+
);
67+
68+
-- Table with Primary Key and Auto Increment
69+
CREATE
70+
TABLE
71+
products
72+
(
73+
product_id
74+
INT
75+
PRIMARY
76+
KEY
77+
AUTO_INCREMENT,
78+
name
79+
VARCHAR(100),
80+
price
81+
DECIMAL(10, 2)
82+
);
83+
84+
-- Table with Primary Key and Foreign Key
85+
CREATE
86+
TABLE
87+
order_items
88+
(
89+
item_id
90+
INT,
91+
order_id
92+
INT,
93+
product_id
94+
INT,
95+
PRIMARY
96+
KEY
97+
(item_id),
98+
FOREIGN
99+
KEY
100+
(order_id)
101+
REFERENCES
102+
orders(order_id)
103+
);
104+
105+
-- Table with Date and Primary Key
106+
CREATE
107+
TABLE
108+
events
109+
(
110+
event_id
111+
INT
112+
PRIMARY
113+
KEY,
114+
event_name
115+
VARCHAR(100),
116+
event_date
117+
DATE with timezone
118+
);

0 commit comments

Comments
 (0)