aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs30
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs10
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs12
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs9
4 files changed, 23 insertions, 38 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index cb86ba9a3..473edc50e 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -1,29 +1,27 @@
1use hir::{Ty, Def}; 1use hir::{Ty, Def};
2 2
3use crate::Cancelable;
4use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind}; 3use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind};
5 4
6/// Complete dot accesses, i.e. fields or methods (currently only fields). 5/// Complete dot accesses, i.e. fields or methods (currently only fields).
7pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 6pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
8 let (function, receiver) = match (&ctx.function, ctx.dot_receiver) { 7 let (function, receiver) = match (&ctx.function, ctx.dot_receiver) {
9 (Some(function), Some(receiver)) => (function, receiver), 8 (Some(function), Some(receiver)) => (function, receiver),
10 _ => return Ok(()), 9 _ => return,
11 }; 10 };
12 let infer_result = function.infer(ctx.db)?; 11 let infer_result = function.infer(ctx.db);
13 let syntax_mapping = function.body_syntax_mapping(ctx.db); 12 let syntax_mapping = function.body_syntax_mapping(ctx.db);
14 let expr = match syntax_mapping.node_expr(receiver) { 13 let expr = match syntax_mapping.node_expr(receiver) {
15 Some(expr) => expr, 14 Some(expr) => expr,
16 None => return Ok(()), 15 None => return,
17 }; 16 };
18 let receiver_ty = infer_result[expr].clone(); 17 let receiver_ty = infer_result[expr].clone();
19 if !ctx.is_call { 18 if !ctx.is_call {
20 complete_fields(acc, ctx, receiver_ty.clone())?; 19 complete_fields(acc, ctx, receiver_ty.clone());
21 } 20 }
22 complete_methods(acc, ctx, receiver_ty)?; 21 complete_methods(acc, ctx, receiver_ty);
23 Ok(())
24} 22}
25 23
26fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> { 24fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
27 for receiver in receiver.autoderef(ctx.db) { 25 for receiver in receiver.autoderef(ctx.db) {
28 match receiver { 26 match receiver {
29 Ty::Adt { def_id, .. } => { 27 Ty::Adt { def_id, .. } => {
@@ -35,7 +33,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
35 field.name().to_string(), 33 field.name().to_string(),
36 ) 34 )
37 .kind(CompletionItemKind::Field) 35 .kind(CompletionItemKind::Field)
38 .set_detail(field.ty(ctx.db)?.map(|ty| ty.to_string())) 36 .set_detail(field.ty(ctx.db).map(|ty| ty.to_string()))
39 .add_to(acc); 37 .add_to(acc);
40 } 38 }
41 } 39 }
@@ -53,14 +51,9 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
53 _ => {} 51 _ => {}
54 }; 52 };
55 } 53 }
56 Ok(())
57} 54}
58 55
59fn complete_methods( 56fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
60 acc: &mut Completions,
61 ctx: &CompletionContext,
62 receiver: Ty,
63) -> Cancelable<()> {
64 receiver.iterate_methods(ctx.db, |func| { 57 receiver.iterate_methods(ctx.db, |func| {
65 let sig = func.signature(ctx.db); 58 let sig = func.signature(ctx.db);
66 if sig.has_self_param() { 59 if sig.has_self_param() {
@@ -69,9 +62,8 @@ fn complete_methods(
69 .kind(CompletionItemKind::Method) 62 .kind(CompletionItemKind::Method)
70 .add_to(acc); 63 .add_to(acc);
71 } 64 }
72 Ok(None::<()>) 65 None::<()>
73 })?; 66 });
74 Ok(())
75} 67}
76 68
77#[cfg(test)] 69#[cfg(test)]
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 42468681a..1eded7658 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -1,16 +1,15 @@
1use crate::{ 1use crate::{
2 Cancelable,
3 completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, 2 completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
4}; 3};
5 4
6pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 5pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
7 let (path, module) = match (&ctx.path_prefix, &ctx.module) { 6 let (path, module) = match (&ctx.path_prefix, &ctx.module) {
8 (Some(path), Some(module)) => (path.clone(), module), 7 (Some(path), Some(module)) => (path.clone(), module),
9 _ => return Ok(()), 8 _ => return,
10 }; 9 };
11 let def_id = match module.resolve_path(ctx.db, &path).take_types() { 10 let def_id = match module.resolve_path(ctx.db, &path).take_types() {
12 Some(it) => it, 11 Some(it) => it,
13 None => return Ok(()), 12 None => return,
14 }; 13 };
15 match def_id.resolve(ctx.db) { 14 match def_id.resolve(ctx.db) {
16 hir::Def::Module(module) => { 15 hir::Def::Module(module) => {
@@ -30,9 +29,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
30 .add_to(acc) 29 .add_to(acc)
31 }); 30 });
32 } 31 }
33 _ => return Ok(()), 32 _ => return,
34 }; 33 };
35 Ok(())
36} 34}
37 35
38#[cfg(test)] 36#[cfg(test)]
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 660c7d16e..699680748 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -1,18 +1,15 @@
1use rustc_hash::FxHashSet; 1use rustc_hash::FxHashSet;
2use ra_syntax::TextUnit; 2use ra_syntax::TextUnit;
3 3
4use crate::{ 4use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
5 Cancelable,
6 completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
7};
8 5
9pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 6pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
10 if !ctx.is_trivial_path { 7 if !ctx.is_trivial_path {
11 return Ok(()); 8 return;
12 } 9 }
13 let module = match &ctx.module { 10 let module = match &ctx.module {
14 Some(it) => it, 11 Some(it) => it,
15 None => return Ok(()), 12 None => return,
16 }; 13 };
17 if let Some(function) = &ctx.function { 14 if let Some(function) = &ctx.function {
18 let scopes = function.scopes(ctx.db); 15 let scopes = function.scopes(ctx.db);
@@ -40,7 +37,6 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
40 .from_resolution(ctx, res) 37 .from_resolution(ctx, res)
41 .add_to(acc) 38 .add_to(acc)
42 }); 39 });
43 Ok(())
44} 40}
45 41
46fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) { 42fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) {
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index f5b5ed689..e537e0082 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -7,7 +7,7 @@ use ra_syntax::{
7}; 7};
8use hir::source_binder; 8use hir::source_binder;
9 9
10use crate::{db, FilePosition, Cancelable}; 10use crate::{db, FilePosition};
11 11
12/// `CompletionContext` is created early during completion to figure out, where 12/// `CompletionContext` is created early during completion to figure out, where
13/// exactly is the cursor, syntax-wise. 13/// exactly is the cursor, syntax-wise.
@@ -41,10 +41,9 @@ impl<'a> CompletionContext<'a> {
41 db: &'a db::RootDatabase, 41 db: &'a db::RootDatabase,
42 original_file: &'a SourceFile, 42 original_file: &'a SourceFile,
43 position: FilePosition, 43 position: FilePosition,
44 ) -> Cancelable<Option<CompletionContext<'a>>> { 44 ) -> Option<CompletionContext<'a>> {
45 let module = source_binder::module_from_position(db, position); 45 let module = source_binder::module_from_position(db, position);
46 let leaf = 46 let leaf = find_leaf_at_offset(original_file.syntax(), position.offset).left_biased()?;
47 ctry!(find_leaf_at_offset(original_file.syntax(), position.offset).left_biased());
48 let mut ctx = CompletionContext { 47 let mut ctx = CompletionContext {
49 db, 48 db,
50 leaf, 49 leaf,
@@ -63,7 +62,7 @@ impl<'a> CompletionContext<'a> {
63 is_call: false, 62 is_call: false,
64 }; 63 };
65 ctx.fill(original_file, position.offset); 64 ctx.fill(original_file, position.offset);
66 Ok(Some(ctx)) 65 Some(ctx)
67 } 66 }
68 67
69 fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) { 68 fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {