Skip to content

Commit

Permalink
[SQL] scope join columns and TSQL WITH contents
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-hall committed Sep 21, 2021
1 parent 4608320 commit 2541790
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Markdown/syntax_test_markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -2578,8 +2578,8 @@ okay
```sql
|^^^^^ meta.code-fence.definition.begin.sql
| ^^^ constant.other.language-name
SELECT 10 *
|^^^^^^^^^^^ markup.raw.code-fence.sql
SELECT *
|^^^^^^^^ markup.raw.code-fence.sql
|^^^^^ keyword.other.DML.sql
FROM TableName
```
Expand Down
4 changes: 2 additions & 2 deletions PHP/tests/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -1639,12 +1639,12 @@ function generate2()
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.begin.php - meta.interpolation - string string
// ^^^^^^^^^^^^^^^^^^^^ meta.string.php meta.interpolation.php source.sql - string.quoted.double.php
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.end.php - meta.interpolation - string string
// ^^^^^^ keyword.other.create.sql
// ^^^^^^ keyword.other.ddl.sql

$sql = "
CREATE TABLE `version`...
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php meta.interpolation.php source.sql - string.quoted.double.php
// ^^^^^^ keyword.other.create.sql
// ^^^^^^ keyword.other.ddl.sql
";

// Do not highlight plain SQL indicator as SQL
Expand Down
111 changes: 80 additions & 31 deletions SQL/SQL (basic).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ variables:
string_escape: (?:\\.)
simple_identifier: (?:\w+)
dml_targets: (?i:aggregate|conversion|database|domain|function|group|((?:fulltext|spatial|unique)\s+)?index|language|operator class|operator|procedure|rule|schema|sequence|table(?:space)?|trigger|type|user|view)
reserved: (?i:\b(?:from|order|group|select|where|inner|outer|left|right|join|on|set|;|with)\b)

contexts:
prototype:
Expand Down Expand Up @@ -132,7 +133,7 @@ contexts:
drop-condition:
- include: dml-condition
- match: (?=\S)
pop: true
set: single-identifier-after-whitespace

dml-condition:
- match: (?i:\b(if)\b)
Expand Down Expand Up @@ -228,31 +229,20 @@ contexts:

ddl-statements:
- match: \b(?i:create(?:\s+or\s+replace)?)\b
scope: keyword.other.create.sql
scope: keyword.other.ddl.sql
push: ddl-create-target
- match: (?i:\s*\b(drop)\s+({{dml_targets}}))
scope: meta.drop.sql
captures:
1: keyword.other.create.sql
2: keyword.other.sql
push: drop-condition
- match: (?i:\s*(drop)\s+(table)\s+(\w+)(\s+cascade)?\b)
scope: meta.drop.sql
captures:
1: keyword.other.create.sql
2: keyword.other.table.sql
3: entity.name.function.sql
4: keyword.other.cascade.sql
- match: (?i:\s*\b(alter)\s+({{dml_targets}})\s+)
scope: meta.alter.sql
captures:
1: keyword.other.create.sql
2: keyword.other.table.sql
- match: (?i:\s*\b(add)\s+(column|constraint|fulltext\s+(index|key)|index|spatial\s+(index|key)))
scope: meta.add.sql
captures:
1: keyword.other.add.sql
2: keyword.other.sql
- match: (?i:\bdrop\s+table\b)
scope: keyword.other.ddl.sql
push: ddl-drop-table
- match: \b(?i:drop)\b
scope: keyword.other.ddl.sql
push: ddl-drop-target
- match: \b(?i:alter\s+table)\b
scope: keyword.other.ddl.sql
push: [ddl-alter-table, table-name, single-identifier-after-whitespace]
- match: \b(?i:alter)\b
scope: keyword.other.ddl.sql
push: ddl-alter-target
- match: (?i:\b(((?:foreign|fulltext|primary|unique)\s+)?key|references|on\sdelete(\s+cascade)?|on\supdate(\s+cascade)?|check|constraint|default)\b)
scope: storage.modifier.sql
- match: (?i:\b(grant(\swith\sgrant\soption)?|revoke)\b)
Expand All @@ -270,6 +260,46 @@ contexts:
- match: (?=\S)
pop: true

