aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/imp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r--crates/ra_analysis/src/imp.rs37
1 files changed, 27 insertions, 10 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 69a3e8dfb..ab795a647 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
6use ra_editor::{self, find_node_at_offset, resolve_local_name, FileSymbol, LineIndex, LocalEdit, CompletionItem}; 6use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit};
7use ra_syntax::{ 7use ra_syntax::{
8 ast::{self, ArgListOwner, Expr, NameOwner}, 8 ast::{self, ArgListOwner, Expr, NameOwner},
9 AstNode, File, SmolStr, 9 AstNode, File, SmolStr,
@@ -21,9 +21,14 @@ 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::module::{ModulesDatabase, ModuleTree, Problem}, 24 descriptors::{
25 descriptors::{FnDescriptor}, 25 DescriptorDatabase,
26 module::{ModuleTree, Problem},
27 function::{FnDescriptor, FnId},
28 },
29 completion::{scope_completion, resolve_based_completion, CompletionItem},
26 symbol_index::SymbolIndex, 30 symbol_index::SymbolIndex,
31 syntax_ptr::SyntaxPtrDatabase,
27 CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position, 32 CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
28 Query, SourceChange, SourceFileEdit, Cancelable, 33 Query, SourceChange, SourceFileEdit, Cancelable,
29}; 34};
@@ -175,7 +180,7 @@ impl AnalysisHostImpl {
175 180
176#[derive(Debug)] 181#[derive(Debug)]
177pub(crate) struct AnalysisImpl { 182pub(crate) struct AnalysisImpl {
178 db: db::RootDatabase, 183 pub(crate) db: db::RootDatabase,
179} 184}
180 185
181impl AnalysisImpl { 186impl AnalysisImpl {
@@ -245,12 +250,11 @@ impl AnalysisImpl {
245 pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { 250 pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
246 let mut res = Vec::new(); 251 let mut res = Vec::new();
247 let mut has_completions = false; 252 let mut has_completions = false;
248 let file = self.file_syntax(file_id); 253 if let Some(scope_based) = scope_completion(&self.db, file_id, offset) {
249 if let Some(scope_based) = ra_editor::scope_completion(&file, offset) {
250 res.extend(scope_based); 254 res.extend(scope_based);
251 has_completions = true; 255 has_completions = true;
252 } 256 }
253 if let Some(scope_based) = crate::completion::resolve_based_completion(&self.db, file_id, offset)? { 257 if let Some(scope_based) = resolve_based_completion(&self.db, file_id, offset)? {
254 res.extend(scope_based); 258 res.extend(scope_based);
255 has_completions = true; 259 has_completions = true;
256 } 260 }
@@ -271,7 +275,7 @@ impl AnalysisImpl {
271 let syntax = file.syntax(); 275 let syntax = file.syntax();
272 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) {
273 // First try to resolve the symbol locally 277 // First try to resolve the symbol locally
274 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) {
275 let mut vec = vec![]; 279 let mut vec = vec![];
276 vec.push(( 280 vec.push((
277 file_id, 281 file_id,
@@ -325,7 +329,7 @@ impl AnalysisImpl {
325 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) {
326 330
327 // We are only handing local references for now 331 // We are only handing local references for now
328 if let Some(resolved) = resolve_local_name(name_ref) { 332 if let Some(resolved) = resolve_local_name(&self.db, file_id, name_ref) {
329 333
330 ret.push((file_id, resolved.1)); 334 ret.push((file_id, resolved.1));
331 335
@@ -333,7 +337,7 @@ impl AnalysisImpl {
333 337
334 let refs : Vec<_> = fn_def.syntax().descendants() 338 let refs : Vec<_> = fn_def.syntax().descendants()
335 .filter_map(ast::NameRef::cast) 339 .filter_map(ast::NameRef::cast)
336 .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()))
337 .collect(); 341 .collect();
338 342
339 for r in refs { 343 for r in refs {
@@ -597,3 +601,16 @@ impl<'a> FnCallNode<'a> {
597 } 601 }
598 } 602 }
599} 603}
604
605fn 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}