From 05ba45c667454028c3e65769d6f63fb0f27c1ca8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jan 2019 21:02:42 +0300 Subject: remove Canceled from API impl --- crates/ra_db/src/cancellation.rs | 2 -- crates/ra_db/src/lib.rs | 2 +- crates/ra_ide_api/src/call_info.rs | 16 ++++++----- crates/ra_ide_api/src/goto_definition.rs | 33 ++++++++++------------ crates/ra_ide_api/src/hover.rs | 48 +++++++++++++------------------- crates/ra_ide_api/src/lib.rs | 16 ++++++----- crates/ra_ide_api/src/parent_module.rs | 11 +++----- crates/ra_ide_api/src/runnables.rs | 9 +++--- 8 files changed, 61 insertions(+), 76 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/cancellation.rs b/crates/ra_db/src/cancellation.rs index 32a268553..439080075 100644 --- a/crates/ra_db/src/cancellation.rs +++ b/crates/ra_db/src/cancellation.rs @@ -21,8 +21,6 @@ pub struct Canceled { _private: (), } -pub type Cancelable = Result; - impl Canceled { pub(crate) fn new() -> Canceled { Canceled { _private: () } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 8473fc050..89113e7a6 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -10,7 +10,7 @@ use std::panic; use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; pub use crate::{ - cancellation::{Canceled, Cancelable}, + cancellation::Canceled, syntax_ptr::LocalSyntaxPtr, input::{ FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index efa320d83..18b9508ef 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -1,6 +1,6 @@ use std::cmp::{max, min}; -use ra_db::{SyntaxDatabase, Cancelable}; +use ra_db::SyntaxDatabase; use ra_syntax::{ AstNode, SyntaxNode, TextUnit, TextRange, SyntaxKind::FN_DEF, @@ -11,21 +11,23 @@ use ra_syntax::{ use crate::{FilePosition, CallInfo, db::RootDatabase}; /// Computes parameter information for the given call expression. -pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable> { +pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { let file = db.source_file(position.file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef - let calling_node = ctry!(FnCallNode::with_node(syntax, position.offset)); - let name_ref = ctry!(calling_node.name_ref()); + let calling_node = FnCallNode::with_node(syntax, position.offset)?; + let name_ref = calling_node.name_ref()?; // Resolve the function's NameRef (NOTE: this isn't entirely accurate). let file_symbols = db.index_resolve(name_ref); - let symbol = ctry!(file_symbols.into_iter().find(|it| it.ptr.kind() == FN_DEF)); + let symbol = file_symbols + .into_iter() + .find(|it| it.ptr.kind() == FN_DEF)?; let fn_file = db.source_file(symbol.file_id); let fn_def = symbol.ptr.resolve(&fn_file); let fn_def = ast::FnDef::cast(&fn_def).unwrap(); - let mut call_info = ctry!(CallInfo::new(fn_def)); + let mut call_info = CallInfo::new(fn_def)?; // If we have a calling expression let's find which argument we are on let num_params = call_info.parameters.len(); let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some(); @@ -61,7 +63,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable } } - Ok(Some(call_info)) + Some(call_info) } enum FnCallNode<'a> { diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index e0f3deb0b..b1becca03 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,4 +1,4 @@ -use ra_db::{FileId, Cancelable, SyntaxDatabase}; +use ra_db::{FileId, SyntaxDatabase}; use ra_syntax::{ AstNode, ast, algo::find_node_at_offset, @@ -9,21 +9,18 @@ use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo}; pub(crate) fn goto_definition( db: &RootDatabase, position: FilePosition, -) -> Cancelable>>> { +) -> Option>> { let file = db.source_file(position.file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { - let navs = reference_definition(db, position.file_id, name_ref)?.to_vec(); - return Ok(Some(RangeInfo::new( - name_ref.syntax().range(), - navs.to_vec(), - ))); + let navs = reference_definition(db, position.file_id, name_ref).to_vec(); + return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec())); } if let Some(name) = find_node_at_offset::(syntax, position.offset) { - let navs = ctry!(name_definition(db, position.file_id, name)?); - return Ok(Some(RangeInfo::new(name.syntax().range(), navs))); + let navs = name_definition(db, position.file_id, name)?; + return Some(RangeInfo::new(name.syntax().range(), navs)); } - Ok(None) + None } pub(crate) enum ReferenceResult { @@ -45,7 +42,7 @@ pub(crate) fn reference_definition( db: &RootDatabase, file_id: FileId, name_ref: &ast::NameRef, -) -> Cancelable { +) -> ReferenceResult { use self::ReferenceResult::*; if let Some(function) = hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax()) @@ -54,7 +51,7 @@ pub(crate) fn reference_definition( // First try to resolve the symbol locally if let Some(entry) = scope.resolve_local_name(name_ref) { let nav = NavigationTarget::from_scope_entry(file_id, &entry); - return Ok(Exact(nav)); + return Exact(nav); }; // Next check if it is a method @@ -71,7 +68,7 @@ pub(crate) fn reference_definition( .and_then(|it| infer_result.method_resolution(it)) { if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { - return Ok(Exact(target)); + return Exact(target); } }; } @@ -88,7 +85,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)) { - return Ok(Exact(target)); + return Exact(target); } } } @@ -99,25 +96,25 @@ pub(crate) fn reference_definition( .into_iter() .map(NavigationTarget::from_symbol) .collect(); - Ok(Approximate(navs)) + Approximate(navs) } fn name_definition( db: &RootDatabase, file_id: FileId, name: &ast::Name, -) -> Cancelable>> { +) -> Option> { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { if let Some(child_module) = hir::source_binder::module_from_declaration(db, file_id, module) { let nav = NavigationTarget::from_module(db, child_module); - return Ok(Some(vec![nav])); + return Some(vec![nav]); } } } - Ok(None) + None } #[cfg(test)] diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 6b5887bda..d91151c15 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,4 +1,4 @@ -use ra_db::{Cancelable, SyntaxDatabase}; +use ra_db::{SyntaxDatabase}; use ra_syntax::{ AstNode, SyntaxNode, TreeArc, ast::self, @@ -7,19 +7,16 @@ use ra_syntax::{ use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget}; -pub(crate) fn hover( - db: &RootDatabase, - position: FilePosition, -) -> Cancelable>> { +pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option> { let file = db.source_file(position.file_id); let mut res = Vec::new(); let mut range = None; if let Some(name_ref) = find_node_at_offset::(file.syntax(), position.offset) { use crate::goto_definition::{ReferenceResult::*, reference_definition}; - let ref_result = reference_definition(db, position.file_id, name_ref)?; + let ref_result = reference_definition(db, position.file_id, name_ref); match ref_result { - Exact(nav) => res.extend(doc_text_for(db, nav)?), + Exact(nav) => res.extend(doc_text_for(db, nav)), Approximate(navs) => { let mut msg = String::from("Failed to exactly resolve the symbol. This is probably because rust_analyzer does not yet support glob imports or traits."); if !navs.is_empty() { @@ -27,7 +24,7 @@ pub(crate) fn hover( } res.push(msg); for nav in navs { - res.extend(doc_text_for(db, nav)?) + res.extend(doc_text_for(db, nav)) } } } @@ -39,25 +36,24 @@ pub(crate) fn hover( let node = find_leaf_at_offset(file.syntax(), position.offset).find_map(|leaf| { leaf.ancestors() .find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some()) - }); - let node = ctry!(node); + })?; let frange = FileRange { file_id: position.file_id, range: node.range(), }; - res.extend(type_of(db, frange)?.map(Into::into)); + res.extend(type_of(db, frange).map(Into::into)); range = Some(node.range()); }; - let range = ctry!(range); + let range = range?; if res.is_empty() { - return Ok(None); + return None; } let res = RangeInfo::new(range, res.join("\n\n---\n")); - Ok(Some(res)) + Some(res) } -pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable> { +pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { let file = db.source_file(frange.file_id); let syntax = file.syntax(); let leaf_node = find_covering_node(syntax, frange.range); @@ -67,34 +63,28 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable