From 0ac4daeda6b413ef4d2f665d854389a53edc42e9 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Wed, 13 Dec 2023 18:25:38 +0100 Subject: [PATCH 1/2] [Python] Add SQL syntax highlighting in f-strings Resolves #3885 --- Python/Python.sublime-syntax | 98 +++++++++++++++++++++- Python/tests/syntax_test_python_strings.py | 28 +++++++ 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index d05c8f5901..b13e0d8400 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/Python/tests/syntax_test_python_strings.py b/Python/tests/syntax_test_python_strings.py index a07d2f2fc6..a2a3408add 100644 --- a/Python/tests/syntax_test_python_strings.py +++ b/Python/tests/syntax_test_python_strings.py @@ -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 From c903f6eef686fd986d52e2e6215e849cdb69c575 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Wed, 13 Dec 2023 19:01:16 +0100 Subject: [PATCH 2/2] [Pyhton] Revert separately proposed change --- Python/Python.sublime-syntax | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index b13e0d8400..156ffd0a31 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -3236,7 +3236,7 @@ contexts: double-quoted-u-string-body: - meta_include_prototype: false - - meta_content_scope: meta.string.python string.quoted.double.python + - meta_content_scope: meta.string.python string.quoted.double.block.python - include: double-quoted-string-end - include: double-quoted-u-string-syntax