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::{
db,
Cancelable, FilePosition,
FilePosition,
completion::{
completion_item::{Completions, CompletionKind},
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
/// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client).
pub(crate) fn completions(
db: &db::RootDatabase,
position: FilePosition,
) -> Cancelable<Option<Completions>> {
pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Option<Completions> {
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();
@ -57,11 +54,11 @@ pub(crate) fn completions(
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
complete_snippet::complete_expr_snippet(&mut acc, &ctx);
complete_snippet::complete_item_snippet(&mut acc, &ctx);
complete_path::complete_path(&mut acc, &ctx)?;
complete_scope::complete_scope(&mut acc, &ctx)?;
complete_dot::complete_dot(&mut acc, &ctx)?;
complete_path::complete_path(&mut acc, &ctx);
complete_scope::complete_scope(&mut acc, &ctx);
complete_dot::complete_dot(&mut acc, &ctx);
Ok(Some(acc))
Some(acc)
}
#[cfg(test)]
@ -72,6 +69,6 @@ fn check_completion(code: &str, expected_completions: &str, kind: CompletionKind
} else {
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);
}

View File

@ -1,26 +1,24 @@
use hir::{Ty, Def};
use crate::Cancelable;
use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind};
/// 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) {
(Some(function), Some(receiver)) => (function, receiver),
_ => return Ok(()),
_ => return,
};
let infer_result = function.infer(ctx.db);
let syntax_mapping = function.body_syntax_mapping(ctx.db);
let expr = match syntax_mapping.node_expr(receiver) {
Some(expr) => expr,
None => return Ok(()),
None => return,
};
let receiver_ty = infer_result[expr].clone();
if !ctx.is_call {
complete_fields(acc, ctx, receiver_ty.clone());
}
complete_methods(acc, ctx, receiver_ty);
Ok(())
}
fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {

View File

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

View File

@ -1,18 +1,15 @@
use rustc_hash::FxHashSet;
use ra_syntax::TextUnit;
use crate::{
Cancelable,
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
};
use crate::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 {
return Ok(());
return;
}
let module = match &ctx.module {
Some(it) => it,
None => return Ok(()),
None => return,
};
if let Some(function) = &ctx.function {
let scopes = function.scopes(ctx.db);
@ -40,7 +37,6 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
.from_resolution(ctx, res)
.add_to(acc)
});
Ok(())
}
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 crate::{db, FilePosition, Cancelable};
use crate::{db, FilePosition};
/// `CompletionContext` is created early during completion to figure out, where
/// exactly is the cursor, syntax-wise.
@ -41,10 +41,9 @@ impl<'a> CompletionContext<'a> {
db: &'a db::RootDatabase,
original_file: &'a SourceFile,
position: FilePosition,
) -> Cancelable<Option<CompletionContext<'a>>> {
) -> Option<CompletionContext<'a>> {
let module = source_binder::module_from_position(db, position);
let leaf =
ctry!(find_leaf_at_offset(original_file.syntax(), position.offset).left_biased());
let leaf = find_leaf_at_offset(original_file.syntax(), position.offset).left_biased()?;
let mut ctx = CompletionContext {
db,
leaf,
@ -63,7 +62,7 @@ impl<'a> CompletionContext<'a> {
is_call: false,
};
ctx.fill(original_file, position.offset);
Ok(Some(ctx))
Some(ctx)
}
fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {

View File

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

View File

@ -9,15 +9,6 @@
//!
//! The sibling `ra_ide_api_light` handles thouse bits of IDE functionality
//! 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 imp;
pub mod mock_analysis;
@ -400,7 +391,7 @@ impl Analysis {
/// Finds all usages of the reference at point.
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.
@ -445,7 +436,7 @@ impl Analysis {
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
let completions = self
.db
.catch_canceled(|db| completion::completions(db, position))??;
.catch_canceled(|db| completion::completions(db, position))?;
Ok(completions.map(|it| it.into()))
}
@ -472,7 +463,7 @@ impl Analysis {
position: FilePosition,
new_name: &str,
) -> 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>(