itroduce FunctionDescriptor
This commit is contained in:
parent
f4d0cb64fc
commit
109a7f3717
3 changed files with 36 additions and 9 deletions
|
@ -1,7 +1,10 @@
|
||||||
pub(super) mod imp;
|
pub(super) mod imp;
|
||||||
mod scope;
|
mod scope;
|
||||||
|
|
||||||
use std::cmp::{max, min};
|
use std::{
|
||||||
|
cmp::{max, min},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode, DocCommentsOwner, NameOwner},
|
ast::{self, AstNode, DocCommentsOwner, NameOwner},
|
||||||
|
@ -9,6 +12,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
hir::HirDatabase,
|
||||||
syntax_ptr::SyntaxPtr, FileId,
|
syntax_ptr::SyntaxPtr, FileId,
|
||||||
loc2id::IdDatabase,
|
loc2id::IdDatabase,
|
||||||
};
|
};
|
||||||
|
@ -23,6 +27,25 @@ impl FnId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) struct FunctionDescriptor {
|
||||||
|
fn_id: FnId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FunctionDescriptor {
|
||||||
|
pub(crate) fn guess_from_source(
|
||||||
|
db: &impl HirDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
fn_def: ast::FnDef,
|
||||||
|
) -> FunctionDescriptor {
|
||||||
|
let fn_id = FnId::get(db, file_id, fn_def);
|
||||||
|
FunctionDescriptor { fn_id }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {
|
||||||
|
db.fn_scopes(self.fn_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FnDescriptor {
|
pub struct FnDescriptor {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
db::SyntaxDatabase,
|
db::SyntaxDatabase,
|
||||||
hir::function::{resolve_local_name, FnId, FnScopes},
|
hir::function::{resolve_local_name, FnId, FnScopes},
|
||||||
hir::module::{
|
hir::module::{
|
||||||
ModuleId, ModuleTree, ModuleSource, ModuleDescriptor,
|
ModuleId, ModuleTree, ModuleSource,
|
||||||
nameres::{ItemMap, InputModuleItems, FileItems}
|
nameres::{ItemMap, InputModuleItems, FileItems}
|
||||||
},
|
},
|
||||||
input::SourceRootId,
|
input::SourceRootId,
|
||||||
|
@ -30,8 +30,11 @@ use crate::{
|
||||||
Cancelable,
|
Cancelable,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) use self::path::{Path, PathKind};
|
pub(crate) use self::{
|
||||||
pub(crate) use self::module::nameres::FileItemId;
|
path::{Path, PathKind},
|
||||||
|
module::{ModuleDescriptor, nameres::FileItemId},
|
||||||
|
function::FunctionDescriptor,
|
||||||
|
};
|
||||||
|
|
||||||
salsa::query_group! {
|
salsa::query_group! {
|
||||||
pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase {
|
pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase {
|
||||||
|
|
|
@ -20,9 +20,10 @@ use crate::{
|
||||||
completion::{completions, CompletionItem},
|
completion::{completions, CompletionItem},
|
||||||
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
||||||
hir::{
|
hir::{
|
||||||
function::{FnDescriptor, FnId},
|
FunctionDescriptor, ModuleDescriptor,
|
||||||
module::{ModuleDescriptor, Problem},
|
function::FnDescriptor,
|
||||||
DeclarationDescriptor, HirDatabase,
|
module::{Problem},
|
||||||
|
DeclarationDescriptor,
|
||||||
},
|
},
|
||||||
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
|
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
|
||||||
symbol_index::SymbolIndex,
|
symbol_index::SymbolIndex,
|
||||||
|
@ -587,8 +588,8 @@ fn resolve_local_name(
|
||||||
name_ref: ast::NameRef,
|
name_ref: ast::NameRef,
|
||||||
) -> Option<(SmolStr, TextRange)> {
|
) -> Option<(SmolStr, TextRange)> {
|
||||||
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
|
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
|
||||||
let fn_id = FnId::get(db, file_id, fn_def);
|
let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def);
|
||||||
let scopes = db.fn_scopes(fn_id);
|
let scopes = function.scope(db);
|
||||||
let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?;
|
let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?;
|
||||||
let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
|
let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
|
||||||
Some((scope_entry.name().clone(), syntax.range()))
|
Some((scope_entry.name().clone(), syntax.range()))
|
||||||
|
|
Loading…
Add table
Reference in a new issue