aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor
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
parentb67295134bf5c518b39bc88abbe1bc5b9d7d3baf (diff)
move resolve local name
Diffstat (limited to 'crates/ra_editor')
-rw-r--r--crates/ra_editor/src/lib.rs10
-rw-r--r--crates/ra_editor/src/scope/fn_scope.rs73
-rw-r--r--crates/ra_editor/src/scope/mod.rs2
3 files changed, 2 insertions, 83 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index b73eb4ac7..ddcb6c6a2 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -151,15 +151,7 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>(
151 leaf.ancestors().filter_map(N::cast).next() 151 leaf.ancestors().filter_map(N::cast).next()
152} 152}
153 153
154pub fn resolve_local_name( 154
155 name_ref: ast::NameRef,
156) -> Option<(SmolStr, TextRange)> {
157 let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
158 let scopes = scope::FnScopes::new(fn_def);
159 let scope_entry = scope::resolve_local_name(name_ref, &scopes)?;
160 let name = scope_entry.ast().name()?;
161 Some((scope_entry.name(), name.syntax().range()))
162}
163 155
164#[cfg(test)] 156#[cfg(test)]
165mod tests { 157mod tests {
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}
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;
2mod mod_scope; 2mod mod_scope;
3 3
4pub use self::{ 4pub use self::{
5 fn_scope::{resolve_local_name, FnScopes}, 5 fn_scope::{FnScopes},
6 mod_scope::ModuleScope, 6 mod_scope::ModuleScope,
7}; 7};