From 5173c6295b9c48e6990d6fb6467fc35cd0dfc902 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:06:26 +0300 Subject: move find_references to references --- crates/ra_ide_api/src/imp.rs | 54 +----------------------------- crates/ra_ide_api/src/lib.rs | 2 +- crates/ra_ide_api/src/references.rs | 66 +++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 67 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index dea71740c..435cc7d4b 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -2,11 +2,7 @@ use hir::{ self, Problem, source_binder }; use ra_ide_api_light::{self, LocalEdit, Severity}; -use ra_syntax::{ - algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, - SourceFile, - TextRange, -}; +use ra_syntax::ast; use ra_db::SourceDatabase; use crate::{ @@ -16,54 +12,6 @@ use crate::{ }; impl db::RootDatabase { - pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { - let file = self.parse(position.file_id); - // Find the binding associated with the offset - let (binding, descr) = match find_binding(self, &file, position) { - None => return Vec::new(), - Some(it) => it, - }; - - let mut ret = binding - .name() - .into_iter() - .map(|name| (position.file_id, name.syntax().range())) - .collect::>(); - ret.extend( - descr - .scopes(self) - .find_all_refs(binding) - .into_iter() - .map(|ref_desc| (position.file_id, ref_desc.range)), - ); - - return ret; - - fn find_binding<'a>( - db: &db::RootDatabase, - source_file: &'a SourceFile, - position: FilePosition, - ) -> Option<(&'a ast::BindPat, hir::Function)> { - let syntax = source_file.syntax(); - if let Some(binding) = find_node_at_offset::(syntax, position.offset) { - let descr = source_binder::function_from_child_node( - db, - position.file_id, - binding.syntax(), - )?; - return Some((binding, descr)); - }; - let name_ref = find_node_at_offset::(syntax, position.offset)?; - let descr = - source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; - let scope = descr.scopes(db); - let resolved = scope.resolve_local_name(name_ref)?; - let resolved = resolved.ptr().to_node(source_file); - let binding = find_node_at_offset::(syntax, resolved.range().end())?; - Some((binding, descr)) - } - } - pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { let syntax = self.parse(file_id); diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 0bb21245d..f5c1aa036 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -322,7 +322,7 @@ impl Analysis { /// Finds all usages of the reference at point. pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { - self.with_db(|db| db.find_all_refs(position)) + self.with_db(|db| references::find_all_refs(db, position)) } /// Returns a short text descrbing element at position. diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 1c9491a0a..b129f3134 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -1,13 +1,10 @@ -use relative_path::RelativePathBuf; - -use hir::{ - self, ModuleSource, source_binder::module_from_declaration, -}; +use relative_path::{RelativePath, RelativePathBuf}; +use hir::{ModuleSource, source_binder}; +use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ + AstNode, SyntaxNode, TextRange, SourceFile, + ast::{self, NameOwner}, algo::find_node_at_offset, - ast, - AstNode, - SyntaxNode }; use crate::{ @@ -17,8 +14,51 @@ use crate::{ SourceChange, SourceFileEdit, }; -use ra_db::SourceDatabase; -use relative_path::RelativePath; + +pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> { + let file = db.parse(position.file_id); + // Find the binding associated with the offset + let (binding, descr) = match find_binding(db, &file, position) { + None => return Vec::new(), + Some(it) => it, + }; + + let mut ret = binding + .name() + .into_iter() + .map(|name| (position.file_id, name.syntax().range())) + .collect::>(); + ret.extend( + descr + .scopes(db) + .find_all_refs(binding) + .into_iter() + .map(|ref_desc| (position.file_id, ref_desc.range)), + ); + + return ret; + + fn find_binding<'a>( + db: &RootDatabase, + source_file: &'a SourceFile, + position: FilePosition, + ) -> Option<(&'a ast::BindPat, hir::Function)> { + let syntax = source_file.syntax(); + if let Some(binding) = find_node_at_offset::(syntax, position.offset) { + let descr = + source_binder::function_from_child_node(db, position.file_id, binding.syntax())?; + return Some((binding, descr)); + }; + let name_ref = find_node_at_offset::(syntax, position.offset)?; + let descr = + source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; + let scope = descr.scopes(db); + let resolved = scope.resolve_local_name(name_ref)?; + let resolved = resolved.ptr().to_node(source_file); + let binding = find_node_at_offset::(syntax, resolved.range().end())?; + Some((binding, descr)) + } +} pub(crate) fn rename( db: &RootDatabase, @@ -57,7 +97,8 @@ fn rename_mod( ) -> Option { let mut source_file_edits = Vec::new(); let mut file_system_edits = Vec::new(); - if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { + if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module) + { let (file_id, module_source) = module.definition_source(db); match module_source { ModuleSource::SourceFile(..) => { @@ -108,8 +149,7 @@ fn rename_reference( position: FilePosition, new_name: &str, ) -> Option { - let edit = db - .find_all_refs(position) + let edit = find_all_refs(db, position) .iter() .map(|(file_id, text_range)| SourceFileEdit { file_id: *file_id, -- cgit v1.2.3