diff options
author | Jeremy A. Kolb <[email protected]> | 2018-10-06 17:02:15 +0100 |
---|---|---|
committer | Jeremy A. Kolb <[email protected]> | 2018-10-06 17:02:15 +0100 |
commit | 828bd73195a43dfba2837812c070880914001e8f (patch) | |
tree | 0de1dc7603e1a50acc71041d1092e120cb565f35 /crates/ra_editor/src | |
parent | 1d4c767879913271bf01912f12c7f8f176c7755d (diff) |
Resolve local names first
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r-- | crates/ra_editor/src/lib.rs | 10 | ||||
-rw-r--r-- | crates/ra_editor/src/scope/fn_scope.rs | 11 |
2 files changed, 10 insertions, 11 deletions
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()); |