diff --git a/crates/ide-completion/src/completions.rs b/crates/ide-completion/src/completions.rs index d020b49cde5..b6358d4f40c 100644 --- a/crates/ide-completion/src/completions.rs +++ b/crates/ide-completion/src/completions.rs @@ -111,6 +111,26 @@ impl Completions { ["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw)); } + pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) { + let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw); + + match ctx.config.snippet_cap { + Some(cap) => { + if snippet.ends_with('}') && ctx.incomplete_let { + // complete block expression snippets with a trailing semicolon, if inside an incomplete let + cov_mark::hit!(let_semi); + item.insert_snippet(cap, format!("{};", snippet)); + } else { + item.insert_snippet(cap, snippet); + } + } + None => { + item.insert_text(if snippet.contains('$') { kw } else { snippet }); + } + }; + item.add_to(self); + } + pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) { ctx.process_all_names(&mut |name, res| match res { ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => { diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index 780869bb8c2..23f47523d66 100644 --- a/crates/ide-completion/src/completions/expr.rs +++ b/crates/ide-completion/src/completions/expr.rs @@ -178,8 +178,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) }); if !is_func_update { - let mut add_keyword = - |kw, snippet| super::keyword::add_keyword(acc, ctx, kw, snippet); + let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet); if ctx.expects_expression() { if !in_block_expr { diff --git a/crates/ide-completion/src/completions/field.rs b/crates/ide-completion/src/completions/field.rs index d81e48cbabd..17395279178 100644 --- a/crates/ide-completion/src/completions/field.rs +++ b/crates/ide-completion/src/completions/field.rs @@ -2,7 +2,7 @@ use crate::{ context::{IdentContext, NameContext, NameKind, NameRefContext, PathCompletionCtx, PathKind}, - CompletionContext, CompletionItem, CompletionItemKind, Completions, + CompletionContext, Completions, }; pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext) { @@ -22,7 +22,7 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext .. }) => { if ctx.qualifier_ctx.vis_node.is_none() { - let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet); + let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet); add_keyword("pub(crate)", "pub(crate)"); add_keyword("pub(super)", "pub(super)"); add_keyword("pub", "pub"); @@ -31,23 +31,3 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext _ => return, } } - -pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) { - let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw); - - match ctx.config.snippet_cap { - Some(cap) => { - if snippet.ends_with('}') && ctx.incomplete_let { - // complete block expression snippets with a trailing semicolon, if inside an incomplete let - cov_mark::hit!(let_semi); - item.insert_snippet(cap, format!("{};", snippet)); - } else { - item.insert_snippet(cap, snippet); - } - } - None => { - item.insert_text(if snippet.contains('$') { kw } else { snippet }); - } - }; - item.add_to(acc); -} diff --git a/crates/ide-completion/src/completions/item_list.rs b/crates/ide-completion/src/completions/item_list.rs index aa0d04cf6cd..287cf46f2e3 100644 --- a/crates/ide-completion/src/completions/item_list.rs +++ b/crates/ide-completion/src/completions/item_list.rs @@ -3,7 +3,7 @@ use crate::{ completions::module_or_fn_macro, context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx}, - CompletionContext, CompletionItem, CompletionItemKind, Completions, + CompletionContext, Completions, }; pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) { @@ -24,7 +24,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) }) => (is_absolute_path, qualifier, None), _ => return, }; - let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet); + let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet); let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None); let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait)); @@ -121,23 +121,3 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) None => {} } } - -pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) { - let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw); - - match ctx.config.snippet_cap { - Some(cap) => { - if snippet.ends_with('}') && ctx.incomplete_let { - // complete block expression snippets with a trailing semicolon, if inside an incomplete let - cov_mark::hit!(let_semi); - item.insert_snippet(cap, format!("{};", snippet)); - } else { - item.insert_snippet(cap, snippet); - } - } - None => { - item.insert_text(if snippet.contains('$') { kw } else { snippet }); - } - }; - item.add_to(acc); -} diff --git a/crates/ide-completion/src/completions/keyword.rs b/crates/ide-completion/src/completions/keyword.rs index d6df5002f5d..e870ecc2295 100644 --- a/crates/ide-completion/src/completions/keyword.rs +++ b/crates/ide-completion/src/completions/keyword.rs @@ -4,9 +4,7 @@ use syntax::ast::Item; -use crate::{ - context::NameRefContext, CompletionContext, CompletionItem, CompletionItemKind, Completions, -}; +use crate::{context::NameRefContext, CompletionContext, Completions}; pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { let item = match ctx.nameref_ctx() { @@ -18,7 +16,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte _ => return, }; - let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet); + let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet); match item { Item::Impl(it) => { @@ -39,26 +37,6 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte } } -pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) { - let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw); - - match ctx.config.snippet_cap { - Some(cap) => { - if snippet.ends_with('}') && ctx.incomplete_let { - // complete block expression snippets with a trailing semicolon, if inside an incomplete let - cov_mark::hit!(let_semi); - item.insert_snippet(cap, format!("{};", snippet)); - } else { - item.insert_snippet(cap, snippet); - } - } - None => { - item.insert_text(if snippet.contains('$') { kw } else { snippet }); - } - }; - item.add_to(acc); -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect};