From c20cbba0ebb1caf3763d55893c4a11dcc06a2cae Mon Sep 17 00:00:00 2001 From: Jeff Brennan <42007840+jeffbrennan@users.noreply.github.com> Date: Mon, 15 Dec 2025 11:11:07 -0500 Subject: [PATCH] python: Add SQL syntax highlighting (#43756) Release Notes: - Added support for SQL syntax highlighting in Python files ## Summary I am a data engineer who spends a lot of time writing SQL in Python files using Zed. This PR adds support for SQL syntax highlighting with common libraries (like pyspark, polars, pandas) and string variables (prefixed with a `# sql` comment). I referenced [#37605](https://github.com/zed-industries/zed/pull/37605) for this implementation to keep the comment prefix consistent. ## Examples image --------- Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> --- crates/languages/src/python/injections.scm | 31 ++++++++++++++++++++++ docs/src/languages/python.md | 19 +++++++++++++ 2 files changed, 50 insertions(+) diff --git a/crates/languages/src/python/injections.scm b/crates/languages/src/python/injections.scm index 9117c713b98fdd2896b13e4949a77c6489b9ee36..d8470140e999f3dc649c0a498987cfae7df6bf59 100644 --- a/crates/languages/src/python/injections.scm +++ b/crates/languages/src/python/injections.scm @@ -1,3 +1,34 @@ ((comment) @injection.content (#set! injection.language "comment") ) + +; SQL ----------------------------------------------------------------------------- +( + [ + ; function calls + (call + [ + (attribute attribute: (identifier) @function_name) + (identifier) @function_name + ] + arguments: (argument_list + (comment) @comment + (string + (string_content) @injection.content + ) + )) + + ; string variables + ((comment) @comment + . + (expression_statement + (assignment + right: (string + (string_content) @injection.content + ) + ) + )) + ] + (#match? @comment "^(#|#\\s+)(?i:sql)\\s*$") + (#set! injection.language "sql") +) diff --git a/docs/src/languages/python.md b/docs/src/languages/python.md index 5051a72209121176e05d41f57cb8d341db2ca351..2323fe2f9560cf03c586eced0052627705addcc3 100644 --- a/docs/src/languages/python.md +++ b/docs/src/languages/python.md @@ -258,6 +258,25 @@ quote-style = "single" For more details, refer to the Ruff documentation about [configuration files](https://docs.astral.sh/ruff/configuration/) and [language server settings](https://docs.astral.sh/ruff/editors/settings/), and the [list of options](https://docs.astral.sh/ruff/settings/). +### Embedded Language Highlighting + +Zed supports syntax highlighting for code embedded in Python strings by adding a comment with the language name. + +```python +# sql +query = "SELECT * FROM users" + +#sql +query = """ + SELECT * + FROM users +""" + +result = func( #sql + "SELECT * FROM users" +) +``` + ## Debugging Zed supports Python debugging through the `debugpy` adapter. You can start with no configuration or define custom launch profiles in `.zed/debug.json`.