aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/function/scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/function/scope.rs')
-rw-r--r--crates/ra_hir/src/function/scope.rs24
1 files changed, 14 insertions, 10 deletions
diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs
index a1a580979..3e4cfad0c 100644
--- a/crates/ra_hir/src/function/scope.rs
+++ b/crates/ra_hir/src/function/scope.rs
@@ -1,7 +1,7 @@
1use rustc_hash::{FxHashMap, FxHashSet}; 1use rustc_hash::{FxHashMap, FxHashSet};
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 AstNode, SmolStr, SyntaxNodeRef, TextUnit, TextRange, 4 AstNode, SyntaxNodeRef, TextUnit, TextRange,
5 algo::generate, 5 algo::generate,
6 ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, 6 ast::{self, ArgListOwner, LoopBodyOwner, NameOwner},
7}; 7};
@@ -9,6 +9,7 @@ use ra_db::LocalSyntaxPtr;
9 9
10use crate::{ 10use crate::{
11 arena::{Arena, Id}, 11 arena::{Arena, Id},
12 Name, AsName,
12}; 13};
13 14
14pub(crate) type ScopeId = Id<ScopeData>; 15pub(crate) type ScopeId = Id<ScopeData>;
@@ -22,7 +23,7 @@ pub struct FnScopes {
22 23
23#[derive(Debug, PartialEq, Eq)] 24#[derive(Debug, PartialEq, Eq)]
24pub struct ScopeEntry { 25pub struct ScopeEntry {
25 name: SmolStr, 26 name: Name,
26 ptr: LocalSyntaxPtr, 27 ptr: LocalSyntaxPtr,
27} 28}
28 29
@@ -101,11 +102,12 @@ impl FnScopes {
101 102
102 pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> { 103 pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> {
103 let mut shadowed = FxHashSet::default(); 104 let mut shadowed = FxHashSet::default();
105 let name = name_ref.as_name();
104 let ret = self 106 let ret = self
105 .scope_chain(name_ref.syntax()) 107 .scope_chain(name_ref.syntax())
106 .flat_map(|scope| self.entries(scope).iter()) 108 .flat_map(|scope| self.entries(scope).iter())
107 .filter(|entry| shadowed.insert(entry.name())) 109 .filter(|entry| shadowed.insert(entry.name()))
108 .filter(|entry| entry.name() == &name_ref.text()) 110 .filter(|entry| entry.name() == &name)
109 .nth(0); 111 .nth(0);
110 ret 112 ret
111 } 113 }
@@ -170,14 +172,14 @@ impl FnScopes {
170 172
171impl ScopeEntry { 173impl ScopeEntry {
172 fn new(pat: ast::BindPat) -> Option<ScopeEntry> { 174 fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
173 let name = pat.name()?; 175 let name = pat.name()?.as_name();
174 let res = ScopeEntry { 176 let res = ScopeEntry {
175 name: name.text(), 177 name,
176 ptr: LocalSyntaxPtr::new(pat.syntax()), 178 ptr: LocalSyntaxPtr::new(pat.syntax()),
177 }; 179 };
178 Some(res) 180 Some(res)
179 } 181 }
180 pub fn name(&self) -> &SmolStr { 182 pub fn name(&self) -> &Name {
181 &self.name 183 &self.name
182 } 184 }
183 pub fn ptr(&self) -> LocalSyntaxPtr { 185 pub fn ptr(&self) -> LocalSyntaxPtr {
@@ -334,7 +336,7 @@ pub struct ReferenceDescriptor {
334mod tests { 336mod tests {
335 use ra_editor::find_node_at_offset; 337 use ra_editor::find_node_at_offset;
336 use ra_syntax::SourceFileNode; 338 use ra_syntax::SourceFileNode;
337 use test_utils::extract_offset; 339 use test_utils::{extract_offset, assert_eq_text};
338 340
339 use super::*; 341 use super::*;
340 342
@@ -355,9 +357,11 @@ mod tests {
355 let actual = scopes 357 let actual = scopes
356 .scope_chain(marker.syntax()) 358 .scope_chain(marker.syntax())
357 .flat_map(|scope| scopes.entries(scope)) 359 .flat_map(|scope| scopes.entries(scope))
358 .map(|it| it.name()) 360 .map(|it| it.name().to_string())
359 .collect::<Vec<_>>(); 361 .collect::<Vec<_>>()
360 assert_eq!(actual.as_slice(), expected); 362 .join("\n");
363 let expected = expected.join("\n");
364 assert_eq_text!(&actual, &expected);
361 } 365 }
362 366
363 #[test] 367 #[test]