remove Canceled from impl of ra_ide_api

This commit is contained in:
Aleksey Kladov 2019-01-15 21:09:51 +03:00
parent 05ba45c667
commit 0bb170a277
7 changed files with 43 additions and 79 deletions

View file

@ -12,7 +12,7 @@ use ra_db::SyntaxDatabase;
use crate::{ use crate::{
db, db,
Cancelable, FilePosition, FilePosition,
completion::{ completion::{
completion_item::{Completions, CompletionKind}, completion_item::{Completions, CompletionKind},
completion_context::CompletionContext, completion_context::CompletionContext,
@ -43,12 +43,9 @@ pub use crate::completion::completion_item::{CompletionItem, InsertText, Complet
/// `foo` *should* be present among the completion variants. Filtering by /// `foo` *should* be present among the completion variants. Filtering by
/// identifier prefix/fuzzy match should be done higher in the stack, together /// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client). /// with ordering of completions (currently this is done by the client).
pub(crate) fn completions( pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Option<Completions> {
db: &db::RootDatabase,
position: FilePosition,
) -> Cancelable<Option<Completions>> {
let original_file = db.source_file(position.file_id); let original_file = db.source_file(position.file_id);
let ctx = ctry!(CompletionContext::new(db, &original_file, position)?); let ctx = CompletionContext::new(db, &original_file, position)?;
let mut acc = Completions::default(); let mut acc = Completions::default();
@ -57,11 +54,11 @@ pub(crate) fn completions(
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx); complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
complete_snippet::complete_expr_snippet(&mut acc, &ctx); complete_snippet::complete_expr_snippet(&mut acc, &ctx);
complete_snippet::complete_item_snippet(&mut acc, &ctx); complete_snippet::complete_item_snippet(&mut acc, &ctx);
complete_path::complete_path(&mut acc, &ctx)?; complete_path::complete_path(&mut acc, &ctx);
complete_scope::complete_scope(&mut acc, &ctx)?; complete_scope::complete_scope(&mut acc, &ctx);
complete_dot::complete_dot(&mut acc, &ctx)?; complete_dot::complete_dot(&mut acc, &ctx);
Ok(Some(acc)) Some(acc)
} }
#[cfg(test)] #[cfg(test)]
@ -72,6 +69,6 @@ fn check_completion(code: &str, expected_completions: &str, kind: CompletionKind
} else { } else {
single_file_with_position(code) single_file_with_position(code)
}; };
let completions = completions(&analysis.db, position).unwrap().unwrap(); let completions = completions(&analysis.db, position).unwrap();
completions.assert_match(expected_completions, kind); completions.assert_match(expected_completions, kind);
} }

View file

@ -1,26 +1,24 @@
use hir::{Ty, Def}; use hir::{Ty, Def};
use crate::Cancelable;
use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind}; use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind};
/// Complete dot accesses, i.e. fields or methods (currently only fields). /// Complete dot accesses, i.e. fields or methods (currently only fields).
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
let (function, receiver) = match (&ctx.function, ctx.dot_receiver) { let (function, receiver) = match (&ctx.function, ctx.dot_receiver) {
(Some(function), Some(receiver)) => (function, receiver), (Some(function), Some(receiver)) => (function, receiver),
_ => return Ok(()), _ => return,
}; };
let infer_result = function.infer(ctx.db); let infer_result = function.infer(ctx.db);
let syntax_mapping = function.body_syntax_mapping(ctx.db); let syntax_mapping = function.body_syntax_mapping(ctx.db);
let expr = match syntax_mapping.node_expr(receiver) { let expr = match syntax_mapping.node_expr(receiver) {
Some(expr) => expr, Some(expr) => expr,
None => return Ok(()), None => return,
}; };
let receiver_ty = infer_result[expr].clone(); let receiver_ty = infer_result[expr].clone();
if !ctx.is_call { if !ctx.is_call {
complete_fields(acc, ctx, receiver_ty.clone()); complete_fields(acc, ctx, receiver_ty.clone());
} }
complete_methods(acc, ctx, receiver_ty); complete_methods(acc, ctx, receiver_ty);
Ok(())
} }
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {

View file

@ -1,16 +1,15 @@
use crate::{ use crate::{
Cancelable,
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
}; };
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
let (path, module) = match (&ctx.path_prefix, &ctx.module) { let (path, module) = match (&ctx.path_prefix, &ctx.module) {
(Some(path), Some(module)) => (path.clone(), module), (Some(path), Some(module)) => (path.clone(), module),
_ => return Ok(()), _ => return,
}; };
let def_id = match module.resolve_path(ctx.db, &path).take_types() { let def_id = match module.resolve_path(ctx.db, &path).take_types() {
Some(it) => it, Some(it) => it,
None => return Ok(()), None => return,
}; };
match def_id.resolve(ctx.db) { match def_id.resolve(ctx.db) {
hir::Def::Module(module) => { hir::Def::Module(module) => {
@ -30,9 +29,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
.add_to(acc) .add_to(acc)
}); });
} }
_ => return Ok(()), _ => return,
}; };
Ok(())
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,18 +1,15 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use ra_syntax::TextUnit; use ra_syntax::TextUnit;
use crate::{ use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
Cancelable,
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
};
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path { if !ctx.is_trivial_path {
return Ok(()); return;
} }
let module = match &ctx.module { let module = match &ctx.module {
Some(it) => it, Some(it) => it,
None => return Ok(()), None => return,
}; };
if let Some(function) = &ctx.function { if let Some(function) = &ctx.function {
let scopes = function.scopes(ctx.db); let scopes = function.scopes(ctx.db);
@ -40,7 +37,6 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
.from_resolution(ctx, res) .from_resolution(ctx, res)
.add_to(acc) .add_to(acc)
}); });
Ok(())
} }
fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) { fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) {

View file

@ -7,7 +7,7 @@ use ra_syntax::{
}; };
use hir::source_binder; use hir::source_binder;
use crate::{db, FilePosition, Cancelable}; use crate::{db, FilePosition};
/// `CompletionContext` is created early during completion to figure out, where /// `CompletionContext` is created early during completion to figure out, where
/// exactly is the cursor, syntax-wise. /// exactly is the cursor, syntax-wise.
@ -41,10 +41,9 @@ impl<'a> CompletionContext<'a> {
db: &'a db::RootDatabase, db: &'a db::RootDatabase,
original_file: &'a SourceFile, original_file: &'a SourceFile,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<CompletionContext<'a>>> { ) -> Option<CompletionContext<'a>> {
let module = source_binder::module_from_position(db, position); let module = source_binder::module_from_position(db, position);
let leaf = let leaf = find_leaf_at_offset(original_file.syntax(), position.offset).left_biased()?;
ctry!(find_leaf_at_offset(original_file.syntax(), position.offset).left_biased());
let mut ctx = CompletionContext { let mut ctx = CompletionContext {
db, db,
leaf, leaf,
@ -63,7 +62,7 @@ impl<'a> CompletionContext<'a> {
is_call: false, is_call: false,
}; };
ctx.fill(original_file, position.offset); ctx.fill(original_file, position.offset);
Ok(Some(ctx)) Some(ctx)
} }
fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) { fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {

View file

@ -110,14 +110,11 @@ impl db::RootDatabase {
}; };
vec![krate.crate_id()] vec![krate.crate_id()]
} }
pub(crate) fn find_all_refs( pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
&self,
position: FilePosition,
) -> Cancelable<Vec<(FileId, TextRange)>> {
let file = self.source_file(position.file_id); let file = self.source_file(position.file_id);
// Find the binding associated with the offset // Find the binding associated with the offset
let (binding, descr) = match find_binding(self, &file, position)? { let (binding, descr) = match find_binding(self, &file, position) {
None => return Ok(Vec::new()), None => return Vec::new(),
Some(it) => it, Some(it) => it,
}; };
@ -134,36 +131,30 @@ impl db::RootDatabase {
.map(|ref_desc| (position.file_id, ref_desc.range)), .map(|ref_desc| (position.file_id, ref_desc.range)),
); );
return Ok(ret); return ret;
fn find_binding<'a>( fn find_binding<'a>(
db: &db::RootDatabase, db: &db::RootDatabase,
source_file: &'a SourceFile, source_file: &'a SourceFile,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<(&'a ast::BindPat, hir::Function)>> { ) -> Option<(&'a ast::BindPat, hir::Function)> {
let syntax = source_file.syntax(); let syntax = source_file.syntax();
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) { if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
let descr = ctry!(source_binder::function_from_child_node( let descr = source_binder::function_from_child_node(
db, db,
position.file_id, position.file_id,
binding.syntax(), binding.syntax(),
)); )?;
return Ok(Some((binding, descr))); return Some((binding, descr));
}; };
let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset)); let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
let descr = ctry!(source_binder::function_from_child_node( let descr =
db, source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
position.file_id,
name_ref.syntax(),
));
let scope = descr.scopes(db); let scope = descr.scopes(db);
let resolved = ctry!(scope.resolve_local_name(name_ref)); let resolved = scope.resolve_local_name(name_ref)?;
let resolved = resolved.ptr().resolve(source_file); let resolved = resolved.ptr().resolve(source_file);
let binding = ctry!(find_node_at_offset::<ast::BindPat>( let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
syntax, Some((binding, descr))
resolved.range().end()
));
Ok(Some((binding, descr)))
} }
} }
@ -239,13 +230,8 @@ impl db::RootDatabase {
.collect() .collect()
} }
pub(crate) fn rename( pub(crate) fn rename(&self, position: FilePosition, new_name: &str) -> Vec<SourceFileEdit> {
&self, self.find_all_refs(position)
position: FilePosition,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
let res = self
.find_all_refs(position)?
.iter() .iter()
.map(|(file_id, text_range)| SourceFileEdit { .map(|(file_id, text_range)| SourceFileEdit {
file_id: *file_id, file_id: *file_id,
@ -255,8 +241,7 @@ impl db::RootDatabase {
builder.finish() builder.finish()
}, },
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>()
Ok(res)
} }
pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> { pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
let name = name_ref.text(); let name = name_ref.text();

View file

@ -9,15 +9,6 @@
//! //!
//! The sibling `ra_ide_api_light` handles thouse bits of IDE functionality //! The sibling `ra_ide_api_light` handles thouse bits of IDE functionality
//! which are restricted to a single file and need only syntax. //! which are restricted to a single file and need only syntax.
macro_rules! ctry {
($expr:expr) => {
match $expr {
None => return Ok(None),
Some(it) => it,
}
};
}
mod db; mod db;
mod imp; mod imp;
pub mod mock_analysis; pub mod mock_analysis;
@ -400,7 +391,7 @@ impl Analysis {
/// Finds all usages of the reference at point. /// Finds all usages of the reference at point.
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
self.with_db(|db| db.find_all_refs(position))? self.with_db(|db| db.find_all_refs(position))
} }
/// Returns a short text descrbing element at position. /// Returns a short text descrbing element at position.
@ -445,7 +436,7 @@ impl Analysis {
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> { pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
let completions = self let completions = self
.db .db
.catch_canceled(|db| completion::completions(db, position))??; .catch_canceled(|db| completion::completions(db, position))?;
Ok(completions.map(|it| it.into())) Ok(completions.map(|it| it.into()))
} }
@ -472,7 +463,7 @@ impl Analysis {
position: FilePosition, position: FilePosition,
new_name: &str, new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> { ) -> Cancelable<Vec<SourceFileEdit>> {
self.with_db(|db| db.rename(position, new_name))? self.with_db(|db| db.rename(position, new_name))
} }
fn with_db<F: FnOnce(&db::RootDatabase) -> T + std::panic::UnwindSafe, T>( fn with_db<F: FnOnce(&db::RootDatabase) -> T + std::panic::UnwindSafe, T>(