This commit is contained in:
Aleksey Kladov 2021-01-15 15:13:30 +03:00
parent 9b1bc19d67
commit 41ea260201

View file

@ -1,6 +1,6 @@
//! Completes keywords. //! Completes keywords.
use syntax::{ast, SyntaxKind}; use syntax::SyntaxKind;
use test_utils::mark; use test_utils::mark;
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; 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, Some(it) => it,
None => return, None => return,
}; };
acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt));
}
fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem { add_keyword(
let res = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) ctx,
.kind(CompletionItemKind::Keyword); acc,
"return",
match ctx.config.snippet_cap { match (ctx.can_be_stmt, fn_def.ret_type().is_some()) {
Some(cap) => res.insert_snippet(cap, snippet), (true, true) => "return $0;",
_ => res.insert_text(if snippet.contains('$') { kw } else { snippet }), (true, false) => "return;",
} (false, true) => "return $0",
.build() (false, false) => "return",
},
)
} }
fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
acc.add(keyword(ctx, kw, snippet)); let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
} .kind(CompletionItemKind::Keyword);
let builder = match ctx.config.snippet_cap {
fn complete_return( Some(cap) => builder.insert_snippet(cap, snippet),
ctx: &CompletionContext, None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
fn_def: &ast::Fn,
can_be_stmt: bool,
) -> Option<CompletionItem> {
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",
}; };
Some(keyword(ctx, "return", snip)) acc.add(builder.build());
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use expect_test::{expect, Expect}; use expect_test::{expect, Expect};
use test_utils::mark;
use crate::{ use crate::{
test_utils::{check_edit, completion_list}, test_utils::{check_edit, completion_list},
CompletionKind, CompletionKind,
}; };
use test_utils::mark;
fn check(ra_fixture: &str, expect: Expect) { fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture, CompletionKind::Keyword); let actual = completion_list(ra_fixture, CompletionKind::Keyword);