From 8c922bfff87850f59bb5314bbd5caee73da15b04 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Thu, 15 Aug 2024 16:53:51 +0200 Subject: [PATCH] feat(rust): Scope `{,pub({crate,self,super})} struct` definitions --- README.md | 31 ++++++++++-------- src/scoping/langs/rust.rs | 32 +++++++++++++++++++ tests/langs/mod.rs | 25 +++++++++++++++ tests/langs/rust/base.rs | 5 +++ ...#mod__langs__base.rs_pub-crate-struct.snap | 7 ++++ ...r#mod__langs__base.rs_pub-self-struct.snap | 7 ++++ .../r#mod__langs__base.rs_pub-struct.snap | 7 ++++ ...#mod__langs__base.rs_pub-super-struct.snap | 7 ++++ .../r#mod__langs__base.rs_struct.snap | 25 +++++++++++++++ 9 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_pub-crate-struct.snap create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_pub-self-struct.snap create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_pub-struct.snap create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_pub-super-struct.snap create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_struct.snap diff --git a/README.md b/README.md index 19b28522..ccb7ca64 100644 --- a/README.md +++ b/README.md @@ -1467,20 +1467,25 @@ Language scopes: [env: RUST=] Possible values: - - comments: Comments (line and block styles; excluding doc comments; comment - chars incl.) - - doc-comments: Doc comments (comment chars included) - - uses: Use statements (paths only; excl. `use`/`as`/`*`) - - strings: Strings (regular, raw, byte; includes interpolation parts in + - comments: Comments (line and block styles; excluding doc comments; + comment chars incl.) + - doc-comments: Doc comments (comment chars included) + - uses: Use statements (paths only; excl. `use`/`as`/`*`) + - strings: Strings (regular, raw, byte; includes interpolation parts in format strings!) - - attribute: Attributes like `#[attr]` - - fn: Function definitions - - pub-fn: Function definitions marked `pub` - - pub-crate-fn: Function definitions marked `pub(crate)` - - pub-self-fn: Function definitions marked `pub(self)` - - pub-super-fn: Function definitions marked `pub(super)` - - mod: `mod` blocks - - mod-tests: `mod tests` blocks + - attribute: Attributes like `#[attr]` + - struct: `struct` definitions + - pub-struct: `struct` definitions marked `pub` + - pub-crate-struct: `struct` definitions marked `pub(crate)` + - pub-self-struct: `struct` definitions marked `pub(self)` + - pub-super-struct: `struct` definitions marked `pub(super)` + - fn: Function definitions + - pub-fn: Function definitions marked `pub` + - pub-crate-fn: Function definitions marked `pub(crate)` + - pub-self-fn: Function definitions marked `pub(self)` + - pub-super-fn: Function definitions marked `pub(super)` + - mod: `mod` blocks + - mod-tests: `mod tests` blocks --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 e408cd85..62084fab 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -24,6 +24,16 @@ pub enum PreparedRustQuery { Strings, /// Attributes like `#[attr]`. Attribute, + /// `struct` definitions. + Struct, + /// `struct` definitions marked `pub`. + PubStruct, + /// `struct` definitions marked `pub(crate)`. + PubCrateStruct, + /// `struct` definitions marked `pub(self)`. + PubSelfStruct, + /// `struct` definitions marked `pub(super)`. + PubSuperStruct, /// Function definitions. Fn, /// Function definitions marked `pub`. @@ -82,6 +92,28 @@ impl From for TSQuery { } PreparedRustQuery::Strings => "(string_content) @string", PreparedRustQuery::Attribute => "(attribute) @attribute", + PreparedRustQuery::Struct => "(struct_item) @struct_item", + PreparedRustQuery::PubStruct => { + r#"(struct_item + (visibility_modifier) @vis + (#eq? @vis "pub") + ) @struct_item"# + } + PreparedRustQuery::PubCrateStruct => { + r"(struct_item + (visibility_modifier (crate)) + ) @struct_item" + } + PreparedRustQuery::PubSelfStruct => { + r"(struct_item + (visibility_modifier (self)) + ) @struct_item" + } + PreparedRustQuery::PubSuperStruct => { + r"(struct_item + (visibility_modifier (super)) + ) @struct_item" + } PreparedRustQuery::Fn => "(function_item) @function_item", PreparedRustQuery::PubFn => { r#"(function_item diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index a2b515b5..e3d7de64 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -202,6 +202,31 @@ impl InScopeLinePart { include_str!("rust/base.rs"), Rust::new(CodeQuery::Prepared(PreparedRustQuery::Attribute)), )] +#[case( + "base.rs_struct", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::Struct)), +)] +#[case( + "base.rs_pub-struct", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::PubStruct)), +)] +#[case( + "base.rs_pub-crate-struct", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::PubCrateStruct)), +)] +#[case( + "base.rs_pub-self-struct", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::PubSelfStruct)), +)] +#[case( + "base.rs_pub-super-struct", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::PubSuperStruct)), +)] #[case( "base.rs_fn", include_str!("rust/base.rs"), diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index f763f769..cdec67cc 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -256,3 +256,8 @@ pub(crate) fn function_pub_crate() {} pub(self) fn function_pub_self() {} pub(super) fn function_pub_super() {} + +pub struct PubStruct {} +pub(crate) struct PubCrateStruct {} +pub(self) struct PubSelfStruct {} +pub(super) struct PubSuperStruct {} diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_pub-crate-struct.snap b/tests/langs/snapshots/r#mod__langs__base.rs_pub-crate-struct.snap new file mode 100644 index 00000000..8fd7ab6c --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_pub-crate-struct.snap @@ -0,0 +1,7 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 261 + l: "pub(crate) struct PubCrateStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_pub-self-struct.snap b/tests/langs/snapshots/r#mod__langs__base.rs_pub-self-struct.snap new file mode 100644 index 00000000..71da9cea --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_pub-self-struct.snap @@ -0,0 +1,7 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 262 + l: "pub(self) struct PubSelfStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_pub-struct.snap b/tests/langs/snapshots/r#mod__langs__base.rs_pub-struct.snap new file mode 100644 index 00000000..744e6512 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_pub-struct.snap @@ -0,0 +1,7 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 260 + l: "pub struct PubStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_pub-super-struct.snap b/tests/langs/snapshots/r#mod__langs__base.rs_pub-super-struct.snap new file mode 100644 index 00000000..0f5ebed0 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_pub-super-struct.snap @@ -0,0 +1,7 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 263 + l: "pub(super) struct PubSuperStruct {}\n" + m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_struct.snap b/tests/langs/snapshots/r#mod__langs__base.rs_struct.snap new file mode 100644 index 00000000..414a9a81 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_struct.snap @@ -0,0 +1,25 @@ +--- +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: 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: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "