2020-10-18 13:09:00 +03:00
|
|
|
//! Completes keywords.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2021-05-26 21:09:27 +02:00
|
|
|
use syntax::{SyntaxKind, T};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2021-05-30 21:23:42 +02:00
|
|
|
use crate::{
|
2021-06-07 20:45:17 +02:00
|
|
|
context::PathCompletionContext, patterns::ImmediateLocation, CompletionContext, CompletionItem,
|
|
|
|
CompletionItemKind, CompletionKind, Completions,
|
2021-05-30 21:23:42 +02:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
|
2020-06-26 20:09:11 -06:00
|
|
|
if ctx.token.kind() == SyntaxKind::COMMENT {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::hit!(no_keyword_completion_in_comments);
|
2020-06-26 20:09:11 -06:00
|
|
|
return;
|
|
|
|
}
|
2021-05-30 21:23:42 +02:00
|
|
|
if matches!(ctx.completion_location, Some(ImmediateLocation::RecordExpr(_))) {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::hit!(no_keyword_completion_in_record_lit);
|
2020-11-16 12:18:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-06-17 15:54:59 +02:00
|
|
|
if ctx.attribute_under_caret.is_some() {
|
|
|
|
cov_mark::hit!(no_keyword_completion_in_attr_of_expr);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-17 15:10:25 +02:00
|
|
|
|
|
|
|
// Suggest .await syntax for types that implement Future trait
|
|
|
|
if let Some(receiver) = ctx.dot_receiver() {
|
|
|
|
if let Some(ty) = ctx.sema.type_of_expr(receiver) {
|
|
|
|
if ty.impls_future(ctx.db) {
|
|
|
|
let mut item =
|
|
|
|
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
|
|
|
|
item.kind(CompletionItemKind::Keyword).detail("expr.await");
|
|
|
|
item.add_to(acc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:46:09 +02:00
|
|
|
let mut add_keyword = |kw, snippet| add_keyword(ctx, acc, kw, snippet);
|
2020-06-26 20:09:11 -06:00
|
|
|
|
2021-05-27 04:34:21 +02:00
|
|
|
let expects_assoc_item = ctx.expects_assoc_item();
|
2021-05-27 02:54:49 +02:00
|
|
|
let has_block_expr_parent = ctx.has_block_expr_parent();
|
2021-05-27 04:34:21 +02:00
|
|
|
let expects_item = ctx.expects_item();
|
2021-05-28 20:46:09 +02:00
|
|
|
|
2021-05-27 02:54:49 +02:00
|
|
|
if ctx.has_impl_or_trait_prev_sibling() {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("where", "where ");
|
2021-06-17 15:43:21 +02:00
|
|
|
if ctx.has_impl_prev_sibling() {
|
|
|
|
add_keyword("for", "for ");
|
|
|
|
}
|
2020-06-13 00:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-26 21:09:27 +02:00
|
|
|
if ctx.previous_token_is(T![unsafe]) {
|
2021-05-28 20:46:09 +02:00
|
|
|
if expects_item || expects_assoc_item || has_block_expr_parent {
|
|
|
|
add_keyword("fn", "fn $1($2) {\n $0\n}")
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 04:34:21 +02:00
|
|
|
if expects_item || has_block_expr_parent {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("trait", "trait $1 {\n $0\n}");
|
|
|
|
add_keyword("impl", "impl $1 {\n $0\n}");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
|
2021-06-16 17:45:58 +02:00
|
|
|
if !ctx.has_visibility_prev_sibling()
|
2021-06-21 15:14:28 +02:00
|
|
|
&& (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_field())
|
2021-06-16 17:45:58 +02:00
|
|
|
{
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("pub(crate)", "pub(crate) ");
|
|
|
|
add_keyword("pub", "pub ");
|
|
|
|
}
|
|
|
|
|
2021-05-27 04:34:21 +02:00
|
|
|
if expects_item || expects_assoc_item || has_block_expr_parent {
|
2021-05-30 21:23:42 +02:00
|
|
|
add_keyword("unsafe", "unsafe ");
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("fn", "fn $1($2) {\n $0\n}");
|
|
|
|
add_keyword("const", "const $0");
|
|
|
|
add_keyword("type", "type $0");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
|
2021-05-27 04:34:21 +02:00
|
|
|
if expects_item || has_block_expr_parent {
|
2021-06-16 17:56:04 +02:00
|
|
|
if !ctx.has_visibility_prev_sibling() {
|
|
|
|
add_keyword("impl", "impl $1 {\n $0\n}");
|
|
|
|
add_keyword("extern", "extern $0");
|
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("use", "use $0");
|
|
|
|
add_keyword("trait", "trait $1 {\n $0\n}");
|
|
|
|
add_keyword("static", "static $0");
|
|
|
|
add_keyword("mod", "mod $0");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 04:34:21 +02:00
|
|
|
if expects_item {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("enum", "enum $1 {\n $0\n}");
|
|
|
|
add_keyword("struct", "struct $0");
|
|
|
|
add_keyword("union", "union $1 {\n $0\n}");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 15:14:28 +02:00
|
|
|
if ctx.expects_type() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:46:09 +02:00
|
|
|
if ctx.expects_expression() {
|
2021-05-30 21:23:42 +02:00
|
|
|
if !has_block_expr_parent {
|
|
|
|
add_keyword("unsafe", "unsafe {\n $0\n}");
|
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("match", "match $1 {\n $0\n}");
|
|
|
|
add_keyword("while", "while $1 {\n $0\n}");
|
|
|
|
add_keyword("while let", "while let $1 = $2 {\n $0\n}");
|
|
|
|
add_keyword("loop", "loop {\n $0\n}");
|
|
|
|
add_keyword("if", "if $1 {\n $0\n}");
|
|
|
|
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
|
|
|
|
add_keyword("for", "for $1 in $2 {\n $0\n}");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
2020-07-10 17:41:43 +02:00
|
|
|
|
2021-05-27 02:54:49 +02:00
|
|
|
if ctx.previous_token_is(T![if]) || ctx.previous_token_is(T![while]) || has_block_expr_parent {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("let", "let ");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
2020-07-10 17:41:43 +02:00
|
|
|
|
2021-05-28 22:03:31 +02:00
|
|
|
if ctx.after_if() {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("else", "else {\n $0\n}");
|
|
|
|
add_keyword("else if", "else if $1 {\n $0\n}");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
|
2021-05-27 18:15:18 +02:00
|
|
|
if ctx.expects_ident_pat_or_ref_expr() {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("mut", "mut ");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
2021-05-28 20:46:09 +02:00
|
|
|
|
2021-06-07 20:45:17 +02:00
|
|
|
let (can_be_stmt, in_loop_body) = match ctx.path_context {
|
|
|
|
Some(PathCompletionContext {
|
|
|
|
is_trivial_path: true, can_be_stmt, in_loop_body, ..
|
|
|
|
}) => (can_be_stmt, in_loop_body),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if in_loop_body {
|
|
|
|
if can_be_stmt {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("continue", "continue;");
|
|
|
|
add_keyword("break", "break;");
|
2020-06-13 13:47:30 +02:00
|
|
|
} else {
|
2021-05-28 20:46:09 +02:00
|
|
|
add_keyword("continue", "continue");
|
|
|
|
add_keyword("break", "break");
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2021-05-27 02:54:49 +02:00
|
|
|
let fn_def = match &ctx.function_def {
|
2019-01-08 22:33:36 +03:00
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2020-06-13 13:47:30 +02:00
|
|
|
|
2021-01-15 15:13:30 +03:00
|
|
|
add_keyword(
|
|
|
|
"return",
|
2021-06-07 20:45:17 +02:00
|
|
|
match (can_be_stmt, fn_def.ret_type().is_some()) {
|
2021-01-15 15:13:30 +03:00
|
|
|
(true, true) => "return $0;",
|
|
|
|
(true, false) => "return;",
|
|
|
|
(false, true) => "return $0",
|
|
|
|
(false, false) => "return",
|
|
|
|
},
|
|
|
|
)
|
2020-06-13 13:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw);
|
|
|
|
item.kind(CompletionItemKind::Keyword);
|
2021-03-11 17:46:41 +02:00
|
|
|
|
|
|
|
match ctx.config.snippet_cap {
|
2021-01-15 15:49:59 +03:00
|
|
|
Some(cap) => {
|
2021-05-28 20:46:09 +02:00
|
|
|
if snippet.ends_with('}') && ctx.incomplete_let {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::hit!(let_semi);
|
2021-05-28 20:46:09 +02:00
|
|
|
item.insert_snippet(cap, format!("{};", snippet));
|
2021-01-15 15:49:59 +03:00
|
|
|
} else {
|
2021-05-28 20:46:09 +02:00
|
|
|
item.insert_snippet(cap, snippet);
|
|
|
|
}
|
2021-03-11 17:46:41 +02:00
|
|
|
}
|
|
|
|
None => {
|
2021-03-12 12:12:32 +03:00
|
|
|
item.insert_text(if snippet.contains('$') { kw } else { snippet });
|
2021-01-15 15:49:59 +03:00
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
};
|
2021-03-12 12:12:32 +03:00
|
|
|
item.add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 13:19:31 +02:00
|
|
|
use expect_test::{expect, Expect};
|
2020-07-03 12:51:18 +02:00
|
|
|
|
2020-10-18 13:09:00 +03:00
|
|
|
use crate::{
|
2021-06-16 21:45:02 +02:00
|
|
|
tests::{check_edit, filtered_completion_list},
|
2020-07-03 13:21:14 +02:00
|
|
|
CompletionKind,
|
|
|
|
};
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2020-07-03 12:51:18 +02:00
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
2021-06-16 21:45:02 +02:00
|
|
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Keyword);
|
2020-07-03 12:51:18 +02:00
|
|
|
expect.assert_eq(&actual)
|
2020-06-12 08:49:12 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
2020-06-13 00:55:21 +02:00
|
|
|
fn test_keywords_in_function() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r"fn quux() { $0 }",
|
2020-07-03 12:51:18 +02:00
|
|
|
expect![[r#"
|
2021-05-28 20:46:09 +02:00
|
|
|
kw unsafe
|
2020-07-03 12:51:18 +02:00
|
|
|
kw fn
|
2021-05-28 20:46:09 +02:00
|
|
|
kw const
|
|
|
|
kw type
|
2020-12-19 13:18:40 +02:00
|
|
|
kw impl
|
2021-06-16 17:56:04 +02:00
|
|
|
kw extern
|
|
|
|
kw use
|
2020-12-19 13:18:40 +02:00
|
|
|
kw trait
|
2021-05-28 20:46:09 +02:00
|
|
|
kw static
|
|
|
|
kw mod
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-03 12:51:18 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-07-03 12:51:18 +02:00
|
|
|
kw let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-13 00:55:21 +02:00
|
|
|
fn test_keywords_inside_block() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r"fn quux() { if true { $0 } }",
|
2020-07-03 12:51:18 +02:00
|
|
|
expect![[r#"
|
2021-05-28 20:46:09 +02:00
|
|
|
kw unsafe
|
2020-07-03 12:51:18 +02:00
|
|
|
kw fn
|
2021-05-28 20:46:09 +02:00
|
|
|
kw const
|
|
|
|
kw type
|
2020-12-19 13:18:40 +02:00
|
|
|
kw impl
|
2021-06-16 17:56:04 +02:00
|
|
|
kw extern
|
|
|
|
kw use
|
2020-12-19 13:18:40 +02:00
|
|
|
kw trait
|
2021-05-28 20:46:09 +02:00
|
|
|
kw static
|
|
|
|
kw mod
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-03 12:51:18 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-07-03 12:51:18 +02:00
|
|
|
kw let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2020-06-13 00:55:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_keywords_after_if() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn quux() { if true { () } $0 }"#,
|
2020-07-03 12:51:18 +02:00
|
|
|
expect![[r#"
|
2021-05-28 20:46:09 +02:00
|
|
|
kw unsafe
|
2020-07-03 12:51:18 +02:00
|
|
|
kw fn
|
2021-05-28 20:46:09 +02:00
|
|
|
kw const
|
|
|
|
kw type
|
2020-12-19 13:18:40 +02:00
|
|
|
kw impl
|
2021-06-16 17:56:04 +02:00
|
|
|
kw extern
|
|
|
|
kw use
|
2020-12-19 13:18:40 +02:00
|
|
|
kw trait
|
2021-05-28 20:46:09 +02:00
|
|
|
kw static
|
|
|
|
kw mod
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-03 12:51:18 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-07-03 12:51:18 +02:00
|
|
|
kw let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw else
|
|
|
|
kw else if
|
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2020-07-03 13:21:14 +02:00
|
|
|
check_edit(
|
|
|
|
"else",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn quux() { if true { () } $0 }"#,
|
2021-05-26 14:24:54 -03:00
|
|
|
r#"fn quux() { if true { () } else {
|
|
|
|
$0
|
|
|
|
} }"#,
|
2020-07-03 13:21:14 +02:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-13 00:55:21 +02:00
|
|
|
fn test_keywords_in_match_arm() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn quux() -> i32 {
|
2021-01-06 20:15:48 +00:00
|
|
|
match () { () => $0 }
|
2020-07-03 12:51:18 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-05-28 20:46:09 +02:00
|
|
|
kw unsafe
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-03 12:51:18 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-12-19 13:18:40 +02:00
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2020-06-13 00:55:21 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
2020-06-13 00:55:21 +02:00
|
|
|
fn test_keywords_in_loop() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r"fn my() { loop { $0 } }",
|
2020-07-03 12:51:18 +02:00
|
|
|
expect![[r#"
|
2021-05-28 20:46:09 +02:00
|
|
|
kw unsafe
|
2020-07-03 12:51:18 +02:00
|
|
|
kw fn
|
2021-05-28 20:46:09 +02:00
|
|
|
kw const
|
|
|
|
kw type
|
2020-12-19 13:18:40 +02:00
|
|
|
kw impl
|
2021-06-16 17:56:04 +02:00
|
|
|
kw extern
|
|
|
|
kw use
|
2020-12-19 13:18:40 +02:00
|
|
|
kw trait
|
2021-05-28 20:46:09 +02:00
|
|
|
kw static
|
|
|
|
kw mod
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-03 12:51:18 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-07-03 12:51:18 +02:00
|
|
|
kw let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw continue
|
|
|
|
kw break
|
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2020-06-13 00:55:21 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
2020-06-13 00:55:21 +02:00
|
|
|
fn test_keywords_after_unsafe_in_block_expr() {
|
2020-07-03 12:51:18 +02:00
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r"fn my_fn() { unsafe $0 }",
|
2020-07-03 12:51:18 +02:00
|
|
|
expect![[r#"
|
|
|
|
kw fn
|
|
|
|
kw trait
|
2020-12-19 13:18:40 +02:00
|
|
|
kw impl
|
2020-07-03 12:51:18 +02:00
|
|
|
"#]],
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2020-06-13 00:55:21 +02:00
|
|
|
}
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2020-07-03 23:46:36 +02:00
|
|
|
#[test]
|
|
|
|
fn no_keyword_completion_in_comments() {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::check!(no_keyword_completion_in_comments);
|
2020-07-03 23:46:36 +02:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test() {
|
2021-01-06 20:15:48 +00:00
|
|
|
let x = 2; // A comment$0
|
2020-07-03 23:46:36 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
/*
|
2021-01-06 20:15:48 +00:00
|
|
|
Some multi-line comment$0
|
2020-07-03 23:46:36 +02:00
|
|
|
*/
|
|
|
|
"#,
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
/// Some doc comment
|
2021-01-06 20:15:48 +00:00
|
|
|
/// let test$0 = 1
|
2020-07-03 23:46:36 +02:00
|
|
|
"#,
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
}
|
2020-07-04 10:36:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_await_impls_future() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-06-15 23:02:38 +03:00
|
|
|
//- minicore: future
|
|
|
|
use core::future::*;
|
2020-07-04 10:36:12 +02:00
|
|
|
struct A {}
|
|
|
|
impl Future for A {}
|
2021-01-06 20:15:48 +00:00
|
|
|
fn foo(a: A) { a.$0 }
|
2020-09-10 20:01:23 +08:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
kw await expr.await
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
2021-06-15 23:02:38 +03:00
|
|
|
//- minicore: future
|
2020-09-10 20:01:23 +08:00
|
|
|
use std::future::*;
|
|
|
|
fn foo() {
|
|
|
|
let a = async {};
|
2021-01-06 20:15:48 +00:00
|
|
|
a.$0
|
2020-09-10 20:01:23 +08:00
|
|
|
}
|
2020-07-04 10:36:12 +02:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
kw await expr.await
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2020-07-10 17:41:43 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn after_let() {
|
|
|
|
check(
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let _ = $0 }"#,
|
2020-07-10 17:41:43 +02:00
|
|
|
expect![[r#"
|
2021-05-30 21:23:42 +02:00
|
|
|
kw unsafe
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-07-10 17:41:43 +02:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-07-10 17:41:43 +02:00
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-07-10 17:41:43 +02:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2020-08-25 17:20:29 +02:00
|
|
|
|
2020-11-16 12:18:01 +01:00
|
|
|
#[test]
|
|
|
|
fn skip_struct_initializer() {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::check!(no_keyword_completion_in_record_lit);
|
2020-11-16 12:18:01 +01:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
pub f: i32,
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
$0
|
2020-11-16 12:18:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#""#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_initializer_field_expr() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
pub f: i32,
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
f: $0
|
2020-11-16 12:18:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-05-30 21:23:42 +02:00
|
|
|
kw unsafe
|
2020-12-19 13:18:40 +02:00
|
|
|
kw match
|
|
|
|
kw while
|
2021-03-03 21:59:41 +01:00
|
|
|
kw while let
|
2020-12-19 13:18:40 +02:00
|
|
|
kw loop
|
2020-11-16 12:18:01 +01:00
|
|
|
kw if
|
|
|
|
kw if let
|
2021-02-08 10:16:20 +10:30
|
|
|
kw for
|
2020-11-16 12:18:01 +01:00
|
|
|
kw return
|
2021-07-07 19:08:37 +02:00
|
|
|
kw self
|
|
|
|
kw super
|
|
|
|
kw crate
|
2020-11-16 12:18:01 +01:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2021-01-15 15:49:59 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_semi() {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::check!(let_semi);
|
2021-01-15 15:49:59 +03:00
|
|
|
check_edit(
|
|
|
|
"match",
|
|
|
|
r#"
|
|
|
|
fn main() { let x = $0 }
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-05-26 14:24:54 -03:00
|
|
|
fn main() { let x = match $1 {
|
|
|
|
$0
|
|
|
|
}; }
|
2021-01-15 15:49:59 +03:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"if",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let x = $0
|
|
|
|
let y = 92;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2021-05-26 14:24:54 -03:00
|
|
|
let x = if $1 {
|
|
|
|
$0
|
|
|
|
};
|
2021-01-15 15:49:59 +03:00
|
|
|
let y = 92;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"loop",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let x = $0
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2021-05-26 14:24:54 -03:00
|
|
|
let x = loop {
|
|
|
|
$0
|
|
|
|
};
|
2021-01-15 15:49:59 +03:00
|
|
|
bar();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|