From 30948e1ecb2fb4fe35bf9c5c1e49464d4ea1d064 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 27 May 2021 03:47:20 +0200 Subject: simplify --- crates/ide_completion/src/completions/flyimport.rs | 7 +------ crates/ide_completion/src/completions/lifetime.rs | 11 ++++++++--- .../src/completions/macro_in_item_position.rs | 14 ++++++++------ crates/ide_completion/src/completions/qualified_path.rs | 10 ++++------ crates/ide_completion/src/completions/record.rs | 11 +++++------ crates/ide_completion/src/completions/unqualified_path.rs | 7 +------ crates/ide_completion/src/context.rs | 13 ++++++++++--- 7 files changed, 37 insertions(+), 36 deletions(-) (limited to 'crates/ide_completion/src') 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) if !ctx.config.enable_imports_on_the_fly { return None; } - if ctx.use_item_syntax.is_some() - || ctx.attribute_under_caret.is_some() - || ctx.mod_declaration_under_caret.is_some() - || ctx.record_lit_syntax.is_some() - || ctx.has_impl_or_trait_parent() - { + if ctx.use_item_syntax.is_some() || ctx.is_path_disallowed() { return None; } 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) if !ctx.lifetime_allowed { return; } + let lp_string; let param_lifetime = match ( &ctx.lifetime_syntax, ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()), ) { (Some(lt), Some(lp)) if lp == lt.clone() => return, - (Some(_), Some(lp)) => Some(lp.to_string()), + (Some(_), Some(lp)) => { + lp_string = lp.to_string(); + Some(&lp_string) + } _ => None, }; ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { - if param_lifetime != Some(name.to_string()) { - acc.add_resolution(ctx, name.to_string(), &res); + let name = name.to_string(); + if param_lifetime != Some(&name) { + acc.add_resolution(ctx, name, &res); } } }); 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}; pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { // Show only macros in top level. - if ctx.is_new_item { - ctx.scope.process_all_names(&mut |name, res| { - if let hir::ScopeDef::MacroDef(mac) = res { - acc.add_macro(ctx, Some(name.to_string()), mac); - } - }) + if !ctx.is_new_item { + return; } + + ctx.scope.process_all_names(&mut |name, res| { + if let hir::ScopeDef::MacroDef(mac) = res { + acc.add_macro(ctx, Some(name.to_string()), mac); + } + }) } #[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; use crate::{CompletionContext, Completions}; pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { + if ctx.is_path_disallowed() { + return; + } let path = match &ctx.path_qual { Some(path) => path.clone(), None => return, }; - if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() { - return; - } - - let context_module = ctx.scope.module(); - let resolution = match ctx.sema.resolve_path(&path) { Some(res) => res, None => return, }; + let context_module = ctx.scope.module(); // Add associated types on type parameters and `Self`. 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) -> let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone())); let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default(); let impl_default_trait = default_trait - .and_then(|default_trait| ty.map(|ty| ty.impls_trait(ctx.db, default_trait, &[]))) - .unwrap_or(false); + .zip(ty) + .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[])); let missing_fields = ctx.sema.record_literal_missing_fields(record_lit); if impl_default_trait && !missing_fields.is_empty() { let completion_text = "..Default::default()"; - let completion_text = completion_text - .strip_prefix(ctx.token.to_string().as_str()) - .unwrap_or(completion_text); let mut item = CompletionItem::new( CompletionKind::Snippet, ctx.source_range(), - "..Default::default()", + completion_text, ); + let completion_text = + completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text); item.insert_text(completion_text).kind(SymbolKind::Field); item.add_to(acc); } 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 if !ctx.is_trivial_path { return; } - if ctx.record_lit_syntax.is_some() - || ctx.record_pat_syntax.is_some() - || ctx.attribute_under_caret.is_some() - || ctx.mod_declaration_under_caret.is_some() - || ctx.has_impl_or_trait_parent() - { + if ctx.is_path_disallowed() { return; } 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> { pub(super) is_path_type: bool, pub(super) has_type_args: bool, pub(super) attribute_under_caret: Option, - pub(super) locals: Vec<(String, Local)>, - pub(super) mod_declaration_under_caret: Option, + pub(super) locals: Vec<(String, Local)>, // keyword patterns pub(super) previous_token: Option, - pub(super) in_loop_body: bool, pub(super) prev_sibling: Option, + pub(super) in_loop_body: bool, pub(super) is_match_arm: bool, pub(super) incomplete_let: bool, @@ -316,6 +315,14 @@ impl<'a> CompletionContext<'a> { self.prev_sibling.is_some() } + pub(crate) fn is_path_disallowed(&self) -> bool { + self.record_lit_syntax.is_some() + || self.record_pat_syntax.is_some() + || self.attribute_under_caret.is_some() + || self.mod_declaration_under_caret.is_some() + || self.has_impl_or_trait_parent() + } + fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); let syntax_element = NodeOrToken::Token(fake_ident_token); -- cgit v1.2.3