rust: Refine sql! syntax injection (#58778)

Thanks for taking a look at this. I think this is a small but worthwhile
refinement to Zed's existing Rust SQL syntax injection behaviour.

**Before screenshot**

<img width="1509" height="992" alt="202606072151kst-before"
src="https://github.com/user-attachments/assets/37247d48-51cf-47c7-8a34-07c821869d0d"
/>

**After screenshot**

<img width="1648" height="1196" alt="202606072159kst-after"
src="https://github.com/user-attachments/assets/c10de0fc-3a30-4750-8f70-a050d74d4150"
/>


The main rationale is that Zed already recognises `sql!` as an explicit
Rust SQL injection marker. This PR does not introduce a new broad
heuristic, infer SQL from arbitrary Rust strings, or add
framework-specific matching. It narrows the existing `sql!` behaviour so
literal forms inject only the Rust `string_content` as SQL, rather than
the whole macro payload.

That distinction matters for real Rust SQL authoring. The current broad
payload capture can work for simple token-tree examples like:

```rust
sql!(SELECT * FROM carts);
```

but practical embedded SQL is often written as a normal string or raw
string, especially for PostgreSQL queries with single-quoted values,
multiline statements, dollar parameters, comments, and other syntax:

```rust
sql!(
    r#"
    SELECT *
    FROM carts
    WHERE status IN ('open', 'checkout_started')
    "#
);
```

For those literal forms, treating Rust delimiters such as `"`, `r#"`, or
`"#` as part of the SQL layer makes the injected SQL range less precise.
This PR fixes that by injecting the literal body only, whilst preserving
the historical unquoted token-tree form.

The practical benefit for Rust developers is straightforward: SQL
snippets, migrations, test fixtures, schema fragments, and other
explicit `sql!` blocks become easier to read and review in Zed without
requiring database-aware LSP support or any project-specific
configuration. The change is also deliberately conservative: it is
syntactic, opt-in through the existing `sql!` marker, test-backed, and
leaves non-`sql!` macros alone.

This should be a good fit for upstream because it improves behaviour Zed
already supports instead of expanding the matching policy into ambiguous
territory. It also follows the same general pattern used elsewhere in
Zed's injections: when a host-language string literal contains an
injected language, capture the literal body rather than the
host-language wrapper syntax.

I also want to call out related work in
https://github.com/zed-industries/zed/pull/58016 and explicitly support
that PR as well.

The two changes are different in scope. This PR refines the existing
generic `sql!` marker and does not add SQLx matching. PR #58016 is the
SQLx-specific improvement, targeting `sqlx::query!`-style macros and
functions where many Rust backend developers write production queries
directly. That matters because SQLx users should not need to wrap SQLx
calls in another marker macro just to get highlighting; SQLx's
compile-time checking and call-site ergonomics are part of its value.

I see these as complementary rather than competing improvements. This PR
covers explicit generic SQL contexts such as snippets, migrations,
fixtures, and non-SQLx embedded SQL. PR #58016 covers first-class SQLx
call sites. Together, they move Zed toward a much stronger Rust
embedded-SQL editing experience while keeping each individual change
focused and reviewable.

---

Self-Review Checklist:

- [X] I've reviewed my own diff for quality, security, and reliability
- [X] Unsafe blocks (if any) have justifying comments
- [X] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [X] Tests cover the new/changed behavior
- [X] Performance impact has been considered and is acceptable

Release Notes:

- Improved SQL syntax highlighting in Rust `sql!` string literals.
This commit is contained in:
Jung Woong Park
2026-10-16 13:30:43 +00:00
committed by GitHub
parent 8c8ada0fbf
commit 14119f8d87
2 changed files with 187 additions and 3 deletions
+18 -1
View File
@@ -33,8 +33,25 @@
(scoped_identifier
(identifier) @_macro_name .)
]
(token_tree
[
(string_literal
(string_content) @injection.content)
(raw_string_literal
(string_content) @injection.content)
])
(#set! injection.language "sql")
(#any-of? @_macro_name "sql"))
(macro_invocation
macro: [
(identifier) @_macro_name
(scoped_identifier
(identifier) @_macro_name .)
]
(token_tree) @injection.content
(#any-of? @_macro_name "sql")
(_) @injection.content
(#not-match? @injection.content "^[({\\[]\\s*(\"|r#*\")")
(#set! injection.language "sql"))
; lazy_regex
@@ -481,6 +481,110 @@ fn test_rust_json_macro_empty_string_highlighting(cx: &mut App) {
);
}
#[gpui::test]
fn test_rust_sql_macro_injection_without_registered_sql(cx: &mut App) {
let registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
let language = rust_lang();
registry.add(language.clone());
for source in [
r#"fn main() { sql!("SELECT * FROM carts"); }"#,
r##"fn main() { sql!(r#"SELECT * FROM carts WHERE status = 'open'"#); }"##,
r####"fn main() { sql!(r###"SELECT * FROM carts WHERE note LIKE '%"##%'"###); }"####,
"fn main() { sql!(SELECT * FROM carts); }",
] {
let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), source);
let mut syntax_map = SyntaxMap::new(&buffer);
syntax_map.set_language_registry(registry.clone());
syntax_map.reparse(language.clone(), &buffer);
assert!(
syntax_map.contains_unknown_injections(),
"expected sql! to trigger an unknown SQL injection in {source:?}"
);
}
for source in [
r#"fn main() { not_sql!("SELECT * FROM carts"); }"#,
r##"fn main() { other_macro!(r#"SELECT * FROM carts"#); }"##,
r#"fn main() { plain_function("SELECT * FROM carts"); }"#,
] {
let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), source);
let mut syntax_map = SyntaxMap::new(&buffer);
syntax_map.set_language_registry(registry.clone());
syntax_map.reparse(language.clone(), &buffer);
assert!(
!syntax_map.contains_unknown_injections(),
"expected non-sql! code not to trigger an unknown SQL injection in {source:?}"
);
}
}
#[gpui::test]
fn test_rust_sql_macro_injection_ranges(cx: &mut App) {
let registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
let language = rust_lang();
registry.add(language.clone());
registry.add(Arc::new(sql_lang()));
let buffer = Buffer::new(
ReplicaId::LOCAL,
BufferId::new(1).unwrap(),
r####"
fn main() {
sql!("SELECT normal FROM carts");
sql!("SELECT trailing FROM carts",);
sql!(r#"SELECT raw FROM carts"#);
sql!(r###"SELECT raw_hashes FROM carts WHERE note LIKE '%"##%'"###);
sql!(
r#"
SELECT multiline
FROM carts
"#
);
sql!(SELECT unquoted FROM carts);
not_sql!(r#"SELECT not_sql FROM carts"#);
}
"####
.unindent(),
);
let mut syntax_map = SyntaxMap::new(&buffer);
syntax_map.set_language_registry(registry);
syntax_map.reparse(language, &buffer);
for text in [
"normal",
"trailing",
"raw",
"raw_hashes",
"multiline",
"unquoted",
] {
assert_language_range_count_at_text(&syntax_map, &buffer, "SQL", text, 1);
}
for text in [
r#""SELECT normal"#,
r#""SELECT trailing"#,
r##"r#"SELECT raw"##,
r####"r###"SELECT raw_hashes"####,
r#"not_sql"#,
] {
assert_language_range_count_at_text(&syntax_map, &buffer, "SQL", text, 0);
}
for (text, offset) in [
(r#"carts");"#, "carts".len()),
(r#"carts",);"#, "carts".len()),
(r##"carts"#);"##, "carts".len()),
(r####"%'"###);"####, "%'".len()),
] {
assert_language_range_count_at_text_offset(&syntax_map, &buffer, "SQL", text, offset, 0);
}
}
#[gpui::test]
fn test_typing_multiple_new_injections(cx: &mut App) {
let (buffer, syntax_map) = test_edit_sequence(
@@ -1930,12 +2034,16 @@ fn comment_lang() -> Language {
)
}
/// Stands in for the real SQL language, which this crate doesn't depend on. The grammar
/// is irrelevant here; only the layers Python's injection query produces are asserted on.
/// Stands in for the real SQL language, which this crate does not depend on.
/// The grammar is irrelevant because these tests assert injected language ranges.
fn sql_lang() -> Language {
Language::new(
LanguageConfig {
name: "SQL".into(),
matcher: Arc::new(LanguageMatcher {
path_suffixes: vec!["sql".into()],
..Default::default()
}),
..Default::default()
},
Some(tree_sitter_json::LANGUAGE.into()),
@@ -1964,6 +2072,65 @@ fn range_for_text(buffer: &Buffer, text: &str) -> Range<usize> {
start..start + text.len()
}
#[track_caller]
fn assert_language_range_count_at_text(
syntax_map: &SyntaxMap,
buffer: &Buffer,
language_name: &str,
text: &str,
expected_count: usize,
) {
assert_language_range_count_at_text_offset(
syntax_map,
buffer,
language_name,
text,
0,
expected_count,
);
}
#[track_caller]
fn assert_language_range_count_at_text_offset(
syntax_map: &SyntaxMap,
buffer: &Buffer,
language_name: &str,
text: &str,
offset_in_text: usize,
expected_count: usize,
) {
let offset = range_for_text(buffer, text).start;
let offset = offset + offset_in_text;
let actual_count = language_ranges(syntax_map, buffer, language_name)
.into_iter()
.filter(|range| range.start <= offset && offset < range.end)
.count();
assert_eq!(
actual_count, expected_count,
"wrong number of {language_name} ranges at {text:?}"
);
}
fn language_ranges(
syntax_map: &SyntaxMap,
buffer: &Buffer,
language_name: &str,
) -> Vec<Range<usize>> {
syntax_map
.layers_for_range(0..buffer.len(), buffer, true)
.filter(|layer| layer.language.name().as_ref() == language_name)
.flat_map(|layer| {
let layer_offset = layer.offset.0;
layer
.tree
.included_ranges()
.into_iter()
.map(move |range| layer_offset + range.start_byte..layer_offset + range.end_byte)
})
.collect()
}
#[track_caller]
fn assert_layers_for_range(
syntax_map: &SyntaxMap,