Add functions to DefId

This commit is contained in:
Aleksey Kladov 2018-12-04 23:44:00 +03:00
parent 947e3350e0
commit d8b0379e10
10 changed files with 90 additions and 83 deletions

View file

@ -210,7 +210,7 @@ impl AnalysisImpl {
let syntax = file.syntax();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
if let Some(fn_descr) =
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)?
{
let scope = fn_descr.scope(&*self.db);
// First try to resolve the symbol locally
@ -257,11 +257,11 @@ impl AnalysisImpl {
Ok(vec![])
}
pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
let file = self.db.source_file(position.file_id);
// Find the binding associated with the offset
let (binding, descr) = match find_binding(&self.db, &file, position) {
None => return Vec::new(),
let (binding, descr) = match find_binding(&self.db, &file, position)? {
None => return Ok(Vec::new()),
Some(it) => it,
};
@ -274,25 +274,36 @@ impl AnalysisImpl {
.map(|ref_desc| (position.file_id, ref_desc.range)),
);
return ret;
return Ok(ret);
fn find_binding<'a>(
db: &db::RootDatabase,
source_file: &'a SourceFileNode,
position: FilePosition,
) -> Option<(ast::BindPat<'a>, hir::Function)> {
) -> Cancelable<Option<(ast::BindPat<'a>, hir::Function)>> {
let syntax = source_file.syntax();
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
let descr = hir::Function::guess_for_bind_pat(db, position.file_id, binding)?;
return Some((binding, descr));
let descr = ctry!(hir::Function::guess_for_bind_pat(
db,
position.file_id,
binding
)?);
return Ok(Some((binding, descr)));
};
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
let descr = hir::Function::guess_for_name_ref(db, position.file_id, name_ref)?;
let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset));
let descr = ctry!(hir::Function::guess_for_name_ref(
db,
position.file_id,
name_ref
)?);
let scope = descr.scope(db);
let resolved = scope.resolve_local_name(name_ref)?;
let resolved = ctry!(scope.resolve_local_name(name_ref));
let resolved = resolved.ptr().resolve(source_file);
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
Some((binding, descr))
let binding = ctry!(find_node_at_offset::<ast::BindPat>(
syntax,
resolved.range().end()
));
Ok(Some((binding, descr)))
}
}
@ -408,7 +419,9 @@ impl AnalysisImpl {
if fs.kind == FN_DEF {
let fn_file = self.db.source_file(fn_file_id);
if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def);
let descr = ctry!(hir::Function::guess_from_source(
&*self.db, fn_file_id, fn_def
)?);
if let Some(descriptor) = descr.signature_info(&*self.db) {
// If we have a calling expression let's find which argument we are on
let mut current_parameter = None;