diff options
author | Aleksey Kladov <[email protected]> | 2018-10-31 12:13:49 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-31 12:13:49 +0000 |
commit | c02be1502c76cc504ccf7f73dce929585c94377c (patch) | |
tree | af21ab880f246e50209789614b45248d1f27ddd3 /crates/ra_editor/src/scope | |
parent | b67295134bf5c518b39bc88abbe1bc5b9d7d3baf (diff) |
move resolve local name
Diffstat (limited to 'crates/ra_editor/src/scope')
-rw-r--r-- | crates/ra_editor/src/scope/fn_scope.rs | 73 | ||||
-rw-r--r-- | crates/ra_editor/src/scope/mod.rs | 2 |
2 files changed, 1 insertions, 74 deletions
diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index f5c113fd9..4cb1f077c 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs | |||
@@ -258,22 +258,6 @@ struct ScopeData { | |||
258 | entries: Vec<ScopeEntry>, | 258 | entries: Vec<ScopeEntry>, |
259 | } | 259 | } |
260 | 260 | ||
261 | pub fn resolve_local_name<'a>( | ||
262 | name_ref: ast::NameRef, | ||
263 | scopes: &'a FnScopes, | ||
264 | ) -> Option<&'a ScopeEntry> { | ||
265 | use rustc_hash::FxHashSet; | ||
266 | |||
267 | let mut shadowed = FxHashSet::default(); | ||
268 | let ret = scopes | ||
269 | .scope_chain(name_ref.syntax()) | ||
270 | .flat_map(|scope| scopes.entries(scope).iter()) | ||
271 | .filter(|entry| shadowed.insert(entry.name())) | ||
272 | .filter(|entry| entry.name() == name_ref.text()) | ||
273 | .nth(0); | ||
274 | ret | ||
275 | } | ||
276 | |||
277 | #[cfg(test)] | 261 | #[cfg(test)] |
278 | mod tests { | 262 | mod tests { |
279 | use super::*; | 263 | use super::*; |
@@ -376,61 +360,4 @@ mod tests { | |||
376 | &["x"], | 360 | &["x"], |
377 | ); | 361 | ); |
378 | } | 362 | } |
379 | |||
380 | fn do_check_local_name(code: &str, expected_offset: u32) { | ||
381 | let (off, code) = extract_offset(code); | ||
382 | let file = File::parse(&code); | ||
383 | let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); | ||
384 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); | ||
385 | |||
386 | let scopes = FnScopes::new(fn_def); | ||
387 | |||
388 | let local_name = resolve_local_name(name_ref, &scopes) | ||
389 | .unwrap() | ||
390 | .ast() | ||
391 | .name() | ||
392 | .unwrap(); | ||
393 | let expected_name = | ||
394 | find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); | ||
395 | assert_eq!(local_name.syntax().range(), expected_name.syntax().range()); | ||
396 | } | ||
397 | |||
398 | #[test] | ||
399 | fn test_resolve_local_name() { | ||
400 | do_check_local_name( | ||
401 | r#" | ||
402 | fn foo(x: i32, y: u32) { | ||
403 | { | ||
404 | let z = x * 2; | ||
405 | } | ||
406 | { | ||
407 | let t = x<|> * 3; | ||
408 | } | ||
409 | }"#, | ||
410 | 21, | ||
411 | ); | ||
412 | } | ||
413 | |||
414 | #[test] | ||
415 | fn test_resolve_local_name_declaration() { | ||
416 | do_check_local_name( | ||
417 | r#" | ||
418 | fn foo(x: String) { | ||
419 | let x : &str = &x<|>; | ||
420 | }"#, | ||
421 | 21, | ||
422 | ); | ||
423 | } | ||
424 | |||
425 | #[test] | ||
426 | fn test_resolve_local_name_shadow() { | ||
427 | do_check_local_name( | ||
428 | r" | ||
429 | fn foo(x: String) { | ||
430 | let x : &str = &x; | ||
431 | x<|> | ||
432 | }", | ||
433 | 46, | ||
434 | ); | ||
435 | } | ||
436 | } | 363 | } |
diff --git a/crates/ra_editor/src/scope/mod.rs b/crates/ra_editor/src/scope/mod.rs index cc2d49392..483f5e63c 100644 --- a/crates/ra_editor/src/scope/mod.rs +++ b/crates/ra_editor/src/scope/mod.rs | |||
@@ -2,6 +2,6 @@ mod fn_scope; | |||
2 | mod mod_scope; | 2 | mod mod_scope; |
3 | 3 | ||
4 | pub use self::{ | 4 | pub use self::{ |
5 | fn_scope::{resolve_local_name, FnScopes}, | 5 | fn_scope::{FnScopes}, |
6 | mod_scope::ModuleScope, | 6 | mod_scope::ModuleScope, |
7 | }; | 7 | }; |