aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/scope/fn_scope.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-31 12:13:49 +0000
committerAleksey Kladov <[email protected]>2018-10-31 12:13:49 +0000
commitc02be1502c76cc504ccf7f73dce929585c94377c (patch)
treeaf21ab880f246e50209789614b45248d1f27ddd3 /crates/ra_editor/src/scope/fn_scope.rs
parentb67295134bf5c518b39bc88abbe1bc5b9d7d3baf (diff)
move resolve local name
Diffstat (limited to 'crates/ra_editor/src/scope/fn_scope.rs')
-rw-r--r--crates/ra_editor/src/scope/fn_scope.rs73
1 files changed, 0 insertions, 73 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
261pub 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)]
278mod tests { 262mod 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}