ddl-drop-target:
- meta_scope: meta.drop.sql
- match: |-
(?xi)
\b{{dml_targets}}\b
captures:
1: keyword.other.sql
set: drop-condition
- match: (?=\S)
set: drop-condition

ddl-drop-table:
- meta_scope: meta.drop.sql
- include: dml-condition
- match: (?=\S)
set: [table-name, single-identifier-after-whitespace]

ddl-alter-table:
- meta_scope: meta.alter.sql
- match: \b(?i:add(?:\s+column)?|alter\s+column)\b
scope: keyword.other.ddl.sql
push: [column-alias, single-identifier-after-whitespace]
- include: expressions
- include: ddl-alter-common
- match: (?=\S)
pop: true

ddl-alter-target:
- meta_scope: meta.alter.sql
- include: ddl-alter-common
- match: (?=\S)
pop: true

ddl-alter-common:
- match: (?i:\s*\b(add)\s+(constraint|(?:fulltext|spatial)\s+(index|key)|index))
scope: meta.add.sql
captures:
1: keyword.other.add.sql
2: keyword.other.sql

dml-statements:
- match: (?i:\bselect\b)
scope: keyword.other.DML.sql
Expand All @@ -278,8 +308,9 @@ contexts:
- match: (?i:\b(?:insert\s+into|update|delete(?:\s+from)?|truncate)\b)
scope: keyword.other.DML.sql
push: [table-name, single-identifier-after-whitespace]
- match: (?i:\b(?:set|with)\b)
- match: (?i:\bset\b)
scope: keyword.other.DML.sql
push: set
- match: (?i:\bvalues\b)
scope: keyword.other.DML.II.sql
- include: joins
Expand All @@ -292,13 +323,11 @@ contexts:
push: maybe-subquery
- match: (?i)\b(asc|desc)\b
scope: keyword.other.order.sql
- match: (?i)\bon\b
scope: keyword.operator.join.sql

joins:
- match: (?i)\b(?:inner|(?:full\s+)?outer|cross|left|right)\s+join\b
scope: keyword.other.DML.sql
push: maybe-subquery
push: [join-on, maybe-subquery]

column-alias:
- meta_content_scope: meta.column-name.sql constant.other.placeholder.sql
Expand All @@ -325,7 +354,27 @@ contexts:
table-alias:
- match: \b(?i:as)\b
scope: keyword.operator.assignment.alias.sql
- match: (?i)(?=\b(?:from|order|group|select|where|inner|outer|left|right|join|on|set|;|with)\b)
- match: (?={{reserved}})
pop: true
- match: (?=\S)
set: [after-table-alias, table-name, single-identifier]

after-table-alias:
- match: (?=\S)
pop: true

join-on:
- match: (?i)\bon\b
scope: keyword.operator.join.sql
push: [join-operators, column-alias, single-identifier-after-whitespace]
- match: (?=\S)
pop: true

join-operators:
- include: operators
- match: (?=\S)
set: [table-name, single-identifier]
set: [column-alias, single-identifier-after-whitespace]

set:
- match: (?=\S)
pop: true
42 changes: 40 additions & 2 deletions SQL/TSQL.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ contexts:

types:
- meta_append: true
- match: (?i:\b(?:smallint|sysname)\b)
- match: (?i:\b(?:smallint|sysname|uniqueidentifier|decimal)\b)
scope: storage.type.sql

statements:
Expand Down Expand Up @@ -207,7 +207,8 @@ contexts:
scope: keyword.control.conditional.case.sql
push: inside-case-expression
- include: variables
- match: \b(?i:output|nowait)\b
- include: with
- match: \b(?i:output|over|partition\s+by)\b
scope: keyword.other.sql
- match: \b(?i:cursor\s+for|open|fetch(?:(?:\s+next)?\s+from)?|close|deallocate)\b
scope: keyword.other.sql
Expand Down Expand Up @@ -264,3 +265,40 @@ contexts:
set: [cursor-name, single-identifier]
- match: (?=\S)
pop: true

