languages: Highlight C/C++ preprocessor call arguments (#66875)

## Summary

Arguments of `preproc_call` nodes, like the `pack(push, 4)` in `#pragma
pack(push, 4)` or the `FOO` in `#undef FOO`, had no highlight capture,
so they always rendered as plain text. `#define` bodies are covered by
the injection, and `#if` conditions are parsed as expressions, but
nothing reached `preproc_call`'s `preproc_arg`.

I added a `(preproc_call argument: (preproc_arg) @preproc)` rule to both
the C and C++ highlight queries. I went with `@preproc` because every
built-in theme already defines it, and it makes the whole directive line
one color, like VS Code does. Happy to switch to `@constant.macro` or
`@string` if you'd prefer.

| Before | After |
| --- | --- |
| <img alt="preproc-before"
src="https://github.com/user-attachments/assets/95fa0660-c41f-4433-98c7-887d9871fe1d"
/> | <img alt="preproc-after"
src="https://github.com/user-attachments/assets/7c4fa978-a6c1-4707-a627-6d7e32dd897a"
/> |
| <img alt="preproc-before-light"
src="https://github.com/user-attachments/assets/3d2479ce-cd04-42ae-bfeb-e5c8c49fb9a6"
/> | <img alt="preproc-after-light"
src="https://github.com/user-attachments/assets/8d342f73-25e2-4dae-b984-72b88fd23287"
/> |


`#define FOO 1` and `#if defined(BAR)` look the same before and after.

Closes #66675

## Testing

- Added a query test for C and one for C++, checking that `#pragma` and
`#undef` arguments are captured as `preproc` and that a `#define` body
isn't. Both fail without the query change.
- `cargo test -p languages` passes (105 tests), and `./script/clippy -p
languages` is clean.
- Built the branch on macOS and compared it with Zed 1.28.1 on the same
file, in One Dark and One Light (screenshots above).

To try it: open a `.c` or `.cpp` file and type `#pragma pack(push, 4)` /
`#undef FOO`. The arguments should now take the directive's color.

I used Claude Code to help with this change and tested it myself.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Improved syntax highlighting of `#pragma` and `#undef` arguments in C
and C++.

---------

Co-authored-by: imryanthedev <352313849+imryanthedev@users.noreply.github.com>
This commit is contained in:
Ryan The Dev
2026-11-12 22:39:26 +00:00
committed by GitHub
parent 8e474e1598
commit 27bbbc1cc4
6 changed files with 91 additions and 2 deletions
Generated
+1
View File
@@ -9772,6 +9772,7 @@ dependencies = [
"globset",
"gpui",
"grammars",
"indoc",
"itertools 0.14.0",
"json_schema_store",
"language",
+3
View File
@@ -41,6 +41,9 @@
(preproc_directive)
] @keyword.preproc @preproc
(preproc_call
argument: (preproc_arg) @preproc)
[
"="
"+="
+3
View File
@@ -199,6 +199,9 @@ type: (primitive_type) @type.builtin
(preproc_directive)
] @keyword.preproc @preproc
(preproc_call
argument: (preproc_arg) @preproc)
(preproc_defined
"defined" @keyword.preproc)
+1
View File
@@ -69,6 +69,7 @@ util.workspace = true
[dev-dependencies]
fs = { workspace = true, features = ["test-support"] }
indoc.workspace = true
language = { workspace = true, features = ["test-support"] }
pretty_assertions.workspace = true
tempfile.workspace = true
+41
View File
@@ -427,9 +427,11 @@ async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServ
#[cfg(test)]
mod tests {
use gpui::{AppContext as _, BorrowAppContext, TestAppContext};
use indoc::indoc;
use language::{AutoindentMode, Buffer};
use settings::SettingsStore;
use std::num::NonZeroU32;
use tree_sitter::StreamingIterator;
use unindent::Unindent;
#[gpui::test]
@@ -739,4 +741,43 @@ mod tests {
buffer
});
}
#[test]
fn test_c_highlights_query_captures_preproc_call_arguments() {
let language: tree_sitter::Language = tree_sitter_c::LANGUAGE.into();
let query_source = include_str!("../../grammars/src/c/highlights.scm");
let query = tree_sitter::Query::new(&language, query_source)
.expect("C highlights query should compile");
let source = indoc! {"
#pragma pack(push, 4)
#undef FOO
#define BAR 42
"};
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).unwrap();
let tree = parser.parse(source, None).expect("C source should parse");
let mut cursor = tree_sitter::QueryCursor::new();
let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());
let mut captures = Vec::new();
while let Some(query_match) = matches.next() {
for capture in query_match.captures() {
let capture_name = query.capture_names()[capture.index as usize];
let captured_text = &source[capture.node.byte_range()];
captures.push((captured_text.to_owned(), capture_name.to_owned()));
}
}
assert!(captures.contains(&("#pragma".into(), "preproc".into())));
assert!(captures.contains(&("pack(push, 4)".into(), "preproc".into())));
assert!(captures.contains(&("FOO".into(), "preproc".into())));
assert!(!captures.contains(&("42".into(), "preproc".into())));
}
}
+42 -2
View File
@@ -11,6 +11,7 @@ pub(crate) fn semantic_token_rules() -> SemanticTokenRules {
#[cfg(test)]
mod tests {
use gpui::{AppContext as _, BorrowAppContext, TestAppContext};
use indoc::indoc;
use language::{AutoindentMode, Buffer};
use settings::SettingsStore;
use std::num::NonZeroU32;
@@ -659,13 +660,13 @@ mod tests {
let query = tree_sitter::Query::new(&language, query_source)
.expect("C++ highlights query should compile");
let source = r#"
let source = indoc! {"
auto mutable_lambda = []() mutable {};
auto static_lambda = []() static {};
#if defined(MY_MACRO)
#endif
"#;
"};
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).unwrap();
@@ -691,4 +692,43 @@ mod tests {
assert!(captures.contains(&("defined".into(), "keyword.preproc".into())));
assert!(captures.contains(&("MY_MACRO".into(), "constant.macro".into())));
}
#[test]
fn test_cpp_highlights_query_captures_preproc_call_arguments() {
let language: tree_sitter::Language = tree_sitter_cpp::LANGUAGE.into();
let query_source = include_str!("../../grammars/src/cpp/highlights.scm");
let query = tree_sitter::Query::new(&language, query_source)
.expect("C++ highlights query should compile");
let source = indoc! {"
#pragma pack(push, 4)
#undef FOO
#define BAR 42
"};
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).unwrap();
let tree = parser.parse(source, None).expect("C++ source should parse");
let mut cursor = tree_sitter::QueryCursor::new();
let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());
let mut captures = Vec::new();
while let Some(query_match) = matches.next() {
for capture in query_match.captures() {
let capture_name = query.capture_names()[capture.index as usize];
let captured_text = &source[capture.node.byte_range()];
captures.push((captured_text.to_owned(), capture_name.to_owned()));
}
}
assert!(captures.contains(&("#pragma".into(), "preproc".into())));
assert!(captures.contains(&("pack(push, 4)".into(), "preproc".into())));
assert!(captures.contains(&("FOO".into(), "preproc".into())));
assert!(!captures.contains(&("42".into(), "preproc".into())));
}
}