diff options
author | Aleksey Kladov <[email protected]> | 2018-10-31 12:13:49 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-31 12:13:49 +0000 |
commit | c02be1502c76cc504ccf7f73dce929585c94377c (patch) | |
tree | af21ab880f246e50209789614b45248d1f27ddd3 /crates/ra_analysis/src/imp.rs | |
parent | b67295134bf5c518b39bc88abbe1bc5b9d7d3baf (diff) |
move resolve local name
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 49b693ae8..6473a1dbc 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -3,7 +3,7 @@ use std::{ | |||
3 | sync::Arc, | 3 | sync::Arc, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use ra_editor::{self, find_node_at_offset, resolve_local_name, FileSymbol, LineIndex, LocalEdit, CompletionItem}; | 6 | use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit, CompletionItem}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | ast::{self, ArgListOwner, Expr, NameOwner}, | 8 | ast::{self, ArgListOwner, Expr, NameOwner}, |
9 | AstNode, File, SmolStr, | 9 | AstNode, File, SmolStr, |
@@ -21,10 +21,13 @@ use crate::{ | |||
21 | self, SyntaxDatabase, FileSyntaxQuery, | 21 | self, SyntaxDatabase, FileSyntaxQuery, |
22 | }, | 22 | }, |
23 | input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE}, | 23 | input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE}, |
24 | descriptors::DescriptorDatabase, | 24 | descriptors::{ |
25 | descriptors::module::{ModuleTree, Problem}, | 25 | DescriptorDatabase, |
26 | descriptors::function::{FnDescriptor}, | 26 | module::{ModuleTree, Problem}, |
27 | function::{FnDescriptor, FnId}, | ||
28 | }, | ||
27 | symbol_index::SymbolIndex, | 29 | symbol_index::SymbolIndex, |
30 | syntax_ptr::SyntaxPtrDatabase, | ||
28 | CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position, | 31 | CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position, |
29 | Query, SourceChange, SourceFileEdit, Cancelable, | 32 | Query, SourceChange, SourceFileEdit, Cancelable, |
30 | }; | 33 | }; |
@@ -272,7 +275,7 @@ impl AnalysisImpl { | |||
272 | let syntax = file.syntax(); | 275 | let syntax = file.syntax(); |
273 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { | 276 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { |
274 | // First try to resolve the symbol locally | 277 | // First try to resolve the symbol locally |
275 | return if let Some((name, range)) = resolve_local_name(name_ref) { | 278 | return if let Some((name, range)) = resolve_local_name(&self.db, file_id, name_ref) { |
276 | let mut vec = vec![]; | 279 | let mut vec = vec![]; |
277 | vec.push(( | 280 | vec.push(( |
278 | file_id, | 281 | file_id, |
@@ -326,7 +329,7 @@ impl AnalysisImpl { | |||
326 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { | 329 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { |
327 | 330 | ||
328 | // We are only handing local references for now | 331 | // We are only handing local references for now |
329 | if let Some(resolved) = resolve_local_name(name_ref) { | 332 | if let Some(resolved) = resolve_local_name(&self.db, file_id, name_ref) { |
330 | 333 | ||
331 | ret.push((file_id, resolved.1)); | 334 | ret.push((file_id, resolved.1)); |
332 | 335 | ||
@@ -334,7 +337,7 @@ impl AnalysisImpl { | |||
334 | 337 | ||
335 | let refs : Vec<_> = fn_def.syntax().descendants() | 338 | let refs : Vec<_> = fn_def.syntax().descendants() |
336 | .filter_map(ast::NameRef::cast) | 339 | .filter_map(ast::NameRef::cast) |
337 | .filter(|&n: &ast::NameRef| resolve_local_name(n) == Some(resolved.clone())) | 340 | .filter(|&n: &ast::NameRef| resolve_local_name(&self.db, file_id, n) == Some(resolved.clone())) |
338 | .collect(); | 341 | .collect(); |
339 | 342 | ||
340 | for r in refs { | 343 | for r in refs { |
@@ -598,3 +601,16 @@ impl<'a> FnCallNode<'a> { | |||
598 | } | 601 | } |
599 | } | 602 | } |
600 | } | 603 | } |
604 | |||
605 | fn resolve_local_name( | ||
606 | db: &db::RootDatabase, | ||
607 | file_id: FileId, | ||
608 | name_ref: ast::NameRef, | ||
609 | ) -> Option<(SmolStr, TextRange)> { | ||
610 | let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; | ||
611 | let fn_id = FnId::new(file_id, fn_def); | ||
612 | let scopes = db.fn_scopes(fn_id); | ||
613 | let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?; | ||
614 | let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); | ||
615 | Some((scope_entry.name().clone(), syntax.range())) | ||
616 | } | ||