From 2ebcd15718286a693be2218403105393b2a01c8c Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Thu, 15 Aug 2024 16:40:52 +0200 Subject: [PATCH] feat(rust): Scope `pub` function definitions --- README.md | 1 + src/scoping/langs/rust.rs | 8 ++ tests/langs/mod.rs | 5 ++ tests/langs/rust/base.rs | 4 + .../r#mod__langs__base.rs_attribute.snap | 10 +-- .../r#mod__langs__base.rs_comments.snap | 6 +- .../snapshots/r#mod__langs__base.rs_fn.snap | 87 ++++++++++--------- .../r#mod__langs__base.rs_mod-tests.snap | 12 +-- .../snapshots/r#mod__langs__base.rs_mod.snap | 12 +-- .../r#mod__langs__base.rs_pub-fn.snap | 31 +++++++ .../r#mod__langs__base.rs_strings.snap | 28 +++--- .../snapshots/r#mod__langs__base.rs_uses.snap | 4 +- 12 files changed, 133 insertions(+), 75 deletions(-) create mode 100644 tests/langs/snapshots/r#mod__langs__base.rs_pub-fn.snap diff --git a/README.md b/README.md index 8a14a6e7..7e5f643a 100644 --- a/README.md +++ b/README.md @@ -1475,6 +1475,7 @@ Language scopes: format strings!) - attribute: Attributes like `#[attr]` - fn: Function definitions + - pub-fn: Function definitions marked `pub` - mod: `mod` blocks - mod-tests: `mod tests` blocks diff --git a/src/scoping/langs/rust.rs b/src/scoping/langs/rust.rs index c3b36324..196b6ab8 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -26,6 +26,8 @@ pub enum PreparedRustQuery { Attribute, /// Function definitions. Fn, + /// Function definitions marked `pub`. + PubFn, /// `mod` blocks. Mod, /// `mod tests` blocks. @@ -75,6 +77,12 @@ impl From for TSQuery { PreparedRustQuery::Strings => "(string_content) @string", PreparedRustQuery::Attribute => "(attribute) @attribute", PreparedRustQuery::Fn => "(function_item) @function_item", + PreparedRustQuery::PubFn => { + r#"(function_item + (visibility_modifier) @vis + (#eq? @vis "pub") + ) @function_item"# + } PreparedRustQuery::Mod => "(mod_item) @mod_item", PreparedRustQuery::ModTests => { r#"(mod_item diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index 153e0510..aa1c95b3 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -207,6 +207,11 @@ impl InScopeLinePart { include_str!("rust/base.rs"), Rust::new(CodeQuery::Prepared(PreparedRustQuery::Fn)), )] +#[case( + "base.rs_pub-fn", + include_str!("rust/base.rs"), + Rust::new(CodeQuery::Prepared(PreparedRustQuery::PubFn)), +)] #[case( "base.rs_mod", include_str!("rust/base.rs"), diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index b6de0616..6afaaa54 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -194,6 +194,10 @@ async fn async_main() -> Result<(), ()> { Ok(()) } +pub async fn async_pub_fn() -> Result<(), ()> { + Ok(()) +} + // Main execution fn main() { use std::fs::read_to_string; diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap index c63e3146..16134622 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap @@ -8,18 +8,18 @@ expression: inscope_parts - n: 181 l: "#[tokio::main]\n" m: " ^^^^^^^^^^^ " -- n: 237 +- n: 241 l: "#[cfg(feature = \"some-feature\")]\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 238 +- n: 242 l: "#[allow(clippy::single_match_else)]\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 239 +- n: 243 l: "#[cfg(doc)]\n" m: " ^^^^^^^^ " -- n: 242 +- n: 246 l: "#[cfg(test)]\n" m: " ^^^^^^^^^ " -- n: 246 +- n: 250 l: " #[test]\n" m: " ^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap b/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap index b83b5512..045a0c17 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap @@ -83,12 +83,12 @@ expression: inscope_parts - n: 189 l: " // Get key \"hello\"\n" m: " ^^^^^^^^^^^^^^^^^^^^ " -- n: 197 +- n: 201 l: "// Main execution\n" m: "^^^^^^^^^^^^^^^^^ " -- n: 201 +- n: 205 l: " // Lambda expression\n" m: " ^^^^^^^^^^^^^^^^^^^^ " -- n: 204 +- n: 208 l: " // Multiline string\n" m: " ^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_fn.snap b/tests/langs/snapshots/r#mod__langs__base.rs_fn.snap index 8ed4084d..9746e8bc 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_fn.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_fn.snap @@ -380,123 +380,132 @@ expression: inscope_parts - n: 195 l: "}\n" m: "^ " +- n: 197 + l: "pub async fn async_pub_fn() -> Result<(), ()> {\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - n: 198 + l: " Ok(())\n" + m: ^^^^^^^^^^^^ +- n: 199 + l: "}\n" + m: "^ " +- n: 202 l: "fn main() {\n" m: ^^^^^^^^^^^^^ -- n: 199 +- n: 203 l: " use std::fs::read_to_string;\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 200 +- n: 204 l: "\n" m: ^^ -- n: 201 +- n: 205 l: " // Lambda expression\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 202 +- n: 206 l: " let square = |x: i32| -> i32 { x * x };\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 203 +- n: 207 l: "\n" m: ^^ -- n: 204 +- n: 208 l: " // Multiline string\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 205 +- n: 209 l: " let multi_line_str = \"\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 206 +- n: 210 l: "This is a\n" m: ^^^^^^^^^^^ -- n: 207 +- n: 211 l: "multi-line string\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 208 +- n: 212 l: "for testing purposes.\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 209 +- n: 213 l: "\";\n" m: ^^^^^ -- n: 210 +- n: 214 l: "\n" m: ^^ -- n: 211 +- n: 215 l: " let multiline_f_string = format!(\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 212 +- n: 216 l: " \"This is a\\nmultiline{} string\\nspanning several lines\",\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 213 +- n: 217 l: " \"{f_string}\"\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 214 +- n: 218 l: " );\n" m: ^^^^^^^^ -- n: 215 +- n: 219 l: "\n" m: ^^ -- n: 216 +- n: 220 l: " let raw_string = r\"This is a raw string with no special treatment for \\n\";\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 217 +- n: 221 l: " let raw_multiline_string = r#\"\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 218 +- n: 222 l: "This is a raw string with no special treatment for \\n\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 219 +- n: 223 l: "\"#;\n" m: ^^^^^^ -- n: 220 +- n: 224 l: " let bytes_string = b\"This is a bytes string\";\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 221 +- n: 225 l: " let raw_f_string = format!(r\"This is a raw f-string with {}\", raw_string);\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 222 +- n: 226 l: "\n" m: ^^ -- n: 223 +- n: 227 l: " free_func();\n" m: ^^^^^^^^^^^^^^^^^^ -- n: 224 +- n: 228 l: " func_decorator(decorated_func);\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 225 +- n: 229 l: " let mut instance = TestStruct {\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 226 +- n: 230 l: " instance_var: String::new(),\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 227 +- n: 231 l: " };\n" m: ^^^^^^^^ -- n: 228 +- n: 232 l: " instance.instance_method();\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 229 +- n: 233 l: " TestStruct::static_decorator(TestStruct::static_method)();\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 230 +- n: 234 l: " println!(\"{}\", square(5));\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 231 +- n: 235 l: " modify_nonlocal();\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 232 +- n: 236 l: " inplace_operations();\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 233 +- n: 237 l: " control_flow();\n" m: ^^^^^^^^^^^^^^^^^^^^^ -- n: 234 +- n: 238 l: " TestEnum::match_statement(TestEnum::VariantOne);\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 235 +- n: 239 l: "}\n" m: "^ " -- n: 240 +- n: 244 l: "fn function_with_attributes() {}\n" m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 247 +- n: 251 l: " fn some_test() {}\n" m: " ^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_mod-tests.snap b/tests/langs/snapshots/r#mod__langs__base.rs_mod-tests.snap index 1f95212e..853100f3 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_mod-tests.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_mod-tests.snap @@ -2,21 +2,21 @@ source: tests/langs/mod.rs expression: inscope_parts --- -- n: 243 +- n: 247 l: "mod tests {\n" m: ^^^^^^^^^^^^^ -- n: 244 +- n: 248 l: " use super::*;\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 245 +- n: 249 l: "\n" m: ^^ -- n: 246 +- n: 250 l: " #[test]\n" m: ^^^^^^^^^^^^^ -- n: 247 +- n: 251 l: " fn some_test() {}\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 248 +- n: 252 l: "}\n" m: "^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_mod.snap b/tests/langs/snapshots/r#mod__langs__base.rs_mod.snap index 50394078..c20c3ec5 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_mod.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_mod.snap @@ -32,21 +32,21 @@ expression: inscope_parts - n: 25 l: "}\n" m: "^ " -- n: 243 +- n: 247 l: "mod tests {\n" m: ^^^^^^^^^^^^^ -- n: 244 +- n: 248 l: " use super::*;\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 245 +- n: 249 l: "\n" m: ^^ -- n: 246 +- n: 250 l: " #[test]\n" m: ^^^^^^^^^^^^^ -- n: 247 +- n: 251 l: " fn some_test() {}\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 248 +- n: 252 l: "}\n" m: "^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_pub-fn.snap b/tests/langs/snapshots/r#mod__langs__base.rs_pub-fn.snap new file mode 100644 index 00000000..376983be --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.rs_pub-fn.snap @@ -0,0 +1,31 @@ +--- +source: tests/langs/mod.rs +expression: inscope_parts +--- +- n: 16 + l: " pub fn x() {\n" + m: " ^^^^^^^^^^^^^^" +- n: 17 + l: " println!(\"Function x from parent module\");\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- n: 18 + l: " }\n" + m: "^^^^^ " +- n: 22 + l: " pub fn y() {\n" + m: " ^^^^^^^^^^^^^^" +- n: 23 + l: " println!(\"Function y from sibling module\");\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- n: 24 + l: " }\n" + m: "^^^^^ " +- n: 197 + l: "pub async fn async_pub_fn() -> Result<(), ()> {\n" + m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- n: 198 + l: " Ok(())\n" + m: ^^^^^^^^^^^^ +- n: 199 + 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 8e22aa73..9bba3cec 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap @@ -86,45 +86,45 @@ expression: inscope_parts - n: 192 l: " println!(\"got value from the server; result={:?}\", result);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 205 +- n: 209 l: " let multi_line_str = \"\n" m: " ^^" -- n: 206 +- n: 210 l: "This is a\n" m: ^^^^^^^^^^^ -- n: 207 +- n: 211 l: "multi-line string\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 208 +- n: 212 l: "for testing purposes.\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 212 +- n: 216 l: " \"This is a\\nmultiline{} string\\nspanning several lines\",\n" m: " ^^^^^^^^^ " -- n: 212 +- n: 216 l: " \"This is a\\nmultiline{} string\\nspanning several lines\",\n" m: " ^^^^^^^^^^^^^^^^^^ " -- n: 212 +- n: 216 l: " \"This is a\\nmultiline{} string\\nspanning several lines\",\n" m: " ^^^^^^^^^^^^^^^^^^^^^^ " -- n: 213 +- n: 217 l: " \"{f_string}\"\n" m: " ^^^^^^^^^^ " -- n: 216 +- n: 220 l: " let raw_string = r\"This is a raw string with no special treatment for \\n\";\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 218 +- n: 222 l: "This is a raw string with no special treatment for \\n\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 220 +- n: 224 l: " let bytes_string = b\"This is a bytes string\";\n" m: " ^^^^^^^^^^^^^^^^^^^^^^ " -- n: 221 +- n: 225 l: " let raw_f_string = format!(r\"This is a raw f-string with {}\", raw_string);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 230 +- n: 234 l: " println!(\"{}\", square(5));\n" m: " ^^ " -- n: 237 +- n: 241 l: "#[cfg(feature = \"some-feature\")]\n" m: " ^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap b/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap index 31527d2f..a7222c04 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap @@ -26,9 +26,9 @@ expression: inscope_parts - n: 10 l: "use std::sync::atomic::{AtomicI32, Ordering};\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 199 +- n: 203 l: " use std::fs::read_to_string;\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 244 +- n: 248 l: " use super::*;\n" m: " ^^^^^^^^ "