Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Sep 22, 2024
1 parent 3e26f68 commit 84438c2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 82 deletions.
51 changes: 21 additions & 30 deletions src/languages/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,35 +622,26 @@ export default function(hljs) {
keywords: { built_in: FUNCTIONS }
};

//COMBOS generator
// const regexPatterns = COMBOS.map(phrase => {
// const escapedPhrase = phrase.replace(/ /g, "\\s+"); // Replace spaces with \s+ to match any whitespace
// return new RegExp(escapedPhrase, "gi"); // Create case-insensitive regex
// });
// turns a multi-word keyword combo into a regex that doesn't
// care about extra whitespace etc.
// input: "START QUERY"
// output: /\bSTART\s+QUERY\b/
function kws_to_regex(list) {
return regex.concat(
/\b/,
regex.either(...list.map((kw) => {
return kw_spaces = kw.replace(/\s+/, "\\s+")
})),
/\b/
)
}

const COMBOSLIST = {
className: 'type',
variants: [
{ match: /\bcreate\s+table\b/ },
{ match: /\binsert\s+into\b/ },
{ match: /\bprimary\s+key\b/ },
{ match: /\bforeign\s+key\b/ },
{ match: /\bnot\s+null\b/ },
{ match: /\balter\s+table\b/ },
{ match: /\badd\s+constraint\b/ },
{ match: /\bgrouping\s+sets\b/ },
{ match: /\bon\s+overflow\b/ },
{ match: /\bcharacter\s+set\b/ },
{ match: /\brespect\s+nulls\b/ },
{ match: /\bignore\s+nulls\b/ },
{ match: /\bnulls\s+first\b/ },
{ match: /\bnulls\s+last\b/ },
{ match: /\bdepth\s+first\b/ },
{ match: /\bbreadth\s+first\b/ }
],
relevance: 0
const COMBOS_MODE = {
scope: "keyword",
match: kws_to_regex(COMBOS),
relevance: 0,
};

// keywords with less than 3 letters are reduced in relevancy
function reduceRelevancy(list, {
exceptions, when
Expand Down Expand Up @@ -683,10 +674,10 @@ export default function(hljs) {
},
contains: [
{
className: "type",
begin: regex.either(...MULTI_WORD_TYPES)
scope: "type",
match: kws_to_regex(MULTI_WORD_TYPES)
},
COMBOSLIST,
COMBOS_MODE,
FUNCTION_CALL,
VARIABLE,
STRING,
Expand Down
64 changes: 32 additions & 32 deletions test/markup/sql/combos.expect.txt
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
<span class="hljs-type">CREATE TABLE</span> users (
id <span class="hljs-type">INT</span> <span class="hljs-type">PRIMARY KEY</span>,
username <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
<span class="hljs-keyword">CREATE TABLE</span> users (
id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
username <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
email <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>)
);

<span class="hljs-comment">-- Table with Composite Primary Key</span>
<span class="hljs-type">CREATE TABLE</span> orders (
order_id <span class="hljs-type">INT</span>,
user_id <span class="hljs-type">INT</span>,
<span class="hljs-type">PRIMARY KEY</span> (order_id, user_id)
<span class="hljs-keyword">CREATE TABLE</span> orders (
order_id <span class="hljs-type">INT</span>,
user_id <span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY KEY</span> (order_id, user_id)
);

<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
<span class="hljs-type">CREATE TABLE</span> products (
product_id <span class="hljs-type">INT</span> <span class="hljs-type">PRIMARY KEY</span> AUTO_INCREMENT,
name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
<span class="hljs-keyword">CREATE TABLE</span> products (
product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span> AUTO_INCREMENT,
name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
);

<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
<span class="hljs-type">CREATE TABLE</span> order_items (
item_id <span class="hljs-type">INT</span>,
order_id <span class="hljs-type">INT</span>,
product_id <span class="hljs-type">INT</span>,
<span class="hljs-type">PRIMARY KEY</span> (item_id),
<span class="hljs-type">FOREIGN KEY</span> (order_id) <span class="hljs-keyword">REFERENCES</span> orders(order_id)
<span class="hljs-keyword">CREATE TABLE</span> order_items (
item_id <span class="hljs-type">INT</span>,
order_id <span class="hljs-type">INT</span>,
product_id <span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY KEY</span> (item_id),
<span class="hljs-keyword">FOREIGN KEY</span> (order_id) <span class="hljs-keyword">REFERENCES</span> orders(order_id)
);

<span class="hljs-comment">-- Table with Date and Primary Key</span>
<span class="hljs-type">CREATE TABLE</span> events (
event_id <span class="hljs-type">INT</span> <span class="hljs-type">PRIMARY KEY</span>,
event_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
<span class="hljs-keyword">CREATE TABLE</span> events (
event_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
event_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
event_date <span class="hljs-type">DATE</span>
);

<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
<span class="hljs-type">CREATE
<span class="hljs-keyword">CREATE
TABLE</span>
users
(
id
<span class="hljs-type">INT</span>
<span class="hljs-type">PRIMARY
<span class="hljs-keyword">PRIMARY
KEY</span>,
username
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
Expand All @@ -51,28 +51,28 @@ email
);

<span class="hljs-comment">-- Table with Composite Primary Key</span>
<span class="hljs-type">CREATE
<span class="hljs-keyword">CREATE
TABLE</span>
orders
(
order_id
<span class="hljs-type">INT</span>,
user_id
<span class="hljs-type">INT</span>,
<span class="hljs-type">PRIMARY
<span class="hljs-keyword">PRIMARY
KEY</span>
(order_id,
user_id)
);

<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
<span class="hljs-type">CREATE
<span class="hljs-keyword">CREATE
TABLE</span>
products
(
product_id
<span class="hljs-type">INT</span>
<span class="hljs-type">PRIMARY
<span class="hljs-keyword">PRIMARY
KEY</span>
AUTO_INCREMENT,
name
Expand All @@ -82,7 +82,7 @@ price
);

<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
<span class="hljs-type">CREATE
<span class="hljs-keyword">CREATE
TABLE</span>
order_items
(
Expand All @@ -92,27 +92,27 @@ order_id
<span class="hljs-type">INT</span>,
product_id
<span class="hljs-type">INT</span>,
<span class="hljs-type">PRIMARY
<span class="hljs-keyword">PRIMARY
KEY</span>
(item_id),
<span class="hljs-type">FOREIGN
<span class="hljs-keyword">FOREIGN
KEY</span>
(order_id)
<span class="hljs-keyword">REFERENCES</span>
orders(order_id)
);

<span class="hljs-comment">-- Table with Date and Primary Key</span>
<span class="hljs-type">CREATE
<span class="hljs-keyword">CREATE
TABLE</span>
events
(
event_id
<span class="hljs-type">INT</span>
<span class="hljs-type">PRIMARY
<span class="hljs-keyword">PRIMARY
KEY</span>,
event_name
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
event_date
<span class="hljs-type">DATE</span>
);
<span class="hljs-type">DATE</span> <span class="hljs-type">with timezone</span>
);
40 changes: 20 additions & 20 deletions test/markup/sql/combos.txt
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
-- Basic Table with a Single Primary Key
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50),
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100)
);

-- Table with Composite Primary Key
CREATE TABLE orders (
order_id INT,
user_id INT,
PRIMARY KEY (order_id, user_id)
CREATE TABLE orders (
order_id INT,
user_id INT,
PRIMARY KEY (order_id, user_id)
);

-- Table with Primary Key and Auto Increment
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
price DECIMAL(10, 2)
);

-- Table with Primary Key and Foreign Key
CREATE TABLE order_items (
item_id INT,
order_id INT,
product_id INT,
PRIMARY KEY (item_id),
CREATE TABLE order_items (
item_id INT,
order_id INT,
product_id INT,
PRIMARY KEY (item_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);

-- Table with Date and Primary Key
CREATE TABLE events (
event_id INT PRIMARY KEY,
event_name VARCHAR(100),
CREATE TABLE events (
event_id INT PRIMARY KEY,
event_name VARCHAR(100),
event_date DATE
);

Expand Down Expand Up @@ -114,5 +114,5 @@ KEY,
event_name
VARCHAR(100),
event_date
DATE
);
DATE with timezone
);

0 comments on commit 84438c2

Please sign in to comment.