2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-06-11 14:16:35 +02:00
|
|
|
use ra_syntax::ast;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-07-04 23:05:17 +03:00
|
|
|
use crate::completion::{
|
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
|
|
|
pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
|
|
|
|
// complete keyword "crate" in use stmt
|
2019-01-20 12:02:00 +08:00
|
|
|
let source_range = ctx.source_range();
|
2019-01-08 22:33:36 +03:00
|
|
|
match (ctx.use_item_syntax.as_ref(), ctx.path_prefix.as_ref()) {
|
|
|
|
(Some(_), None) => {
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Keyword, source_range, "crate")
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Keyword)
|
2019-01-19 22:02:50 +08:00
|
|
|
.insert_text("crate::")
|
2019-01-08 22:33:36 +03:00
|
|
|
.add_to(acc);
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Keyword, source_range, "self")
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Keyword)
|
|
|
|
.add_to(acc);
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Keyword, source_range, "super")
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Keyword)
|
2019-01-19 22:02:50 +08:00
|
|
|
.insert_text("super::")
|
2019-01-08 22:33:36 +03:00
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
(Some(_), Some(_)) => {
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Keyword, source_range, "self")
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Keyword)
|
|
|
|
.add_to(acc);
|
2019-01-20 12:02:00 +08:00
|
|
|
CompletionItem::new(CompletionKind::Keyword, source_range, "super")
|
2019-01-08 22:33:36 +03:00
|
|
|
.kind(CompletionItemKind::Keyword)
|
2019-01-19 22:02:50 +08:00
|
|
|
.insert_text("super::")
|
2019-01-08 22:33:36 +03:00
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem {
|
2020-04-24 02:26:38 +02:00
|
|
|
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()
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:16:35 +02:00
|
|
|
fn add_keyword(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
acc: &mut Completions,
|
|
|
|
kw: &str,
|
|
|
|
snippet: &str,
|
|
|
|
should_add: bool,
|
|
|
|
) {
|
|
|
|
if should_add {
|
|
|
|
acc.add(keyword(ctx, kw, snippet));
|
2020-06-02 01:29:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
|
2020-06-11 14:16:35 +02:00
|
|
|
add_keyword(ctx, acc, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
|
2020-06-11 23:25:58 +02:00
|
|
|
add_keyword(ctx, acc, "use", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
|
2020-06-11 14:16:35 +02:00
|
|
|
add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item);
|
|
|
|
add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item);
|
|
|
|
add_keyword(ctx, acc, "enum", "enum $0 {}", ctx.is_new_item && !ctx.after_unsafe);
|
|
|
|
add_keyword(ctx, acc, "struct", "struct $0 {}", ctx.is_new_item && !ctx.after_unsafe);
|
|
|
|
add_keyword(ctx, acc, "union", "union $0 {}", ctx.is_new_item && !ctx.after_unsafe);
|
|
|
|
add_keyword(ctx, acc, "match", "match $0 {}", ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "loop", "loop {$0}", ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "while", "while $0 {}", ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "let", "let ", ctx.after_if || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "else", "else {$0}", ctx.after_if);
|
|
|
|
add_keyword(ctx, acc, "else if", "else if $0 {}", ctx.after_if);
|
|
|
|
add_keyword(ctx, acc, "mod", "mod $0 {}", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent);
|
|
|
|
add_keyword(ctx, acc, "true", "true", !ctx.is_new_item); // this should be defined properly
|
|
|
|
add_keyword(ctx, acc, "false", "false", !ctx.is_new_item); // this should be defined properly
|
|
|
|
add_keyword(ctx, acc, "const", "const ", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "static", "static ", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "extern", "extern ", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "unsafe", "unsafe ", ctx.is_new_item || ctx.block_expr_parent);
|
|
|
|
add_keyword(ctx, acc, "continue", "continue;", ctx.in_loop_body && ctx.can_be_stmt);
|
|
|
|
add_keyword(ctx, acc, "break", "break;", ctx.in_loop_body && ctx.can_be_stmt);
|
|
|
|
add_keyword(ctx, acc, "continue", "continue", ctx.in_loop_body && !ctx.can_be_stmt);
|
|
|
|
add_keyword(ctx, acc, "break", "break", ctx.in_loop_body && !ctx.can_be_stmt);
|
2020-06-11 23:25:58 +02:00
|
|
|
add_keyword(ctx, acc, "pub", "pub ", ctx.is_new_item && !ctx.inside_trait);
|
|
|
|
add_keyword(ctx, acc, "where", "where ", ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling);
|
2020-06-11 14:16:35 +02:00
|
|
|
complete_use_tree_keyword(acc, ctx);
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-07-19 12:56:47 +03:00
|
|
|
let fn_def = match &ctx.function_syntax {
|
2019-01-08 22:33:36 +03:00
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2019-07-19 12:56:47 +03:00
|
|
|
acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt));
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:02:50 +08:00
|
|
|
fn complete_return(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
fn_def: &ast::FnDef,
|
|
|
|
can_be_stmt: bool,
|
|
|
|
) -> Option<CompletionItem> {
|
2019-01-08 22:33:36 +03:00
|
|
|
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",
|
|
|
|
};
|
2019-01-19 22:02:50 +08:00
|
|
|
Some(keyword(ctx, "return", snip))
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-03-11 10:46:43 +01:00
|
|
|
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
2019-08-29 16:49:10 +03:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2019-06-04 21:26:12 +01:00
|
|
|
fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Keyword)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_keywords_in_use_stmt() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
use <|>
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "crate",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 21..21,
|
|
|
|
delete: 21..21,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "crate::",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "self",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 21..21,
|
|
|
|
delete: 21..21,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "self",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "super",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 21..21,
|
|
|
|
delete: 21..21,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "super::",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
use a::<|>
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "self",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 24..24,
|
|
|
|
delete: 24..24,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "self",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "super",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 24..24,
|
|
|
|
delete: 24..24,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "super::",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
use a::{b, <|>}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "self",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 28..28,
|
|
|
|
delete: 28..28,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "self",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "super",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 28..28,
|
|
|
|
delete: 28..28,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "super::",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_various_keywords_in_function() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_else_after_if() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() {
|
|
|
|
if true {
|
|
|
|
()
|
|
|
|
} <|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "else",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "else {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "else if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "else if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 108..108,
|
|
|
|
delete: 108..108,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_return_value() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
<|>
|
|
|
|
92
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 56..56,
|
|
|
|
delete: 56..56,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 56..56,
|
|
|
|
delete: 56..56,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 56..56,
|
|
|
|
delete: 56..56,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 56..56,
|
|
|
|
delete: 56..56,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 56..56,
|
|
|
|
delete: 56..56,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() {
|
|
|
|
<|>
|
|
|
|
92
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 49..49,
|
|
|
|
delete: 49..49,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_add_semi_after_return_if_not_a_statement() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
match () {
|
|
|
|
() => <|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-06-04 21:26:12 +01:00
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 97..97,
|
|
|
|
delete: 97..97,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 97..97,
|
|
|
|
delete: 97..97,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 97..97,
|
|
|
|
delete: 97..97,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 97..97,
|
|
|
|
delete: 97..97,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 97..97,
|
|
|
|
delete: 97..97,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_return_in_block_has_semi() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
if condition {
|
|
|
|
<|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-06-04 21:26:12 +01:00
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
if condition {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
let x = 92;
|
|
|
|
x
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-06-04 21:26:12 +01:00
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 95..95,
|
|
|
|
delete: 95..95,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_break_and_continue_in_loops() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
loop { <|> }
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "break",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "break;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "continue",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "continue;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 63..63,
|
|
|
|
delete: 63..63,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
2019-01-19 22:02:50 +08:00
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
// No completion: lambda isolates control flow
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn quux() -> i32 {
|
|
|
|
loop { || { <|> } }
|
|
|
|
}
|
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 68..68,
|
|
|
|
delete: 68..68,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 68..68,
|
|
|
|
delete: 68..68,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 68..68,
|
|
|
|
delete: 68..68,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 68..68,
|
|
|
|
delete: 68..68,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return $0;",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 68..68,
|
|
|
|
delete: 68..68,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_semi_after_break_continue_in_expr() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-06-04 21:26:12 +01:00
|
|
|
do_keyword_completion(
|
|
|
|
r"
|
|
|
|
fn f() {
|
|
|
|
loop {
|
|
|
|
match () {
|
|
|
|
() => br<|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-04 21:26:12 +01:00
|
|
|
",
|
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "break",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "break",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "continue",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "continue",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "if $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "loop",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "loop {$0}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "match $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "return",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "return",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2020-04-24 23:51:02 +02:00
|
|
|
source_range: 122..124,
|
|
|
|
delete: 122..124,
|
2019-11-15 12:56:24 +03:00
|
|
|
insert: "while $0 {}",
|
|
|
|
kind: Keyword,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-08 22:33:36 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|