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()))); + } }