use crate::{
db,
- Cancelable, FilePosition,
+ FilePosition,
completion::{
completion_item::{Completions, CompletionKind},
completion_context::CompletionContext,
/// `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();
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)]
} 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);
}
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) {
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) => {
.add_to(acc)
});
}
- _ => return Ok(()),
+ _ => return,
};
- Ok(())
}
#[cfg(test)]
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);
.from_resolution(ctx, res)
.add_to(acc)
});
- Ok(())
}
fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) {
};
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.
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,
is_call: false,
};
ctx.fill(original_file, position.offset);
- Ok(Some(ctx))
+ Some(ctx)
}
fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {
};
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,
};
.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))
}
}
.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,
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();
//!
//! 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;
/// 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.
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()))
}
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>(