Skip to content

Commit

Permalink
[Python] Add SQL syntax highlighting in f-strings
Browse files Browse the repository at this point in the history
Resolves #3885
  • Loading branch information
deathaxe committed Dec 13, 2023
1 parent 9ff949b commit 0ac4dae
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
98 changes: 97 additions & 1 deletion Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -2826,11 +2826,35 @@ contexts:
push: triple-double-quoted-f-string-body

triple-double-quoted-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.double.block.python
- include: triple-double-quoted-string-end
- include: triple-double-quoted-f-string-syntax

triple-double-quoted-f-string-syntax:
# Single-line string, unicode or not, starting with a SQL keyword
- match: (?={{sql_indicator}})
set: triple-double-quoted-sql-f-string-body
# Single-line string, unicode or not
- match: (?=\S)
set: triple-double-quoted-plain-f-string-body

triple-double-quoted-plain-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.double.block.python
- include: triple-double-quoted-string-end
- include: triple-double-quoted-f-string-content

triple-double-quoted-sql-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python
- include: triple-double-quoted-string-end
- match: ''
push: scope:source.sql
with_prototype:
- include: triple-double-quoted-string-pop
- include: triple-double-quoted-f-string-content

triple-double-quoted-f-string-content:
- include: string-prototype
- include: string-continuations
Expand Down Expand Up @@ -3168,11 +3192,35 @@ contexts:
push: double-quoted-f-string-body

double-quoted-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.double.python
- include: double-quoted-string-end
- include: double-quoted-f-string-syntax

double-quoted-f-string-syntax:
# Single-line string, unicode or not, starting with a SQL keyword
- match: (?={{sql_indicator}})
set: double-quoted-sql-f-string-body
# Single-line string, unicode or not
- match: (?=\S)
set: double-quoted-plain-f-string-body

double-quoted-plain-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.double.python
- include: double-quoted-string-end
- include: double-quoted-f-string-content

double-quoted-sql-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python
- include: double-quoted-string-end
- match: ''
push: scope:source.sql
with_prototype:
- include: double-quoted-string-pop
- include: double-quoted-f-string-content

double-quoted-f-string-content:
- include: string-prototype
- include: double-quoted-f-string-replacements
Expand All @@ -3188,7 +3236,7 @@ contexts:

double-quoted-u-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.double.block.python
- meta_content_scope: meta.string.python string.quoted.double.python
- include: double-quoted-string-end
- include: double-quoted-u-string-syntax

Expand Down Expand Up @@ -3506,11 +3554,35 @@ contexts:
push: triple-single-quoted-f-string-body

triple-single-quoted-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.single.block.python
- include: triple-single-quoted-string-end
- include: triple-single-quoted-f-string-syntax

triple-single-quoted-f-string-syntax:
# Single-line string, unicode or not, starting with a SQL keyword
- match: (?={{sql_indicator}})
set: triple-single-quoted-sql-f-string-body
# Single-line string, unicode or not
- match: (?=\S)
set: triple-single-quoted-plain-f-string-body

triple-single-quoted-plain-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.single.block.python
- include: triple-single-quoted-string-end
- include: triple-single-quoted-f-string-content

triple-single-quoted-sql-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python
- include: triple-single-quoted-string-end
- match: ''
push: scope:source.sql
with_prototype:
- include: triple-single-quoted-string-pop
- include: triple-single-quoted-f-string-content

triple-single-quoted-f-string-content:
- include: string-prototype
- include: string-continuations
Expand Down Expand Up @@ -3848,11 +3920,35 @@ contexts:
push: single-quoted-f-string-body

single-quoted-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.single.python
- include: single-quoted-string-end
- include: single-quoted-f-string-syntax

single-quoted-f-string-syntax:
# Single-line string, unicode or not, starting with a SQL keyword
- match: (?={{sql_indicator}})
set: single-quoted-sql-f-string-body
# Single-line string, unicode or not
- match: (?=\S)
set: single-quoted-plain-f-string-body

single-quoted-plain-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python string.quoted.single.python
- include: single-quoted-string-end
- include: single-quoted-f-string-content

single-quoted-sql-f-string-body:
- meta_include_prototype: false
- meta_content_scope: meta.string.python
- include: single-quoted-string-end
- match: ''
push: scope:source.sql
with_prototype:
- include: single-quoted-string-pop
- include: single-quoted-f-string-content

single-quoted-f-string-content:
- include: string-prototype
- include: single-quoted-f-string-replacements
Expand Down
28 changes: 28 additions & 0 deletions Python/tests/syntax_test_python_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,34 @@
# ^^^^^^^^^^^ meta.interpolation.python meta.interpolation.python
# ^^^ - meta.interpolation.python meta.interpolation.python

f"""SELECT * FROM {table!s:r}"""
# <- storage.type.string.python
#^^^ meta.string.python string.quoted.double.block.python punctuation.definition.string.begin.python
# ^^^^^^^^^^^^^^ meta.string.python source.sql
# ^^^^^^^^^^^ meta.string.python meta.interpolation.python
# ^^^ meta.string.python string.quoted.double.block.python punctuation.definition.string.end.python

f'''SELECT * FROM {table!s:r}'''
# <- storage.type.string.python
#^^^ meta.string.python string.quoted.single.block.python punctuation.definition.string.begin.python
# ^^^^^^^^^^^^^^ meta.string.python source.sql
# ^^^^^^^^^^^ meta.string.python meta.interpolation.python
# ^^^ meta.string.python string.quoted.single.block.python punctuation.definition.string.end.python

f"SELECT * FROM {table!s:r}"
# <- storage.type.string.python
#^ meta.string.python string.quoted.double.python punctuation.definition.string.begin.python
# ^^^^^^^^^^^^^^ meta.string.python source.sql
# ^^^^^^^^^^^ meta.string.python meta.interpolation.python
# ^ meta.string.python string.quoted.double.python punctuation.definition.string.end.python

f'SELECT * FROM {table!s:r}'
# <- storage.type.string.python
#^ meta.string.python string.quoted.single.python punctuation.definition.string.begin.python
# ^^^^^^^^^^^^^^ meta.string.python source.sql
# ^^^^^^^^^^^ meta.string.python meta.interpolation.python
# ^ meta.string.python string.quoted.single.python punctuation.definition.string.end.python

rf"{value:{width!s:d}}"
# <- storage.type.string - meta.string - string
#^ storage.type.string - meta.string - string
Expand Down

0 comments on commit 0ac4dae

Please sign in to comment.