diff --git a/README.md b/README.md index bcb4365e..b0dafaba 100644 --- a/README.md +++ b/README.md @@ -1494,6 +1494,7 @@ Language scopes: - pub-super-fn: Function definitions marked `pub(super)` - mod: `mod` blocks - mod-tests: `mod tests` blocks + - type-def: Type definitions (`struct`, `enum`, `union`) --rust-query Scope Rust code using a custom tree-sitter query. diff --git a/src/scoping/langs/rust.rs b/src/scoping/langs/rust.rs index 50c6f76d..584aa4e6 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -64,6 +64,8 @@ pub enum PreparedRustQuery { Mod, /// `mod tests` blocks. ModTests, + /// Type definitions (`struct`, `enum`, `union`). + TypeDef, } impl From for TSQuery { @@ -201,6 +203,16 @@ impl From for TSQuery { ) @mod_tests "# } + PreparedRustQuery::TypeDef => { + r" + [ + (struct_item) + (enum_item) + (union_item) + ] + @typedef + " + } }, ) .expect("Prepared queries to be valid") diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index c1d12fc7..ef1c87ae 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -302,6 +302,11 @@ impl InScopeLinePart { include_str!("rust/base.rs"), Rust::new(CodeQuery::Prepared(PreparedRustQuery::ModTests)), )] +#[case( + "base.rs_typedefs", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::TypeDef)), +)] #[case( "base.tf_variable-block", include_str!("hcl/base.tf"), diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index 270ab7ad..c5c6035c 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -266,3 +266,9 @@ pub enum PubEnum {} pub(crate) enum PubCrateEnum {} pub(self) enum PubSelfEnum {} pub(super) enum PubSuperEnum {} + +#[repr(C)] +union MyUnion { + f1: u32, + f2: f32, +} diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap index 16134622..888b2f18 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap @@ -23,3 +23,6 @@ expression: inscope_parts - n: 250 l: " #[test]\n" m: " ^^^^ " +- n: 270 + l: "#[repr(C)]\n" + m: " ^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_typedefs.snap b/tests/langs/snapshots/r#mod__langs__base.rs_typedefs.snap new file mode 100644 index 00000000..52fca3c7 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_typedefs.snap @@ -0,0 +1,64 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 70 + l: "struct TestStruct {\n" + m: ^^^^^^^^^^^^^^^^^^^^^ +- n: 71 + l: " instance_var: String,\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- n: 72 + l: "}\n" + m: "^ " +- n: 111 + l: "enum TestEnum {\n" + m: ^^^^^^^^^^^^^^^^^ +- n: 112 + l: " VariantOne,\n" + m: ^^^^^^^^^^^^^^^^^ +- n: 113 + l: " VariantTwo,\n" + m: ^^^^^^^^^^^^^^^^^ +- n: 114 + l: " VariantOther,\n" + m: ^^^^^^^^^^^^^^^^^^^ +- n: 115 + l: "}\n" + m: "^ " +- n: 260 + l: "pub struct PubStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 261 + l: "pub(crate) struct PubCrateStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 262 + l: "pub(self) struct PubSelfStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 263 + l: "pub(super) struct PubSuperStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 265 + l: "pub enum PubEnum {}\n" + m: "^^^^^^^^^^^^^^^^^^^ " +- n: 266 + l: "pub(crate) enum PubCrateEnum {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 267 + l: "pub(self) enum PubSelfEnum {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 268 + l: "pub(super) enum PubSuperEnum {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " +- n: 271 + l: "union MyUnion {\n" + m: ^^^^^^^^^^^^^^^^^ +- n: 272 + l: " f1: u32,\n" + m: ^^^^^^^^^^^^^^ +- n: 273 + l: " f2: f32,\n" + m: ^^^^^^^^^^^^^^ +- n: 274 + l: "}\n" + m: "^ "