Skip to content

Commit

Permalink
feat(rust): Scope pub function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 15, 2024
1 parent c2dd0c3 commit 2ebcd15
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum PreparedRustQuery {
Attribute,
/// Function definitions.
Fn,
/// Function definitions marked `pub`.
PubFn,
/// `mod` blocks.
Mod,
/// `mod tests` blocks.
Expand Down Expand Up @@ -75,6 +77,12 @@ impl From<PreparedRustQuery> 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
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
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 @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions tests/langs/snapshots/r#mod__langs__base.rs_attribute.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: " ^^^^ "
6 changes: 3 additions & 3 deletions tests/langs/snapshots/r#mod__langs__base.rs_comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: " ^^^^^^^^^^^^^^^^^^^ "
87 changes: 48 additions & 39 deletions tests/langs/snapshots/r#mod__langs__base.rs_fn.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: " ^^^^^^^^^^^^^^^^^ "
12 changes: 6 additions & 6 deletions tests/langs/snapshots/r#mod__langs__base.rs_mod-tests.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: "^ "
12 changes: 6 additions & 6 deletions tests/langs/snapshots/r#mod__langs__base.rs_mod.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: "^ "
31 changes: 31 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.rs_pub-fn.snap
Original file line number Diff line number Diff line change
@@ -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: "^ "
Loading

0 comments on commit 2ebcd15

Please sign in to comment.