mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
remove Cancelable from source binders
This commit is contained in:
parent
a36b2cf377
commit
11f3c8afb2
16 changed files with 68 additions and 90 deletions
|
@ -5,7 +5,7 @@
|
|||
///
|
||||
/// So, this modules should not be used during hir construction, it exists
|
||||
/// purely for "IDE needs".
|
||||
use ra_db::{FileId, FilePosition, Cancelable};
|
||||
use ra_db::{FileId, FilePosition};
|
||||
use ra_syntax::{
|
||||
SmolStr, TextRange, SyntaxNode,
|
||||
ast::{self, AstNode, NameOwner},
|
||||
|
@ -18,7 +18,7 @@ use crate::{
|
|||
};
|
||||
|
||||
/// Locates the module by `FileId`. Picks topmost module in the file.
|
||||
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> {
|
||||
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Module> {
|
||||
let module_source = SourceItemId {
|
||||
file_id: file_id.into(),
|
||||
item_id: None,
|
||||
|
@ -31,25 +31,22 @@ pub fn module_from_declaration(
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
decl: &ast::Module,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
let parent_module = module_from_file_id(db, file_id)?;
|
||||
) -> Option<Module> {
|
||||
let parent_module = module_from_file_id(db, file_id);
|
||||
let child_name = decl.name();
|
||||
match (parent_module, child_name) {
|
||||
(Some(parent_module), Some(child_name)) => {
|
||||
if let Some(child) = parent_module.child(db, &child_name.as_name())? {
|
||||
return Ok(Some(child));
|
||||
if let Some(child) = parent_module.child(db, &child_name.as_name()) {
|
||||
return Some(child);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
Ok(None)
|
||||
None
|
||||
}
|
||||
|
||||
/// Locates the module by position in the source code.
|
||||
pub fn module_from_position(
|
||||
db: &impl HirDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> {
|
||||
let file = db.source_file(position.file_id);
|
||||
match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) {
|
||||
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m),
|
||||
|
@ -61,7 +58,7 @@ fn module_from_inline(
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
module: &ast::Module,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
) -> Option<Module> {
|
||||
assert!(!module.has_semi());
|
||||
let file_id = file_id.into();
|
||||
let file_items = db.file_items(file_id);
|
||||
|
@ -78,7 +75,7 @@ pub fn module_from_child_node(
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
child: &SyntaxNode,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
) -> Option<Module> {
|
||||
if let Some(m) = child
|
||||
.ancestors()
|
||||
.filter_map(ast::Module::cast)
|
||||
|
@ -90,22 +87,16 @@ pub fn module_from_child_node(
|
|||
}
|
||||
}
|
||||
|
||||
fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Cancelable<Option<Module>> {
|
||||
fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Option<Module> {
|
||||
let source_root_id = db.file_source_root(source.file_id.as_original_file());
|
||||
let module_tree = db.module_tree(source_root_id);
|
||||
let module_id = ctry!(module_tree.find_module_by_source(source));
|
||||
Ok(Some(Module::from_module_id(db, source_root_id, module_id)?))
|
||||
let module_id = module_tree.find_module_by_source(source)?;
|
||||
Some(Module::from_module_id(db, source_root_id, module_id))
|
||||
}
|
||||
|
||||
pub fn function_from_position(
|
||||
db: &impl HirDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<Function>> {
|
||||
pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> {
|
||||
let file = db.source_file(position.file_id);
|
||||
let fn_def = ctry!(find_node_at_offset::<ast::FnDef>(
|
||||
file.syntax(),
|
||||
position.offset
|
||||
));
|
||||
let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?;
|
||||
function_from_source(db, position.file_id, fn_def)
|
||||
}
|
||||
|
||||
|
@ -113,10 +104,10 @@ pub fn function_from_source(
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
fn_def: &ast::FnDef,
|
||||
) -> Cancelable<Option<Function>> {
|
||||
let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?);
|
||||
) -> Option<Function> {
|
||||
let module = module_from_child_node(db, file_id, fn_def.syntax())?;
|
||||
let res = function_from_module(db, &module, fn_def);
|
||||
Ok(Some(res))
|
||||
Some(res)
|
||||
}
|
||||
|
||||
pub fn function_from_module(
|
||||
|
@ -145,21 +136,18 @@ pub fn function_from_child_node(
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
node: &SyntaxNode,
|
||||
) -> Cancelable<Option<Function>> {
|
||||
let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast));
|
||||
) -> Option<Function> {
|
||||
let fn_def = node.ancestors().find_map(ast::FnDef::cast)?;
|
||||
function_from_source(db, file_id, fn_def)
|
||||
}
|
||||
|
||||
pub fn macro_symbols(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
) -> Cancelable<Vec<(SmolStr, TextRange)>> {
|
||||
let module = match module_from_file_id(db, file_id)? {
|
||||
pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, TextRange)> {
|
||||
let module = match module_from_file_id(db, file_id) {
|
||||
Some(it) => it,
|
||||
None => return Ok(Vec::new()),
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let loc = module.def_id.loc(db);
|
||||
let items = db.input_module_items(loc.source_root_id, loc.module_id)?;
|
||||
let items = db.input_module_items(loc.source_root_id, loc.module_id);
|
||||
let mut res = Vec::new();
|
||||
|
||||
for macro_call_id in items
|
||||
|
@ -184,5 +172,5 @@ pub fn macro_symbols(
|
|||
}
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
res
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue