diff --git a/README.md b/README.md index b30be061..e1ca0f6a 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/src/scoping/langs/rust.rs b/src/scoping/langs/rust.rs index 31859ef1..a44f7d13 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -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`. @@ -195,6 +197,11 @@ impl From 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 . diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index d6c0b8a7..68325e19 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -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"), diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index b48fb919..2da4139c 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -318,3 +318,7 @@ where impl X for Y {} impl X for Y {} impl X for Y {} + +impl PubStruct { + const PANIC: () = panic!("bam!"); +} diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_identifier.snap b/tests/langs/snapshots/r#mod__langs__base.rs_identifier.snap index 8f472706..4a1126c1 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_identifier.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_identifier.snap @@ -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: " ^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_impl-fn.snap b/tests/langs/snapshots/r#mod__langs__base.rs_impl-fn.snap new file mode 100644 index 00000000..74c2decc --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_impl-fn.snap @@ -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(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: "^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_impl-type.snap b/tests/langs/snapshots/r#mod__langs__base.rs_impl-type.snap index 2390136c..22720b43 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_impl-type.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_impl-type.snap @@ -143,3 +143,12 @@ expression: inscope_parts - n: 302 l: "impl Y {}\n" m: "^^^^^^^^^^^^^^^ " +- n: 322 + l: "impl PubStruct {\n" + m: ^^^^^^^^^^^^^^^^^^ +- n: 323 + l: " const PANIC: () = panic!(\"bam!\");\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- n: 324 + l: "}\n" + m: "^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_impl.snap b/tests/langs/snapshots/r#mod__langs__base.rs_impl.snap index 6bda46a5..c80b0b5a 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_impl.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_impl.snap @@ -179,3 +179,12 @@ expression: inscope_parts - n: 320 l: "impl X 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: "^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap index f5f95274..3cb4e33b 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap @@ -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: " ^^^^ "