]> git.lizzy.rs Git - rust.git/commitdiff
simplify
authorLukas Wirth <lukastw97@gmail.com>
Thu, 27 May 2021 01:47:20 +0000 (03:47 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Thu, 27 May 2021 01:47:20 +0000 (03:47 +0200)
crates/ide_completion/src/completions/flyimport.rs
crates/ide_completion/src/completions/lifetime.rs
crates/ide_completion/src/completions/macro_in_item_position.rs
crates/ide_completion/src/completions/qualified_path.rs
crates/ide_completion/src/completions/record.rs
crates/ide_completion/src/completions/unqualified_path.rs
crates/ide_completion/src/context.rs

index b7d3ee8ce7bb05a780e7967460b5eb89004dfefb..be9cfbded9bb8d406176a245234aa63e8b49a504 100644 (file)
@@ -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 = {
index 5eeddf7a401aa79585be2d0d96b57991a3130d68..5f6285b849723e7f0ef81f56eb85a44d3ec04187 100644 (file)
@@ -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);
             }
         }
     });
index 2be299ac2188dc53942dfbfb0a409c878c8e49da..c5e377500053c11b639d1f42c5f3f4ba723f7396 100644 (file)
@@ -4,13 +4,15 @@
 
 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)]
index eedb44873f6e4494356b077cecd2b58ba3e97c98..ed48f61af60d8e8198863d90df4929ea096e986a 100644 (file)
@@ -7,21 +7,19 @@
 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| {
index 40006fb74f6ef90ab12b1893c0175a51af39ccbb..e1526b70b8c89c556cf68ef39527f8bb6c0a691d 100644 (file)
@@ -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);
             }
index 7496d26c4db89b709c6e118863d39b3d9214a388..046a393aeceb325530cf3b73bb9b1a3361ef575e 100644 (file)
@@ -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;
     }
 
index 5d15fde2fdfade267081d4a52d15ab33f0e9a9ad..66577df941da0e19e48c1f98c348994b24a6ee2c 100644 (file)
@@ -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<ast::Attr>,
-    pub(super) locals: Vec<(String, Local)>,
-
     pub(super) mod_declaration_under_caret: Option<ast::Module>,
+    pub(super) locals: Vec<(String, Local)>,
 
     // keyword patterns
     pub(super) previous_token: Option<SyntaxToken>,
-    pub(super) in_loop_body: bool,
     pub(super) prev_sibling: Option<PrevSibling>,
+    pub(super) in_loop_body: bool,
     pub(super) is_match_arm: bool,
     pub(super) incomplete_let: bool,
 
@@ -316,6 +315,14 @@ pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
         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);