Skip to content

Commit

Permalink
fix(rust): uses scope only scopes things actually behind a use
Browse files Browse the repository at this point in the history
See the snapshot diff on what this changes
  • Loading branch information
alexpovel committed Jul 24, 2024
1 parent 5f30d5a commit ea1a734
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 155 deletions.
27 changes: 16 additions & 11 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@ impl From<PreparedRustQuery> 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 => {
Expand Down
1 change: 1 addition & 0 deletions tests/langs/rust/base.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
58 changes: 29 additions & 29 deletions tests/langs/snapshots/r#mod__langs__base.rs_comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: " ^^^^^^^^^^^^^^^^^^^ "
20 changes: 10 additions & 10 deletions tests/langs/snapshots/r#mod__langs__base.rs_doc-comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: ^^^^^^^^^
84 changes: 42 additions & 42 deletions tests/langs/snapshots/r#mod__langs__base.rs_strings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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: " ^^^^^^ "
Loading

0 comments on commit ea1a734

Please sign in to comment.