Skip to content

Commit

Permalink
[JavaScript] Add SQL embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Nov 16, 2024
1 parent 29b8a9c commit b7db19b
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
18 changes: 18 additions & 0 deletions JavaScript/Embeddings/SQL (JS template).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%YAML 1.2
---
# http://www.sublimetext.com/docs/syntax.html
# highlight tagged template strings
scope: source.sql.js-template
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax

contexts:
prototype:
- meta_prepend: true
- include: scope:source.js#text-interpolations

strings-content:
- meta_prepend: true
- include: scope:source.js#string-interpolations
18 changes: 18 additions & 0 deletions JavaScript/Embeddings/SQL (TS template).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%YAML 1.2
---
# http://www.sublimetext.com/docs/syntax.html
# highlight tagged template strings
scope: source.sql.ts-template
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax

contexts:
prototype:
- meta_prepend: true
- include: scope:source.ts#text-interpolations

strings-content:
- meta_prepend: true
- include: scope:source.ts#string-interpolations
12 changes: 12 additions & 0 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,18 @@ contexts:
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (sql|SQL)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:source.sql.js-template
embed_scope: meta.string.template.js source.sql.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (?:({{identifier_name}})\s*)?(\`)
captures:
1: variable.function.tagged-template.js
Expand Down
12 changes: 12 additions & 0 deletions JavaScript/TypeScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,18 @@ contexts:
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (sql|SQL)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:source.sql.ts-template
embed_scope: meta.string.template.js source.sql.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (?:({{identifier_name}})\s*)?(\`)
captures:
1: variable.function.tagged-template.js
Expand Down
45 changes: 45 additions & 0 deletions JavaScript/tests/syntax_test_js_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,51 @@ var style = css`
/* ^ punctuation.definition.string.end.js */
/* ^ - meta.string */

/*
* SQL Templates
*/

var sql = sql`SELECT * FROM "foo";`
/* ^^^^^^^^^^^^^^^^^^^^^^ meta.string.template.js */
/* ^ string.quoted.other.js punctuation.definition.string.begin.js - source.sql.embedded */
/* ^^^^^^^^^^^^^^^^^^^^ source.sql.embedded.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js - source.sql.embedded */


var sql = SQL`SELECT * FROM "foo";`
/* ^^^^^^^^^^^^^^^^^^^^^^ meta.string.template.js */
/* ^ string.quoted.other.js punctuation.definition.string.begin.js - source.sql.embedded */
/* ^^^^^^^^^^^^^^^^^^^^ source.sql.embedded.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js - source.sql.embedded */

var sql = sql`
/* ^^^ variable.function.tagged-template */
/* ^^ meta.string.template.js string.quoted.other.js */
/* ^ punctuation.definition.string.begin.js */
/* ^ - source.sql.embedded */
SELECT *,
/* ^^^ keyword.other.DML.sql */
/* ^ constant.other.wildcard.asterisk.sql */
f.id AS database_id
/* ^^ keyword.operator.assignment.alias.sql */
FROM foo
WHERE f.a IS NULL
/* ^^ keyword.other.DML.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
AND f.b IS NOT NULL
/* ^^^ keyword.operator.logical.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
`
/* <- meta.string.template.js string.quoted.other.js - source.sql.embedded */
/*^^^ meta.string.template.js string.quoted.other.js - source.sql.embedded */
/* ^ punctuation.definition.string.end.js */
/* ^ - meta.string */

/*
* Unknown Template
*/
Expand Down
45 changes: 45 additions & 0 deletions JavaScript/tests/syntax_test_typescript_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ var style = css`
/* ^ punctuation.definition.string.end.js */
/* ^ - meta.string */

/*
* SQL Templates
*/

var sql = sql`SELECT * FROM "foo";`
/* ^^^^^^^^^^^^^^^^^^^^^^ meta.string.template.js */
/* ^ string.quoted.other.js punctuation.definition.string.begin.js - source.sql.embedded */
/* ^^^^^^^^^^^^^^^^^^^^ source.sql.embedded.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js - source.sql.embedded */


var sql = SQL`SELECT * FROM "foo";`
/* ^^^^^^^^^^^^^^^^^^^^^^ meta.string.template.js */
/* ^ string.quoted.other.js punctuation.definition.string.begin.js - source.sql.embedded */
/* ^^^^^^^^^^^^^^^^^^^^ source.sql.embedded.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js - source.sql.embedded */

var sql = sql`
/* ^^^ variable.function.tagged-template */
/* ^^ meta.string.template.js string.quoted.other.js */
/* ^ punctuation.definition.string.begin.js */
/* ^ - source.sql.embedded */
SELECT *,
/* ^^^ keyword.other.DML.sql */
/* ^ constant.other.wildcard.asterisk.sql */
f.id AS database_id
/* ^^ keyword.operator.assignment.alias.sql */
FROM foo
WHERE f.a IS NULL
/* ^^ keyword.other.DML.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
AND f.b IS NOT NULL
/* ^^^ keyword.operator.logical.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
`
/* <- meta.string.template.js string.quoted.other.js - source.sql.embedded */
/*^^^ meta.string.template.js string.quoted.other.js - source.sql.embedded */
/* ^ punctuation.definition.string.end.js */
/* ^ - meta.string */

/*
* Unknown Template
*/
Expand Down

0 comments on commit b7db19b

Please sign in to comment.