From ea1a734bd6b5c72115f7dd3aa618e04f8507b5cc Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Wed, 24 Jul 2024 19:20:32 +0200 Subject: [PATCH] fix(rust): `uses` scope only scopes things actually behind a `use` See the snapshot diff on what this changes --- src/scoping/langs/rust.rs | 27 +++--- tests/langs/rust/base.rs | 1 + .../r#mod__langs__base.rs_comments.snap | 58 ++++++------- .../r#mod__langs__base.rs_doc-comments.snap | 20 ++--- .../r#mod__langs__base.rs_strings.snap | 84 +++++++++---------- .../snapshots/r#mod__langs__base.rs_uses.snap | 72 ++-------------- 6 files changed, 107 insertions(+), 155 deletions(-) diff --git a/src/scoping/langs/rust.rs b/src/scoping/langs/rust.rs index b5545e10..d912016e 100644 --- a/src/scoping/langs/rust.rs +++ b/src/scoping/langs/rust.rs @@ -50,17 +50,22 @@ impl From for TSQuery { } PreparedRustQuery::Uses => { r" - (scoped_identifier - path: [ - (scoped_identifier) - (identifier) - ] @use) - (scoped_use_list - path: [ - (scoped_identifier) - (identifier) - ] @use) - (use_wildcard (scoped_identifier) @use) + (use_declaration + argument: [ + (scoped_identifier + path: [ + (scoped_identifier) + (identifier) + ] @use) + (scoped_use_list + path: [ + (scoped_identifier) + (identifier) + ] @use) + (use_wildcard (scoped_identifier) @use) + (use_as_clause (scoped_identifier) @use) + ] + ) " } PreparedRustQuery::Strings => { diff --git a/tests/langs/rust/base.rs b/tests/langs/rust/base.rs index 2cfc78be..cf52312b 100644 --- a/tests/langs/rust/base.rs +++ b/tests/langs/rust/base.rs @@ -1,6 +1,7 @@ //! Module for testing various Rust grammar elements. use a::b::{c, d, e::f, g::h::i}; +use a::item as b_item; use something::prelude::*; use std::collections::HashMap; use std::collections::HashSet; 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 4677a474..b83b5512 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_comments.snap @@ -5,90 +5,90 @@ expression: inscope_parts - n: 1 l: "//! Module for testing various Rust grammar elements.\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 26 +- n: 27 l: "// Global variable\n" m: "^^^^^^^^^^^^^^^^^^ " -- n: 33 +- n: 34 l: " // A free function for testing.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 40 +- n: 41 l: "// Decorator for functions\n" m: "^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 45 +- n: 46 l: " // Decorator for free function.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 58 +- n: 59 l: " // Function with a decorator.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 80 +- n: 81 l: " /*\n" m: " ^^^^" -- n: 81 +- n: 82 l: " More comment types\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 82 +- n: 83 l: " */\n" m: "^^^^^^ " -- n: 84 +- n: 85 l: " // Static decorator for methods\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 89 +- n: 90 l: " // Decorator for static methods.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 96 +- n: 97 l: " // Method\n" m: " ^^^^^^^^^ " -- n: 98 +- n: 99 l: " // Instance method.\n" m: " ^^^^^^^^^^^^^^^^^^^ " -- n: 104 +- n: 105 l: " // Static method.\n" m: " ^^^^^^^^^^^^^^^^^ " -- n: 109 +- n: 110 l: "// Enum definition\n" m: "^^^^^^^^^^^^^^^^^^ " -- n: 118 +- n: 119 l: " // Function demonstrating match statement.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 127 +- n: 128 l: "// Statements\n" m: "^^^^^^^^^^^^^ " -- n: 141 +- n: 142 l: " // Function demonstrating inplace operators.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 150 +- n: 151 l: "// Control flow\n" m: "^^^^^^^^^^^^^^^ " -- n: 152 +- n: 153 l: " // Function demonstrating various control flow statements.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 153 +- n: 154 l: " // if statement\n" m: " ^^^^^^^^^^^^^^^ " -- n: 160 +- n: 161 l: " // while statement\n" m: " ^^^^^^^^^^^^^^^^^^ " -- n: 167 +- n: 168 l: " // for statement\n" m: " ^^^^^^^^^^^^^^^^ " -- n: 172 +- n: 173 l: " // with statement\n" m: " ^^^^^^^^^^^^^^^^^ " -- n: 182 +- n: 183 l: " // Open a connection to the mini-redis address.\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 185 +- n: 186 l: " // Set the key \"hello\" with value \"world\"\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 188 +- n: 189 l: " // Get key \"hello\"\n" m: " ^^^^^^^^^^^^^^^^^^^^ " -- n: 196 +- n: 197 l: "// Main execution\n" m: "^^^^^^^^^^^^^^^^^ " -- n: 200 +- n: 201 l: " // Lambda expression\n" m: " ^^^^^^^^^^^^^^^^^^^^ " -- n: 203 +- n: 204 l: " // Multiline string\n" m: " ^^^^^^^^^^^^^^^^^^^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.rs_doc-comments.snap b/tests/langs/snapshots/r#mod__langs__base.rs_doc-comments.snap index 7398d52f..581b26fc 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_doc-comments.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_doc-comments.snap @@ -2,33 +2,33 @@ source: tests/langs/mod.rs expression: inscope_parts --- -- n: 29 +- n: 30 l: "/// Free function\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 30 +- n: 31 l: "///\n" m: ^^^^^ -- n: 31 +- n: 32 l: "/// With a docstring.\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 62 +- n: 63 l: "/// Struct definition\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 63 +- n: 64 l: "///\n" m: ^^^^^ -- n: 64 +- n: 65 l: "/// Also has a docstring. With code:\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 65 +- n: 66 l: "///\n" m: ^^^^^ -- n: 66 +- n: 67 l: "/// ```\n" m: ^^^^^^^^^ -- n: 67 +- n: 68 l: "/// let x = 3;\n" m: ^^^^^^^^^^^^^^^^ -- n: 68 +- n: 69 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 fe2bf21f..1f4b12ca 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_strings.snap @@ -2,129 +2,129 @@ source: tests/langs/mod.rs expression: inscope_parts --- -- n: 16 +- n: 17 l: " println!(\"Function x from parent module\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 22 +- n: 23 l: " println!(\"Function y from sibling module\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 37 +- n: 38 l: " println!(\"Global test_var is now {}\", test_var);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 50 +- n: 51 l: " println!(\"Function decorator called\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 59 +- n: 60 l: " println!(\"Inside decorated function\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 76 +- n: 77 l: " instance_var: String::from(\"hello\"),\n" m: " ^^^^^^^^^ " -- n: 91 +- n: 92 l: " println!(\"Static method decorator called\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 99 +- n: 100 l: " self.instance_var = \"Instance variable\".to_string();\n" m: " ^^^^^^^^^^^^^^^^^^^^^ " -- n: 100 +- n: 101 l: " println!(\"Instance variable is {}\", self.instance_var);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 105 +- n: 106 l: " println!(\"Inside static method\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 120 +- n: 121 l: " TestEnum::VariantOne => println!(\"One\"),\n" m: " ^^^^^^^ " -- n: 121 +- n: 122 l: " TestEnum::VariantTwo => println!(\"Two\"),\n" m: " ^^^^^^^ " -- n: 122 +- n: 123 l: " TestEnum::VariantOther => println!(\"Other\"),\n" m: " ^^^^^^^^^ " -- n: 129 +- n: 130 l: " let mut nonlocal_var = \"Initial value\".to_string();\n" m: " ^^^^^^^^^^^^^^^^^ " -- n: 133 +- n: 134 l: " nonlocal_var = \"Modified value\".to_string();\n" m: " ^^^^^^^^^^^^^^^^^^ " -- n: 137 +- n: 138 l: " println!(\"Nonlocal variable is {}\", nonlocal_var);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 147 +- n: 148 l: " println!(\"Inplace operations result: {}\", x);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 155 +- n: 156 l: " println!(\"test_var is greater than 5\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 157 +- n: 158 l: " println!(\"test_var is 5 or less\");\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 163 +- n: 164 l: " println!(\"Counter is {}\", counter);\n" m: " ^^^^^^^^^^^^^^^^^ " -- n: 169 +- n: 170 l: " println!(\"Loop iteration {}\", i);\n" m: " ^^^^^^^^^^^^^^^^^^^^^ " -- n: 173 +- n: 174 l: " let file = File::open(file!()).expect(\"Cannot open file\");\n" m: " ^^^^^^^^^^^^^^^^^^^^ " -- n: 176 +- n: 177 l: " println!(\"Read from file: {:?}\", line);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 183 +- n: 184 l: " let mut client = client::connect(\"127.0.0.1:6379\").await?;\n" m: " ^^^^^^^^^^^^^^^^^^ " -- n: 186 +- n: 187 l: " client.set(\"hello\", \"world\".into()).await?;\n" m: " ^^^^^^^^^ " -- n: 186 +- n: 187 l: " client.set(\"hello\", \"world\".into()).await?;\n" m: " ^^^^^^^^^ " -- n: 189 +- n: 190 l: " let result = client.get(\"hello\").await?;\n" m: " ^^^^^^^^^ " -- n: 191 +- n: 192 l: " println!(\"got value from the server; result={:?}\", result);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 204 +- n: 205 l: " let multi_line_str = \"\n" m: " ^^^^" -- n: 205 +- n: 206 l: "This is a\n" m: ^^^^^^^^^^^ -- n: 206 +- n: 207 l: "multi-line string\n" m: ^^^^^^^^^^^^^^^^^^^ -- n: 207 +- n: 208 l: "for testing purposes.\n" m: ^^^^^^^^^^^^^^^^^^^^^^^ -- n: 208 +- n: 209 l: "\";\n" m: "^^ " -- n: 211 +- n: 212 l: " \"This is a\\nmultiline{} string\\nspanning several lines\",\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 212 +- n: 213 l: " \"{f_string}\"\n" m: " ^^^^^^^^^^^^^^ " -- n: 215 +- n: 216 l: " let raw_string = r\"This is a raw string with no special treatment for \\n\";\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 216 +- n: 217 l: " let raw_multiline_string = r#\"\n" m: " ^^^^^^" -- n: 217 +- n: 218 l: "This is a raw string with no special treatment for \\n\n" m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- n: 218 +- n: 219 l: "\"#;\n" m: "^^^ " -- n: 219 +- n: 220 l: " let bytes_string = b\"This is a bytes string\";\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 220 +- n: 221 l: " let raw_f_string = format!(r\"This is a raw f-string with {}\", raw_string);\n" m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ " -- n: 229 +- n: 230 l: " println!(\"{}\", square(5));\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 7dd7cc8c..e7ea78eb 100644 --- a/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap +++ b/tests/langs/snapshots/r#mod__langs__base.rs_uses.snap @@ -5,81 +5,27 @@ expression: inscope_parts - n: 3 l: "use a::b::{c, d, e::f, g::h::i};\n" m: " ^^^^ " -- n: 3 - l: "use a::b::{c, d, e::f, g::h::i};\n" - m: " ^ " -- n: 3 - l: "use a::b::{c, d, e::f, g::h::i};\n" - m: " ^^^^ " - n: 4 + l: "use a::item as b_item;\n" + m: " ^^^^^^^ " +- n: 5 l: "use something::prelude::*;\n" m: " ^^^^^^^^^^^^^^^^^^ " -- n: 5 +- n: 6 l: "use std::collections::HashMap;\n" m: " ^^^^^^^^^^^^^^^^ " -- n: 6 +- n: 7 l: "use std::collections::HashSet;\n" m: " ^^^^^^^^^^^^^^^^ " -- n: 7 +- n: 8 l: "use std::fs::File;\n" m: " ^^^^^^^ " -- n: 8 +- n: 9 l: "use std::io::{self, BufRead, BufReader, Write};\n" m: " ^^^^^^^ " -- n: 9 +- n: 10 l: "use std::sync::atomic::{AtomicI32, Ordering};\n" m: " ^^^^^^^^^^^^^^^^^ " -- n: 27 - l: "static TEST_VAR: AtomicI32 = AtomicI32::new(10);\n" - m: " ^^^^^^^^^ " -- n: 34 - l: " let mut test_var = TEST_VAR.load(Ordering::SeqCst);\n" - m: " ^^^^^^^^ " -- n: 36 - l: " TEST_VAR.store(test_var, Ordering::SeqCst);\n" - m: " ^^^^^^^^ " -- n: 76 - l: " instance_var: String::from(\"hello\"),\n" - m: " ^^^^^^ " -- n: 120 - l: " TestEnum::VariantOne => println!(\"One\"),\n" - m: " ^^^^^^^^ " -- n: 121 - l: " TestEnum::VariantTwo => println!(\"Two\"),\n" - m: " ^^^^^^^^ " -- n: 122 - l: " TestEnum::VariantOther => println!(\"Other\"),\n" - m: " ^^^^^^^^ " -- n: 154 - l: " if TEST_VAR.load(Ordering::SeqCst) > 5 {\n" - m: " ^^^^^^^^ " -- n: 173 - l: " let file = File::open(file!()).expect(\"Cannot open file\");\n" - m: " ^^^^ " -- n: 174 - l: " let reader = BufReader::new(file);\n" - m: " ^^^^^^^^^ " -- n: 180 - l: "#[tokio::main]\n" - m: " ^^^^^ " -- n: 183 - l: " let mut client = client::connect(\"127.0.0.1:6379\").await?;\n" - m: " ^^^^^^ " -- n: 198 +- n: 199 l: " use std::fs::read_to_string;\n" m: " ^^^^^^^ " -- n: 225 - l: " instance_var: String::new(),\n" - m: " ^^^^^^ " -- n: 228 - l: " TestStruct::static_decorator(TestStruct::static_method)();\n" - m: " ^^^^^^^^^^ " -- n: 228 - l: " TestStruct::static_decorator(TestStruct::static_method)();\n" - m: " ^^^^^^^^^^ " -- n: 233 - l: " TestEnum::match_statement(TestEnum::VariantOne);\n" - m: " ^^^^^^^^ " -- n: 233 - l: " TestEnum::match_statement(TestEnum::VariantOne);\n" - m: " ^^^^^^^^ "