aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-05-27 02:47:20 +0100
committerLukas Wirth <[email protected]>2021-05-27 02:47:20 +0100
commit30948e1ecb2fb4fe35bf9c5c1e49464d4ea1d064 (patch)
tree3192e07f5e0bd772e47f65be732fa8fd590b3e8b /crates/ide_completion
parent6ec4ea8d9eb9ad6ad8b91968bde09121b5b791a0 (diff)
simplify
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs7
-rw-r--r--crates/ide_completion/src/completions/lifetime.rs11
-rw-r--r--crates/ide_completion/src/completions/macro_in_item_position.rs14
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs10
-rw-r--r--crates/ide_completion/src/completions/record.rs11
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs7
-rw-r--r--crates/ide_completion/src/context.rs13
7 files changed, 37 insertions, 36 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index b7d3ee8ce..be9cfbded 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -110,12 +110,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
110 if !ctx.config.enable_imports_on_the_fly { 110 if !ctx.config.enable_imports_on_the_fly {
111 return None; 111 return None;
112 } 112 }
113 if ctx.use_item_syntax.is_some() 113 if ctx.use_item_syntax.is_some() || ctx.is_path_disallowed() {
114 || ctx.attribute_under_caret.is_some()
115 || ctx.mod_declaration_under_caret.is_some()
116 || ctx.record_lit_syntax.is_some()
117 || ctx.has_impl_or_trait_parent()
118 {
119 return None; 114 return None;
120 } 115 }
121 let potential_import_name = { 116 let potential_import_name = {
diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs
index 5eeddf7a4..5f6285b84 100644
--- a/crates/ide_completion/src/completions/lifetime.rs
+++ b/crates/ide_completion/src/completions/lifetime.rs
@@ -8,19 +8,24 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
8 if !ctx.lifetime_allowed { 8 if !ctx.lifetime_allowed {
9 return; 9 return;
10 } 10 }
11 let lp_string;
11 let param_lifetime = match ( 12 let param_lifetime = match (
12 &ctx.lifetime_syntax, 13 &ctx.lifetime_syntax,
13 ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()), 14 ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
14 ) { 15 ) {
15 (Some(lt), Some(lp)) if lp == lt.clone() => return, 16 (Some(lt), Some(lp)) if lp == lt.clone() => return,
16 (Some(_), Some(lp)) => Some(lp.to_string()), 17 (Some(_), Some(lp)) => {
18 lp_string = lp.to_string();
19 Some(&lp_string)
20 }
17 _ => None, 21 _ => None,
18 }; 22 };
19 23
20 ctx.scope.process_all_names(&mut |name, res| { 24 ctx.scope.process_all_names(&mut |name, res| {
21 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { 25 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
22 if param_lifetime != Some(name.to_string()) { 26 let name = name.to_string();
23 acc.add_resolution(ctx, name.to_string(), &res); 27 if param_lifetime != Some(&name) {
28 acc.add_resolution(ctx, name, &res);
24 } 29 }
25 } 30 }
26 }); 31 });
diff --git a/crates/ide_completion/src/completions/macro_in_item_position.rs b/crates/ide_completion/src/completions/macro_in_item_position.rs
index 2be299ac2..c5e377500 100644
--- a/crates/ide_completion/src/completions/macro_in_item_position.rs
+++ b/crates/ide_completion/src/completions/macro_in_item_position.rs
@@ -4,13 +4,15 @@ use crate::{CompletionContext, Completions};
4 4
5pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { 5pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
6 // Show only macros in top level. 6 // Show only macros in top level.
7 if ctx.is_new_item { 7 if !ctx.is_new_item {
8 ctx.scope.process_all_names(&mut |name, res| { 8 return;
9 if let hir::ScopeDef::MacroDef(mac) = res {
10 acc.add_macro(ctx, Some(name.to_string()), mac);
11 }
12 })
13 } 9 }
10
11 ctx.scope.process_all_names(&mut |name, res| {
12 if let hir::ScopeDef::MacroDef(mac) = res {
13 acc.add_macro(ctx, Some(name.to_string()), mac);
14 }
15 })
14} 16}
15 17
16#[cfg(test)] 18#[cfg(test)]
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index eedb44873..ed48f61af 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -7,21 +7,19 @@ use syntax::AstNode;
7use crate::{CompletionContext, Completions}; 7use crate::{CompletionContext, Completions};
8 8
9pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { 9pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10 if ctx.is_path_disallowed() {
11 return;
12 }
10 let path = match &ctx.path_qual { 13 let path = match &ctx.path_qual {
11 Some(path) => path.clone(), 14 Some(path) => path.clone(),
12 None => return, 15 None => return,
13 }; 16 };
14 17
15 if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() {
16 return;
17 }
18
19 let context_module = ctx.scope.module();
20
21 let resolution = match ctx.sema.resolve_path(&path) { 18 let resolution = match ctx.sema.resolve_path(&path) {
22 Some(res) => res, 19 Some(res) => res,
23 None => return, 20 None => return,
24 }; 21 };
22 let context_module = ctx.scope.module();
25 23
26 // Add associated types on type parameters and `Self`. 24 // Add associated types on type parameters and `Self`.
27 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| { 25 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs
index 40006fb74..e1526b70b 100644
--- a/crates/ide_completion/src/completions/record.rs
+++ b/crates/ide_completion/src/completions/record.rs
@@ -13,20 +13,19 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
13 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone())); 13 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone()));
14 let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default(); 14 let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
15 let impl_default_trait = default_trait 15 let impl_default_trait = default_trait
16 .and_then(|default_trait| ty.map(|ty| ty.impls_trait(ctx.db, default_trait, &[]))) 16 .zip(ty)
17 .unwrap_or(false); 17 .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[]));
18 18
19 let missing_fields = ctx.sema.record_literal_missing_fields(record_lit); 19 let missing_fields = ctx.sema.record_literal_missing_fields(record_lit);
20 if impl_default_trait && !missing_fields.is_empty() { 20 if impl_default_trait && !missing_fields.is_empty() {
21 let completion_text = "..Default::default()"; 21 let completion_text = "..Default::default()";
22 let completion_text = completion_text
23 .strip_prefix(ctx.token.to_string().as_str())
24 .unwrap_or(completion_text);
25 let mut item = CompletionItem::new( 22 let mut item = CompletionItem::new(
26 CompletionKind::Snippet, 23 CompletionKind::Snippet,
27 ctx.source_range(), 24 ctx.source_range(),
28 "..Default::default()", 25 completion_text,
29 ); 26 );
27 let completion_text =
28 completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
30 item.insert_text(completion_text).kind(SymbolKind::Field); 29 item.insert_text(completion_text).kind(SymbolKind::Field);
31 item.add_to(acc); 30 item.add_to(acc);
32 } 31 }
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 7496d26c4..046a393ae 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -9,12 +9,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
9 if !ctx.is_trivial_path { 9 if !ctx.is_trivial_path {
10 return; 10 return;
11 } 11 }
12 if ctx.record_lit_syntax.is_some() 12 if ctx.is_path_disallowed() {
13 || ctx.record_pat_syntax.is_some()
14 || ctx.attribute_under_caret.is_some()
15 || ctx.mod_declaration_under_caret.is_some()
16 || ctx.has_impl_or_trait_parent()
17 {
18 return; 13 return;
19 } 14 }
20 15
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 5d15fde2f..66577df94 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -115,14 +115,13 @@ pub(crate) struct CompletionContext<'a> {
115 pub(super) is_path_type: bool, 115 pub(super) is_path_type: bool,
116 pub(super) has_type_args: bool, 116 pub(super) has_type_args: bool,
117 pub(super) attribute_under_caret: Option<ast::Attr>, 117 pub(super) attribute_under_caret: Option<ast::Attr>,
118 pub(super) locals: Vec<(String, Local)>,
119
120 pub(super) mod_declaration_under_caret: Option<ast::Module>, 118 pub(super) mod_declaration_under_caret: Option<ast::Module>,
119 pub(super) locals: Vec<(String, Local)>,
121 120
122 // keyword patterns 121 // keyword patterns
123 pub(super) previous_token: Option<SyntaxToken>, 122 pub(super) previous_token: Option<SyntaxToken>,
124 pub(super) in_loop_body: bool,
125 pub(super) prev_sibling: Option<PrevSibling>, 123 pub(super) prev_sibling: Option<PrevSibling>,
124 pub(super) in_loop_body: bool,
126 pub(super) is_match_arm: bool, 125 pub(super) is_match_arm: bool,
127 pub(super) incomplete_let: bool, 126 pub(super) incomplete_let: bool,
128 127
@@ -316,6 +315,14 @@ impl<'a> CompletionContext<'a> {
316 self.prev_sibling.is_some() 315 self.prev_sibling.is_some()
317 } 316 }
318 317
318 pub(crate) fn is_path_disallowed(&self) -> bool {
319 self.record_lit_syntax.is_some()
320 || self.record_pat_syntax.is_some()
321 || self.attribute_under_caret.is_some()
322 || self.mod_declaration_under_caret.is_some()
323 || self.has_impl_or_trait_parent()
324 }
325
319 fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { 326 fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
320 let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); 327 let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
321 let syntax_element = NodeOrToken::Token(fake_ident_token); 328 let syntax_element = NodeOrToken::Token(fake_ident_token);