From 27bbbc1cc480a7500cec122f120e796d1fb8500c Mon Sep 17 00:00:00 2001 From: Ryan The Dev Date: Thu, 12 Nov 2026 22:39:26 +0000 Subject: [PATCH] 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 | | --- | --- | | preproc-before | preproc-after | | preproc-before-light | preproc-after-light | `#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> --- Cargo.lock | 1 + crates/grammars/src/c/highlights.scm | 3 ++ crates/grammars/src/cpp/highlights.scm | 3 ++ crates/languages/Cargo.toml | 1 + crates/languages/src/c.rs | 41 ++++++++++++++++++++++++ crates/languages/src/cpp.rs | 44 ++++++++++++++++++++++++-- 6 files changed, 91 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3165936c56b..6a814c73755 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9772,6 +9772,7 @@ dependencies = [ "globset", "gpui", "grammars", + "indoc", "itertools 0.14.0", "json_schema_store", "language", diff --git a/crates/grammars/src/c/highlights.scm b/crates/grammars/src/c/highlights.scm index b3a3914e465..6464cbc6e69 100644 --- a/crates/grammars/src/c/highlights.scm +++ b/crates/grammars/src/c/highlights.scm @@ -41,6 +41,9 @@ (preproc_directive) ] @keyword.preproc @preproc +(preproc_call + argument: (preproc_arg) @preproc) + [ "=" "+=" diff --git a/crates/grammars/src/cpp/highlights.scm b/crates/grammars/src/cpp/highlights.scm index e86602711e0..0a15661a8b4 100644 --- a/crates/grammars/src/cpp/highlights.scm +++ b/crates/grammars/src/cpp/highlights.scm @@ -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) diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml index f300452a062..99ab108febb 100644 --- a/crates/languages/Cargo.toml +++ b/crates/languages/Cargo.toml @@ -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 diff --git a/crates/languages/src/c.rs b/crates/languages/src/c.rs index fe3b19309d4..13e3065bedb 100644 --- a/crates/languages/src/c.rs +++ b/crates/languages/src/c.rs @@ -427,9 +427,11 @@ async fn get_cached_server_binary(container_dir: PathBuf) -> Option 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()))); + } }