]> git.lizzy.rs Git - rust.git/commitdiff
remove Canceled from API impl
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 15 Jan 2019 18:02:42 +0000 (21:02 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 15 Jan 2019 18:02:42 +0000 (21:02 +0300)
crates/ra_db/src/cancellation.rs
crates/ra_db/src/lib.rs
crates/ra_ide_api/src/call_info.rs
crates/ra_ide_api/src/goto_definition.rs
crates/ra_ide_api/src/hover.rs
crates/ra_ide_api/src/lib.rs
crates/ra_ide_api/src/parent_module.rs
crates/ra_ide_api/src/runnables.rs

index 32a2685535c59b97c59d8073e2550e622c5d1cdd..439080075593095204482bd106f959ba1e7fccb4 100644 (file)
@@ -21,8 +21,6 @@ pub struct Canceled {
     _private: (),
 }
 
-pub type Cancelable<T> = Result<T, Canceled>;
-
 impl Canceled {
     pub(crate) fn new() -> Canceled {
         Canceled { _private: () }
index 8473fc050370cb35f11c46b718151f383c2f19bd..89113e7a6ff641ae16bc7cad55a4502d6c2f09c9 100644 (file)
@@ -10,7 +10,7 @@
 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,
index efa320d83d0bf1ac4b0b1f155c66cef6a511600f..18b9508ef3cf22f2ee71e55c6dc7f0cd3d3d5b8e 100644 (file)
@@ -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,
 use crate::{FilePosition, CallInfo, db::RootDatabase};
 
 /// Computes parameter information for the given call expression.
-pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable<Option<CallInfo>> {
+pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
     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> {
index e0f3deb0b87af2c1f0b0d63288d26157a23bf21e..b1becca03167d60c8ab142bb7d2aa5367a9a8f1f 100644 (file)
@@ -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 @@
 pub(crate) fn goto_definition(
     db: &RootDatabase,
     position: FilePosition,
-) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
+) -> Option<RangeInfo<Vec<NavigationTarget>>> {
     let file = db.source_file(position.file_id);
     let syntax = file.syntax();
     if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(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::<ast::Name>(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> {
+) -> 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<Vec<NavigationTarget>>> {
+) -> Option<Vec<NavigationTarget>> {
     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)]
index 6b5887bda1e2a3e2613d7464c94785f5341c5c45..d91151c15baf2353af6ae59fa145aa699983f733 100644 (file)
@@ -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 crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget};
 
-pub(crate) fn hover(
-    db: &RootDatabase,
-    position: FilePosition,
-) -> Cancelable<Option<RangeInfo<String>>> {
+pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<String>> {
     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::<ast::NameRef>(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<Option<String>> {
+pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
     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<Option
         .take_while(|it| it.range() == leaf_node.range())
         .find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())
         .unwrap_or(leaf_node);
-    let parent_fn = ctry!(node.ancestors().find_map(ast::FnDef::cast));
-    let function = ctry!(hir::source_binder::function_from_source(
-        db,
-        frange.file_id,
-        parent_fn
-    ));
+    let parent_fn = node.ancestors().find_map(ast::FnDef::cast)?;
+    let function = hir::source_binder::function_from_source(db, frange.file_id, parent_fn)?;
     let infer = function.infer(db);
     let syntax_mapping = function.body_syntax_mapping(db);
     if let Some(expr) = ast::Expr::cast(node).and_then(|e| syntax_mapping.node_expr(e)) {
-        Ok(Some(infer[expr].to_string()))
+        Some(infer[expr].to_string())
     } else if let Some(pat) = ast::Pat::cast(node).and_then(|p| syntax_mapping.node_pat(p)) {
-        Ok(Some(infer[pat].to_string()))
+        Some(infer[pat].to_string())
     } else {
-        Ok(None)
+        None
     }
 }
 
 // FIXME: this should not really use navigation target. Rather, approximatelly
 // resovled symbol should return a `DefId`.
-fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable<Option<String>> {
-    let result = match (nav.description(db), nav.docs(db)) {
+fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Option<String> {
+    match (nav.description(db), nav.docs(db)) {
         (Some(desc), Some(docs)) => Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs),
         (Some(desc), None) => Some("```rust\n".to_string() + &*desc + "\n```"),
         (None, Some(docs)) => Some(docs),
         _ => None,
-    };
-
-    Ok(result)
+    }
 }
 
 impl NavigationTarget {
index 0f690fc849d2f1420196fd923c00afe3c1ff173a..f18eb09ae7d48cd625ade3216b9f5ac5d450e976 100644 (file)
@@ -58,9 +58,11 @@ macro_rules! ctry {
     LineIndex, LineCol, translate_offset_with_edit,
 };
 pub use ra_db::{
-    Cancelable, Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
+    Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
 };
 
+pub type Cancelable<T> = Result<T, Canceled>;
+
 #[derive(Default)]
 pub struct AnalysisChange {
     new_roots: Vec<(SourceRootId, bool)>,
@@ -393,7 +395,7 @@ pub fn goto_definition(
         position: FilePosition,
     ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
         self.db
-            .catch_canceled(|db| goto_definition::goto_definition(db, position))?
+            .catch_canceled(|db| goto_definition::goto_definition(db, position))
     }
 
     /// Finds all usages of the reference at point.
@@ -403,18 +405,18 @@ pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, T
 
     /// Returns a short text descrbing element at position.
     pub fn hover(&self, position: FilePosition) -> Cancelable<Option<RangeInfo<String>>> {
-        self.with_db(|db| hover::hover(db, position))?
+        self.with_db(|db| hover::hover(db, position))
     }
 
     /// Computes parameter information for the given call expression.
     pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
         self.db
-            .catch_canceled(|db| call_info::call_info(db, position))?
+            .catch_canceled(|db| call_info::call_info(db, position))
     }
 
     /// Returns a `mod name;` declaration which created the current module.
     pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
-        self.with_db(|db| parent_module::parent_module(db, position))?
+        self.with_db(|db| parent_module::parent_module(db, position))
     }
 
     /// Returns crates this file belongs too.
@@ -430,7 +432,7 @@ pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
     /// Returns the set of possible targets to run for the current file.
     pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
         self.db
-            .catch_canceled(|db| runnables::runnables(db, file_id))?
+            .catch_canceled(|db| runnables::runnables(db, file_id))
     }
 
     /// Computes syntax highlighting for the given file.
@@ -460,7 +462,7 @@ pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
 
     /// Computes the type of the expression at the given position.
     pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
-        self.with_db(|db| hover::type_of(db, frange))?
+        self.with_db(|db| hover::type_of(db, frange))
     }
 
     /// Returns the edit required to rename reference at the position to the new
index 379b3f3a4f22cf9c40a35d0b30a5b92812ed62b7..e94297fe386de9ab777f4d81d755c1f27e3346f1 100644 (file)
@@ -1,19 +1,16 @@
-use ra_db::{Cancelable, FilePosition};
+use ra_db::FilePosition;
 
 use crate::{NavigationTarget, db::RootDatabase};
 
 /// This returns `Vec` because a module may be included from several places. We
 /// don't handle this case yet though, so the Vec has length at most one.
-pub(crate) fn parent_module(
-    db: &RootDatabase,
-    position: FilePosition,
-) -> Cancelable<Vec<NavigationTarget>> {
+pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
     let module = match hir::source_binder::module_from_position(db, position) {
-        None => return Ok(Vec::new()),
+        None => return Vec::new(),
         Some(it) => it,
     };
     let nav = NavigationTarget::from_module_to_decl(db, module);
-    Ok(vec![nav])
+    vec![nav]
 }
 
 #[cfg(test)]
index a3207fdd25298a75d60f9db2b1dbcf0dc80596ea..0f9f8deb3231422459524c06bff5c2701664cba9 100644 (file)
@@ -3,7 +3,7 @@
     TextRange, SyntaxNode,
     ast::{self, AstNode, NameOwner, ModuleItemOwner},
 };
-use ra_db::{Cancelable, SyntaxDatabase};
+use ra_db::SyntaxDatabase;
 
 use crate::{db::RootDatabase, FileId};
 
@@ -21,14 +21,13 @@ pub enum RunnableKind {
     Bin,
 }
 
-pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Runnable>> {
+pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
     let source_file = db.source_file(file_id);
-    let res = source_file
+    source_file
         .syntax()
         .descendants()
         .filter_map(|i| runnable(db, file_id, i))
-        .collect();
-    Ok(res)
+        .collect()
 }
 
 fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option<Runnable> {