A VS Code extension that provides SQL syntax highlighting and formatting for SQL code embedded in Python strings, including support for Jupyter notebooks.
- Highlights SQL syntax within Python triple-quoted strings marked with
--sql - Supports both regular strings and f-strings
- Works in
.pyfiles and Jupyter notebooks (.ipynb) - No bleeding of syntax highlighting beyond string boundaries
- Formats SQL code with customizable rules
- Accessible via Command Palette or keyboard shortcut
- Smart formatting that preserves BigQuery-specific syntax
-
Install the extension from the VSIX file:
code --install-extension sql-in-python-highlighter-0.2.0.vsix
-
Reload VS Code window:
Cmd+Shift+P→ "Developer: Reload Window"
Mark your SQL strings with --sql comment:
# Regular strings
query = """--sql
SELECT * FROM users
WHERE status = 'active'
"""
# F-strings with variables
user_id = 123
query = f"""--sql
SELECT * FROM users
WHERE id = {user_id}
"""Format your SQL using:
- Keyboard Shortcut:
Shift+Alt+F(when in .sql, .py, or .ipynb files) - Command Palette:
Cmd+Shift+P→ "Format SQL"
- UPPERCASE SQL keywords (SELECT, FROM, WHERE, etc.)
- 4-space indentation throughout
- BigQuery support: Preserves project IDs like
my-project.dataset.table - CTE formatting: Keeps
WITH name AS (on one line with proper indentation - WHERE clause pattern: Automatically adds
WHERE 1 = 1for cleaner AND conditions
Before formatting:
select * from `my-project-id.dataset.table` where status = 'active' and created_at > '2024-01-01'After formatting:
SELECT
*
FROM
`my-project-id.dataset.table`
WHERE 1 = 1
AND status = 'active'
AND created_at > '2024-01-01'.py- Python files.ipynb- Jupyter notebooks.sql- SQL files
To set this extension as your default SQL formatter in VS Code, add to your settings.json:
{
"[sql]": {
"editor.defaultFormatter": "eluc1a.sql-in-python-highlighter"
}
}- Added SQL formatter with ipynb, py, and sql file support
- Added f-string support for syntax highlighting
- Initial release with basic SQL syntax highlighting
- Closing braces may not indent correctly in some cases Please report issues on the GitHub repository.
MIT