move find_references to references

This commit is contained in:
Aleksey Kladov 2019-02-08 14:06:26 +03:00
parent f5bb704568
commit 5173c6295b
3 changed files with 55 additions and 67 deletions

View file

@ -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::<Vec<_>>();
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::<ast::BindPat>(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::<ast::NameRef>(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::<ast::BindPat>(syntax, resolved.range().end())?;
Some((binding, descr))
}
}
pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
let syntax = self.parse(file_id);

View file

@ -322,7 +322,7 @@ impl Analysis {
/// Finds all usages of the reference at point.
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
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.

View file

@ -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::<Vec<_>>();
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::<ast::BindPat>(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::<ast::NameRef>(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::<ast::BindPat>(syntax, resolved.range().end())?;
Some((binding, descr))
}
}
pub(crate) fn rename(
db: &RootDatabase,
@ -57,7 +97,8 @@ fn rename_mod(
) -> Option<SourceChange> {
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<SourceChange> {
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,