Skip to content

Commit

Permalink
feat(rust): Scope functions inside impl blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 16, 2024
1 parent 83473c3 commit e9c3c16
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,8 @@ Language scopes:
- pub-super-enum: `enum` definitions marked `pub(super)`
- enum-variant: Variant members of `enum` definitions
- fn: Function definitions
- impl-fn: Function definitions inside `impl` blocks (associated
functions/methods)
- priv-fn: Function definitions not marked `pub`
- pub-fn: Function definitions marked `pub`
- pub-crate-fn: Function definitions marked `pub(crate)`
Expand Down
7 changes: 7 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub enum PreparedRustQuery {
EnumVariant,
/// Function definitions.
Fn,
/// Function definitions inside `impl` blocks (associated functions/methods).
ImplFn,
/// Function definitions not marked `pub`.
PrivFn,
/// Function definitions marked `pub`.
Expand Down Expand Up @@ -195,6 +197,11 @@ impl From<PreparedRustQuery> for TSQuery {
}
PreparedRustQuery::EnumVariant => "(enum_variant) @enum_variant",
PreparedRustQuery::Fn => "(function_item) @function_item",
PreparedRustQuery::ImplFn => {
r"(impl_item
body: (_ (function_item) @function)
)"
}
PreparedRustQuery::PrivFn => {
r"(function_item
.
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ impl InScopeLinePart {
include_str!("rust/base.rs"),
Rust::new(CodeQuery::Prepared(PreparedRustQuery::Fn)),
)]
#[case(
"base.rs_impl-fn",
include_str!("rust/base.rs"),
Rust::new(CodeQuery::Prepared(PreparedRustQuery::ImplFn)),
)]
#[case(
"base.rs_pub-priv-fn",
include_str!("rust/base.rs"),
Expand Down
4 changes: 4 additions & 0 deletions tests/langs/rust/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,7 @@ where
impl X for Y {}
impl<T> X for Y<T> {}
impl<T> X<T> for Y {}

impl PubStruct {
const PANIC: () = panic!("bam!");
}
6 changes: 6 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_identifier.snap
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,9 @@ expression: inscope_parts
- n: 314
l: " U::from(self.as_ref().to_owned())\n"
m: " ^^^^ "
- n: 323
l: " const PANIC: () = panic!(\"bam!\");\n"
m: " ^^^^^ "
- n: 323
l: " const PANIC: () = panic!(\"bam!\");\n"
m: " ^^^^^ "
109 changes: 109 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_impl-fn.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 75
l: " fn new() -> Self {\n"
m: " ^^^^^^^^^^^^^^^^^^^^"
- n: 76
l: " TestStruct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^
- n: 77
l: " instance_var: String::from(\"hello\"),\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 78
l: " }\n"
m: ^^^^^^^^^^^
- n: 79
l: " }\n"
m: "^^^^^ "
- n: 86
l: " fn static_decorator<F>(func: F) -> impl Fn()\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 87
l: " where\n"
m: ^^^^^^^^^^^
- n: 88
l: " F: Fn(),\n"
m: ^^^^^^^^^^^^^^^^^^
- n: 89
l: " {\n"
m: ^^^^^^^
- n: 90
l: " // Decorator for static methods.\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 91
l: " move || {\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 92
l: " println!(\"Static method decorator called\");\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 93
l: " func();\n"
m: ^^^^^^^^^^^^^^^^^^^^^
- n: 94
l: " }\n"
m: ^^^^^^^^^^^
- n: 95
l: " }\n"
m: "^^^^^ "
- n: 98
l: " fn instance_method(&mut self) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 99
l: " // Instance method.\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 100
l: " self.instance_var = \"Instance variable\".to_string();\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 101
l: " println!(\"Instance variable is {}\", self.instance_var);\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 102
l: " }\n"
m: "^^^^^ "
- n: 104
l: " fn static_method() {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^"
- n: 105
l: " // Static method.\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 106
l: " println!(\"Inside static method\");\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 107
l: " }\n"
m: "^^^^^ "
- n: 118
l: " fn match_statement(x: TestEnum) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 119
l: " // Function demonstrating match statement.\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 120
l: " match x {\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 121
l: " TestEnum::VariantOne => println!(\"One\"),\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 122
l: " TestEnum::VariantTwo => println!(\"Two\"),\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 123
l: " TestEnum::VariantOther => println!(\"Other\"),\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 124
l: " }\n"
m: ^^^^^^^^^^^
- n: 125
l: " }\n"
m: "^^^^^ "
- n: 313
l: " fn convert(&self) -> U {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 314
l: " U::from(self.as_ref().to_owned())\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 315
l: " }\n"
m: "^^^^^ "
9 changes: 9 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_impl-type.snap
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,12 @@ expression: inscope_parts
- n: 302
l: "impl<T> Y<T> {}\n"
m: "^^^^^^^^^^^^^^^ "
- n: 322
l: "impl PubStruct {\n"
m: ^^^^^^^^^^^^^^^^^^
- n: 323
l: " const PANIC: () = panic!(\"bam!\");\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 324
l: "}\n"
m: "^ "
9 changes: 9 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_impl.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,12 @@ expression: inscope_parts
- n: 320
l: "impl<T> X<T> for Y {}\n"
m: "^^^^^^^^^^^^^^^^^^^^^ "
- n: 322
l: "impl PubStruct {\n"
m: ^^^^^^^^^^^^^^^^^^
- n: 323
l: " const PANIC: () = panic!(\"bam!\");\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 324
l: "}\n"
m: "^ "
3 changes: 3 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_strings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,6 @@ expression: inscope_parts
- n: 297
l: "extern \"C\" fn example_extern() -> i32 {\n"
m: " ^ "
- n: 323
l: " const PANIC: () = panic!(\"bam!\");\n"
m: " ^^^^ "

0 comments on commit e9c3c16

Please sign in to comment.