remove Cancelable from navigation target

This commit is contained in:
Aleksey Kladov 2019-01-15 18:50:16 +03:00
parent 9dd4099d93
commit f1367e0370
11 changed files with 29 additions and 42 deletions

View File

@ -297,8 +297,8 @@ impl Function {
self.def_id
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
Ok(def_id_to_ast(db, self.def_id))
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
def_id_to_ast(db, self.def_id)
}
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {

View File

@ -148,7 +148,7 @@ impl Module {
} else {
return Ok(PerNs::none());
};
let module = match curr.resolve(db)? {
let module = match curr.resolve(db) {
Def::Module(it) => it,
Def::Enum(e) => {
if segments.len() == idx + 1 {

View File

@ -832,10 +832,10 @@ pub(crate) fn body_syntax_mapping(
db: &impl HirDatabase,
def_id: DefId,
) -> Cancelable<Arc<BodySyntaxMapping>> {
let def = def_id.resolve(db)?;
let def = def_id.resolve(db);
let body_syntax_mapping = match def {
Def::Function(f) => collect_fn_body_syntax(&f.source(db)?.1),
Def::Function(f) => collect_fn_body_syntax(&f.source(db).1),
// TODO: consts, etc.
_ => panic!("Trying to get body for item type without body"),
};

View File

@ -159,9 +159,9 @@ impl DefId {
db.as_ref().id2loc(self)
}
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
pub fn resolve(self, db: &impl HirDatabase) -> Def {
let loc = self.loc(db);
let res = match loc.kind {
match loc.kind {
DefKind::Module => {
let module = Module::from_module_id(db, loc.source_root_id, loc.module_id);
Def::Module(module)
@ -195,8 +195,7 @@ impl DefId {
DefKind::StructCtor => Def::Item,
DefKind::Item => Def::Item,
};
Ok(res)
}
}
pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<SyntaxNode>) {

View File

@ -481,7 +481,7 @@ pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> C
}
pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
let def = def_id.resolve(db)?;
let def = def_id.resolve(db);
match def {
Def::Module(..) => {
log::debug!("trying to get type for module {:?}", def_id);
@ -507,7 +507,7 @@ pub(super) fn type_for_field(
def_id: DefId,
field: Name,
) -> Cancelable<Option<Ty>> {
let def = def_id.resolve(db)?;
let def = def_id.resolve(db);
let variant_data = match def {
Def::Struct(s) => s.variant_data(db)?,
Def::EnumVariant(ev) => ev.variant_data(db),
@ -877,7 +877,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
} else {
return Ok((Ty::Unknown, None));
};
Ok(match def_id.resolve(self.db)? {
Ok(match def_id.resolve(self.db) {
Def::Struct(s) => {
let ty = type_for_struct(self.db, s)?;
(ty, Some(def_id))

View File

@ -27,7 +27,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
for receiver in receiver.autoderef(ctx.db) {
match receiver {
Ty::Adt { def_id, .. } => {
match def_id.resolve(ctx.db)? {
match def_id.resolve(ctx.db) {
Def::Struct(s) => {
for field in s.fields(ctx.db) {
CompletionItem::new(

View File

@ -12,7 +12,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
Some(it) => it,
None => return Ok(()),
};
match def_id.resolve(ctx.db)? {
match def_id.resolve(ctx.db) {
hir::Def::Module(module) => {
let module_scope = module.scope(ctx.db)?;
for (name, res) in module_scope.entries() {

View File

@ -144,7 +144,7 @@ impl Builder {
ctx: &CompletionContext,
resolution: &hir::Resolution,
) -> Builder {
let resolved = resolution.def_id.and_then(|d| d.resolve(ctx.db).ok());
let resolved = resolution.def_id.map(|d| d.resolve(ctx.db));
let kind = match resolved {
PerNs {
types: Some(hir::Def::Module(..)),

View File

@ -70,7 +70,7 @@ pub(crate) fn reference_definition(
.node_expr(expr)
.and_then(|it| infer_result.method_resolution(it))
{
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? {
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
return Ok(Exact(target));
}
};
@ -87,7 +87,7 @@ pub(crate) fn reference_definition(
{
let resolved = module.resolve_path(db, &path)?;
if let Some(def_id) = resolved.take_types().or(resolved.take_values()) {
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? {
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
return Ok(Exact(target));
}
}
@ -112,7 +112,7 @@ fn name_definition(
if let Some(child_module) =
hir::source_binder::module_from_declaration(db, file_id, module)
{
let nav = NavigationTarget::from_module(db, child_module)?;
let nav = NavigationTarget::from_module(db, child_module);
return Ok(Some(vec![nav]));
}
}

View File

@ -1,4 +1,4 @@
use ra_db::{FileId, Cancelable};
use ra_db::FileId;
use ra_syntax::{
SyntaxNode, AstNode, SmolStr, TextRange, ast,
SyntaxKind::{self, NAME},
@ -69,47 +69,35 @@ impl NavigationTarget {
}
}
pub(crate) fn from_module(
db: &RootDatabase,
module: hir::Module,
) -> Cancelable<NavigationTarget> {
pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let (file_id, source) = module.definition_source(db);
let name = module
.name(db)
.map(|it| it.to_string().into())
.unwrap_or_default();
let res = match source {
match source {
ModuleSource::SourceFile(node) => {
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
}
ModuleSource::Module(node) => {
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
}
};
Ok(res)
}
}
pub(crate) fn from_module_to_decl(
db: &RootDatabase,
module: hir::Module,
) -> Cancelable<NavigationTarget> {
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let name = module
.name(db)
.map(|it| it.to_string().into())
.unwrap_or_default();
if let Some((file_id, source)) = module.declaration_source(db) {
return Ok(NavigationTarget::from_syntax(
file_id,
name,
None,
source.syntax(),
));
return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
}
NavigationTarget::from_module(db, module)
}
// TODO once Def::Item is gone, this should be able to always return a NavigationTarget
pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Option<NavigationTarget> {
let res = match def {
Def::Struct(s) => {
let (file_id, node) = s.source(db);
@ -124,7 +112,7 @@ impl NavigationTarget {
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Function(f) => {
let (file_id, node) = f.source(db)?;
let (file_id, node) = f.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Trait(f) => {
@ -143,10 +131,10 @@ impl NavigationTarget {
let (file_id, node) = f.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Module(m) => NavigationTarget::from_module(db, m)?,
Def::Item => return Ok(None),
Def::Module(m) => NavigationTarget::from_module(db, m),
Def::Item => return None,
};
Ok(Some(res))
Some(res)
}
#[cfg(test)]

View File

@ -12,7 +12,7 @@ pub(crate) fn parent_module(
None => return Ok(Vec::new()),
Some(it) => it,
};
let nav = NavigationTarget::from_module_to_decl(db, module)?;
let nav = NavigationTarget::from_module_to_decl(db, module);
Ok(vec![nav])
}