diff options
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 8 | ||||
-rw-r--r-- | crates/ra_editor/src/lib.rs | 10 | ||||
-rw-r--r-- | crates/ra_editor/src/scope/fn_scope.rs | 11 |
3 files changed, 14 insertions, 15 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 05c91fb83..517867e86 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -199,11 +199,11 @@ impl AnalysisImpl { | |||
199 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { | 199 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { |
200 | 200 | ||
201 | // First try to resolve the symbol locally | 201 | // First try to resolve the symbol locally |
202 | if let Some(name) = resolve_local_name(&file, offset, name_ref) { | 202 | if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) { |
203 | let vec: Vec<(FileId, FileSymbol)>::new(); | 203 | let mut vec = vec![]; |
204 | vec.push((file_id, FileSymbol { | 204 | vec.push((file_id, FileSymbol { |
205 | name: name.text(), | 205 | name, |
206 | node_range: name.syntax().range(), | 206 | node_range: range, |
207 | kind : NAME | 207 | kind : NAME |
208 | })); | 208 | })); |
209 | 209 | ||
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index fcb3e12e6..2a801f7da 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -19,7 +19,7 @@ mod scope; | |||
19 | mod test_utils; | 19 | mod test_utils; |
20 | 20 | ||
21 | use ra_syntax::{ | 21 | use ra_syntax::{ |
22 | File, TextUnit, TextRange, SyntaxNodeRef, | 22 | File, TextUnit, TextRange, SmolStr, SyntaxNodeRef, |
23 | ast::{self, AstNode, NameOwner}, | 23 | ast::{self, AstNode, NameOwner}, |
24 | algo::find_leaf_at_offset, | 24 | algo::find_leaf_at_offset, |
25 | SyntaxKind::{self, *}, | 25 | SyntaxKind::{self, *}, |
@@ -164,12 +164,12 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( | |||
164 | .next() | 164 | .next() |
165 | } | 165 | } |
166 | 166 | ||
167 | pub fn resolve_local_name<'a>(file: &'a File, offset: TextUnit, name_ref: ast::NameRef) -> Option<ast::Name<'a>> { | 167 | pub fn resolve_local_name(file: &File, offset: TextUnit, name_ref: ast::NameRef) -> Option<(SmolStr, TextRange)> { |
168 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), offset)?; | 168 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), offset)?; |
169 | let scopes = scope::FnScopes::new(fn_def); | 169 | let scopes = scope::FnScopes::new(fn_def); |
170 | 170 | let scope_entry = scope::resolve_local_name(name_ref, &scopes)?; | |
171 | // TODO: This doesn't work because of scopes lifetime | 171 | let name = scope_entry.ast().name()?; |
172 | scope::resolve_local_name(name_ref, &scopes) | 172 | Some((scope_entry.name(), name.syntax().range())) |
173 | } | 173 | } |
174 | 174 | ||
175 | #[cfg(test)] | 175 | #[cfg(test)] |
diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 03f9df094..67eb8e2ab 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs | |||
@@ -89,7 +89,7 @@ impl ScopeEntry { | |||
89 | .unwrap() | 89 | .unwrap() |
90 | .text() | 90 | .text() |
91 | } | 91 | } |
92 | fn ast(&self) -> ast::BindPat { | 92 | pub fn ast(&self) -> ast::BindPat { |
93 | ast::BindPat::cast(self.syntax.borrowed()) | 93 | ast::BindPat::cast(self.syntax.borrowed()) |
94 | .unwrap() | 94 | .unwrap() |
95 | } | 95 | } |
@@ -241,16 +241,15 @@ struct ScopeData { | |||
241 | entries: Vec<ScopeEntry> | 241 | entries: Vec<ScopeEntry> |
242 | } | 242 | } |
243 | 243 | ||
244 | pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option<ast::Name<'a>> { | 244 | pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option<&'a ScopeEntry> { |
245 | use std::collections::HashSet; | 245 | use std::collections::HashSet; |
246 | 246 | ||
247 | let mut shadowed = HashSet::new(); | 247 | let mut shadowed = HashSet::new(); |
248 | let names = scopes.scope_chain(name_ref.syntax()) | 248 | scopes.scope_chain(name_ref.syntax()) |
249 | .flat_map(|scope| scopes.entries(scope).iter()) | 249 | .flat_map(|scope| scopes.entries(scope).iter()) |
250 | .filter(|entry| shadowed.insert(entry.name())) | 250 | .filter(|entry| shadowed.insert(entry.name())) |
251 | .filter(|entry| entry.name() == name_ref.text()) | 251 | .filter(|entry| entry.name() == name_ref.text()) |
252 | .nth(0)?; | 252 | .nth(0) |
253 | names.ast().name() | ||
254 | } | 253 | } |
255 | 254 | ||
256 | #[cfg(test)] | 255 | #[cfg(test)] |
@@ -365,7 +364,7 @@ mod tests { | |||
365 | 364 | ||
366 | let scopes = FnScopes::new(fn_def); | 365 | let scopes = FnScopes::new(fn_def); |
367 | 366 | ||
368 | let local_name = resolve_local_name(name_ref, &scopes).unwrap(); | 367 | let local_name = resolve_local_name(name_ref, &scopes).unwrap().ast().name().unwrap(); |
369 | 368 | ||
370 | let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); | 369 | let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); |
371 | assert_eq!(local_name.syntax().range(), expected_name.syntax().range()); | 370 | assert_eq!(local_name.syntax().range(), expected_name.syntax().range()); |