Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Add SQL syntax highlighting in f-strings #3888

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions 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 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