Skip to content

Commit

Permalink
[JavaScript] Add SQL embedding (#4094)
Browse files Browse the repository at this point in the history
This is useful for server-side JS.

Especially using one of the following libraries:
https://github.com/felixfbecker/node-sql-template-strings
https://github.com/blakeembrey/sql-template-tag
https://github.com/XeCycle/pg-template-tag

Using `version: 1` for the Embedding syntaxes since they need to match the inherited `SQL.sublime-syntax`.

It does not currently handle interpolation inside SQL strings.
  • Loading branch information
jtojnar authored Nov 17, 2024
1 parent a71dc94 commit 5b342a7
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
14 changes: 14 additions & 0 deletions JavaScript/Embeddings/SQL (for JS template).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%YAML 1.2
---
# http://www.sublimetext.com/docs/syntax.html
# highlight tagged template strings
scope: source.sql.js-template
version: 1
hidden: true

extends: Packages/SQL/SQL.sublime-syntax

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

extends: Packages/SQL/SQL.sublime-syntax

contexts:
prototype:
- meta_prepend: true
- include: scope:source.ts#text-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
54 changes: 54 additions & 0 deletions JavaScript/tests/syntax_test_js_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,60 @@ var style = css`color:${color}`;
/* ^ punctuation.terminator.statement.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 */

var sql = sql`SELECT * FROM ${users};`
/* ^^^^^^^^^^^^^^ meta.string.template.js source.sql.embedded.js - meta.interpolation.js */
/* ^^^^^^^^ meta.string.template.js source.sql.embedded.js meta.interpolation.js */
/* ^^ punctuation.section.interpolation.begin.js */
/* ^^^^^ source.js.embedded variable.other.readwrite.js */
/* ^ punctuation.section.interpolation.end.js */
/* ^ punctuation.terminator.statement.sql - meta.interpolation.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js */

/*
* Unknown Template
*/
Expand Down
54 changes: 54 additions & 0 deletions JavaScript/tests/syntax_test_typescript_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,60 @@ var style = css`color:${color}`;
/* ^ string.quoted.other.js punctuation.definition.string.end.js */
/* ^ punctuation.terminator.statement.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 */

var sql = sql`SELECT * FROM ${users};`
/* ^^^^^^^^^^^^^^ meta.string.template.js source.sql.embedded.js - meta.interpolation.js */
/* ^^^^^^^^ meta.string.template.js source.sql.embedded.js meta.interpolation.js */
/* ^^ punctuation.section.interpolation.begin.js */
/* ^^^^^ source.js.embedded variable.other.readwrite.js */
/* ^ punctuation.section.interpolation.end.js */
/* ^ punctuation.terminator.statement.sql - meta.interpolation.js */
/* ^ string.quoted.other.js punctuation.definition.string.end.js */

/*
* Unknown Template
*/
Expand Down

0 comments on commit 5b342a7

Please sign in to comment.