From 41ea2602017027e22f5a68df3d6ad98e2ae880f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Jan 2021 15:13:30 +0300 Subject: [PATCH] Simplify --- crates/completion/src/completions/keyword.rs | 46 ++++++++------------ 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/crates/completion/src/completions/keyword.rs b/crates/completion/src/completions/keyword.rs index 665214895c2..effc3e4bfc3 100644 --- a/crates/completion/src/completions/keyword.rs +++ b/crates/completion/src/completions/keyword.rs @@ -1,6 +1,6 @@ //! Completes keywords. -use syntax::{ast, SyntaxKind}; +use syntax::SyntaxKind; use test_utils::mark; use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; @@ -143,47 +143,39 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte Some(it) => it, None => return, }; - acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt)); -} -fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem { - let res = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) - .kind(CompletionItemKind::Keyword); - - match ctx.config.snippet_cap { - Some(cap) => res.insert_snippet(cap, snippet), - _ => res.insert_text(if snippet.contains('$') { kw } else { snippet }), - } - .build() + add_keyword( + ctx, + acc, + "return", + match (ctx.can_be_stmt, fn_def.ret_type().is_some()) { + (true, true) => "return $0;", + (true, false) => "return;", + (false, true) => "return $0", + (false, false) => "return", + }, + ) } fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { - acc.add(keyword(ctx, kw, snippet)); -} - -fn complete_return( - ctx: &CompletionContext, - fn_def: &ast::Fn, - can_be_stmt: bool, -) -> Option { - let snip = match (can_be_stmt, fn_def.ret_type().is_some()) { - (true, true) => "return $0;", - (true, false) => "return;", - (false, true) => "return $0", - (false, false) => "return", + let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) + .kind(CompletionItemKind::Keyword); + let builder = match ctx.config.snippet_cap { + Some(cap) => builder.insert_snippet(cap, snippet), + None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }), }; - Some(keyword(ctx, "return", snip)) + acc.add(builder.build()); } #[cfg(test)] mod tests { use expect_test::{expect, Expect}; + use test_utils::mark; use crate::{ test_utils::{check_edit, completion_list}, CompletionKind, }; - use test_utils::mark; fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(ra_fixture, CompletionKind::Keyword);