set:
- meta_prepend: true
- match: \b(?i:nocount)\b
scope: constant.language.switch.tsql
- match: \b(?i:on|off)\b
scope: constant.language.boolean.tsql
- match: (?=\S)
pop: true

after-table-alias:
- meta_prepend: true
- include: with

with:
- match: (?i)\bwith\b
scope: keyword.other.DML.sql
push: with-paren

with-paren:
- match: \(
scope: punctuation.section.group.begin.sql
set: inside-with-group
- match: \b(?i:nowait)\b
scope: keyword.other.tsql
- match: (?=\S)
pop: true

inside-with-group:
- meta_scope: meta.group.sql
- match: \)
scope: punctuation.section.group.end.sql
pop: true
- match: \w+
scope: constant.language.with.tsql
- match: ','
scope: punctuation.separator.sequence.tsql
26 changes: 15 additions & 11 deletions SQL/syntax_test_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ SELECT "My /* Crazy Column Name" FROM my_table;


;CREATE TABLE foo (id INTEGER PRIMARY KEY);
-- <- keyword.other.create
--^^^^^ keyword.other.create
-- <- meta.create keyword.other.ddl
--^^^^^ keyword.other.ddl
-- ^^^^^ keyword.other
-- ^^^ entity.name.function
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - entity.name.function

create table some_schema.test2( id serial );
--^^^^ meta.create keyword.other.create
--^^^^ meta.create keyword.other.ddl
-- ^^^^^ meta.create keyword.other
-- ^^^^^^^^^^^^^^^^^ entity.name.function
-- ^ punctuation.accessor.dot
-- ^^^^^^^^^^^^^^ - entity.name.function

create table some_schema . test2 ( id serial );
--^^^^ meta.create keyword.other.create
--^^^^ meta.create keyword.other.ddl
-- ^^^^^ meta.create keyword.other
-- ^^^^^^^^^^^^^^^^^^^ entity.name
-- ^ punctuation.accessor.dot
-- ^^^^^^^^^^^^^^^ - entity.name.function

create table "testing123" (id integer);
--^^^^ meta.create keyword.other.create
--^^^^ meta.create keyword.other.ddl
-- ^^^^^ meta.create keyword.other
-- ^ punctuation.definition.identifier.begin
-- ^^^^^^^^^^ entity.name.function
-- ^ punctuation.definition.identifier.end

create table `dbo`."testing123" (id integer);
--^^^^ meta.create keyword.other.create
--^^^^ meta.create keyword.other.ddl
-- ^^^^^ meta.create keyword.other
-- ^^^^^^^^^^^^^^^^^^ entity.name.function
-- ^ punctuation.accessor.dot
Expand Down Expand Up @@ -130,18 +130,22 @@ create fulltext index if not exists `myindex` ON mytable;
-- ^^^^^^^^^^^^^^ keyword.other.sql

ALTER TABLE dbo.testing123 ADD COLUMN mycolumn longtext;
-- ^^ keyword.other
-- ^^^^^ keyword.other.table
-- ^^^ keyword.other.add.sql
-- ^^^^^^ keyword.other.sql
-- ^^^^^^^^ meta.alter keyword.other.ddl
-- ^^^ keyword.other.ddl.sql
-- ^^^^^^ keyword.other.ddl.sql
-- ^^^^^^^^ meta.alter meta.column-name constant.other.placeholder
-- ^^^^^^^^ storage.type.sql

ALTER TABLE testing123 CHANGE COLUMN mycolumn mycolumn ENUM('foo', 'bar');
-- ^^^^ storage.type.sql

DROP TABLE IF EXISTS testing123;
-- <- meta.drop.sql keyword.other.create.sql
-- <- meta.drop.sql keyword.other.ddl.sql
-- ^^^^^^^ meta.drop keyword.other.ddl
-- ^^ meta.drop keyword.control.flow
-- ^^^^^^ keyword.operator.logical.sql
-- ^^^^^^^^^^ meta.table-name constant.other.placeholder
-- ^ punctuation.terminator.statement

select *
from some_table
Expand Down
Loading

0 comments on commit 2541790

Please sign in to comment.