Skip to content

Commit

Permalink
feat(rust): Scope {,pub({crate,self,super})} struct definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 15, 2024
1 parent 0e90ed5 commit 8c922bf
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 13 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TREE-SITTER-QUERY>
Scope Rust code using a custom tree-sitter query.
Expand Down
32 changes: 32 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -82,6 +92,28 @@ impl From<PreparedRustQuery> 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
Expand Down
25 changes: 25 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/rust/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 261
l: "pub(crate) struct PubCrateStruct {}\n"
m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 262
l: "pub(self) struct PubSelfStruct {}\n"
m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
7 changes: 7 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_pub-struct.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 260
l: "pub struct PubStruct {}\n"
m: "^^^^^^^^^^^^^^^^^^^^^^^ "
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 263
l: "pub(super) struct PubSuperStruct {}\n"
m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
25 changes: 25 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_struct.snap
Original file line number Diff line number Diff line change
@@ -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: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "

0 comments on commit 8c922bf

Please sign in to comment.