Skip to content

Commit

Permalink
feat(rust): Scope type definitions (struct, enum, union)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 15, 2024
1 parent fab1bc3 commit 33fc03b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TREE-SITTER-QUERY>
Scope Rust code using a custom tree-sitter query.
Expand Down
12 changes: 12 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum PreparedRustQuery {
Mod,
/// `mod tests` blocks.
ModTests,
/// Type definitions (`struct`, `enum`, `union`).
TypeDef,
}

impl From<PreparedRustQuery> for TSQuery {
Expand Down Expand Up @@ -201,6 +203,16 @@ impl From<PreparedRustQuery> for TSQuery {
) @mod_tests
"#
}
PreparedRustQuery::TypeDef => {
r"
[
(struct_item)
(enum_item)
(union_item)
]
@typedef
"
}
},
)
.expect("Prepared queries to be valid")
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
6 changes: 6 additions & 0 deletions tests/langs/rust/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
3 changes: 3 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ expression: inscope_parts
- n: 250
l: " #[test]\n"
m: " ^^^^ "
- n: 270
l: "#[repr(C)]\n"
m: " ^^^^^^^ "
64 changes: 64 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_typedefs.snap
Original file line number Diff line number Diff line change
@@ -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: "^ "

0 comments on commit 33fc03b

Please sign in to comment.