rust/crates/ra_ide/src/completion/complete_keyword.rs

783 lines
22 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-01-08 22:33:36 +03:00
use ra_syntax::{
ast::{self, LoopBodyOwner},
2019-10-05 17:03:03 +03:00
match_ast, AstNode,
SyntaxKind::*,
SyntaxToken,
2019-01-08 22:33:36 +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
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) => {
CompletionItem::new(CompletionKind::Keyword, source_range, "crate")
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
.insert_text("crate::")
2019-01-08 22:33:36 +03:00
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, source_range, "self")
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, source_range, "super")
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
.insert_text("super::")
2019-01-08 22:33:36 +03:00
.add_to(acc);
}
(Some(_), Some(_)) => {
CompletionItem::new(CompletionKind::Keyword, source_range, "self")
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, source_range, "super")
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
.insert_text("super::")
2019-01-08 22:33:36 +03:00
.add_to(acc);
}
_ => {}
}
}
fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem {
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
2019-01-08 22:33:36 +03:00
.kind(CompletionItemKind::Keyword)
2019-02-18 12:07:47 +03:00
.insert_snippet(snippet)
2019-01-08 22:33:36 +03:00
.build()
}
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path {
return;
}
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,
};
acc.add(keyword(ctx, "if", "if $0 {}"));
acc.add(keyword(ctx, "match", "match $0 {}"));
acc.add(keyword(ctx, "while", "while $0 {}"));
acc.add(keyword(ctx, "loop", "loop {$0}"));
2019-01-08 22:33:36 +03:00
if ctx.after_if {
acc.add(keyword(ctx, "else", "else {$0}"));
acc.add(keyword(ctx, "else if", "else if $0 {}"));
2019-01-08 22:33:36 +03:00
}
2019-07-19 12:56:47 +03:00
if is_in_loop_body(&ctx.token) {
2019-01-08 22:33:36 +03:00
if ctx.can_be_stmt {
acc.add(keyword(ctx, "continue", "continue;"));
acc.add(keyword(ctx, "break", "break;"));
2019-01-08 22:33:36 +03:00
} else {
acc.add(keyword(ctx, "continue", "continue"));
acc.add(keyword(ctx, "break", "break"));
2019-01-08 22:33:36 +03:00
}
}
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-07-19 12:56:47 +03:00
fn is_in_loop_body(leaf: &SyntaxToken) -> bool {
2020-03-07 16:50:30 +01:00
// FIXME move this to CompletionContext and make it handle macros
2019-03-30 13:25:53 +03:00
for node in leaf.parent().ancestors() {
2019-01-08 22:33:36 +03:00
if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR {
break;
}
2019-10-05 17:03:03 +03:00
let loop_body = match_ast! {
match node {
2020-04-06 17:21:33 +03:00
ast::ForExpr(it) => it.loop_body(),
ast::WhileExpr(it) => it.loop_body(),
ast::LoopExpr(it) => it.loop_body(),
2019-10-05 17:03:03 +03:00
_ => None,
}
};
if let Some(body) = loop_body {
2019-07-20 12:58:27 +03:00
if leaf.text_range().is_subrange(&body.syntax().text_range()) {
2019-01-08 22:33:36 +03:00
return true;
}
}
}
false
}
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",
};
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;
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!(
do_keyword_completion(
r"
use <|>
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "crate",
source_range: [21; 21),
delete: [21; 21),
insert: "crate::",
kind: Keyword,
},
CompletionItem {
label: "self",
source_range: [21; 21),
delete: [21; 21),
insert: "self",
kind: Keyword,
},
CompletionItem {
label: "super",
source_range: [21; 21),
delete: [21; 21),
insert: "super::",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03:00
);
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
do_keyword_completion(
r"
use a::<|>
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "self",
source_range: [24; 24),
delete: [24; 24),
insert: "self",
kind: Keyword,
},
CompletionItem {
label: "super",
source_range: [24; 24),
delete: [24; 24),
insert: "super::",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03:00
);
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
do_keyword_completion(
r"
use a::{b, <|>}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "self",
source_range: [28; 28),
delete: [28; 28),
insert: "self",
kind: Keyword,
},
CompletionItem {
label: "super",
source_range: [28; 28),
delete: [28; 28),
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!(
do_keyword_completion(
r"
fn quux() {
<|>
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [49; 49),
delete: [49; 49),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [49; 49),
delete: [49; 49),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [49; 49),
delete: [49; 49),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [49; 49),
delete: [49; 49),
insert: "return;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [49; 49),
delete: [49; 49),
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!(
do_keyword_completion(
r"
fn quux() {
if true {
()
} <|>
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "else",
source_range: [108; 108),
delete: [108; 108),
insert: "else {$0}",
kind: Keyword,
},
CompletionItem {
label: "else if",
source_range: [108; 108),
delete: [108; 108),
insert: "else if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "if",
source_range: [108; 108),
delete: [108; 108),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [108; 108),
delete: [108; 108),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [108; 108),
delete: [108; 108),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [108; 108),
delete: [108; 108),
insert: "return;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [108; 108),
delete: [108; 108),
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!(
do_keyword_completion(
r"
fn quux() -> i32 {
<|>
92
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [56; 56),
delete: [56; 56),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [56; 56),
delete: [56; 56),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [56; 56),
delete: [56; 56),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [56; 56),
delete: [56; 56),
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [56; 56),
delete: [56; 56),
insert: "while $0 {}",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03:00
);
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
do_keyword_completion(
r"
fn quux() {
<|>
92
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [49; 49),
delete: [49; 49),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [49; 49),
delete: [49; 49),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [49; 49),
delete: [49; 49),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [49; 49),
delete: [49; 49),
insert: "return;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [49; 49),
delete: [49; 49),
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!(
do_keyword_completion(
r"
fn quux() -> i32 {
match () {
() => <|>
}
2019-01-08 22:33:36 +03:00
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [97; 97),
delete: [97; 97),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [97; 97),
delete: [97; 97),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [97; 97),
delete: [97; 97),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [97; 97),
delete: [97; 97),
insert: "return $0",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [97; 97),
delete: [97; 97),
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!(
do_keyword_completion(
r"
fn quux() -> i32 {
if condition {
<|>
}
2019-01-08 22:33:36 +03:00
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [95; 95),
delete: [95; 95),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [95; 95),
delete: [95; 95),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [95; 95),
delete: [95; 95),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [95; 95),
delete: [95; 95),
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [95; 95),
delete: [95; 95),
insert: "while $0 {}",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03:00
);
2019-08-29 16:49:10 +03:00
assert_debug_snapshot!(
do_keyword_completion(
r"
fn quux() -> i32 {
if condition {
<|>
}
let x = 92;
x
2019-01-08 22:33:36 +03:00
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [95; 95),
delete: [95; 95),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [95; 95),
delete: [95; 95),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [95; 95),
delete: [95; 95),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [95; 95),
delete: [95; 95),
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [95; 95),
delete: [95; 95),
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!(
do_keyword_completion(
r"
fn quux() -> i32 {
loop { <|> }
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "break",
source_range: [63; 63),
delete: [63; 63),
insert: "break;",
kind: Keyword,
},
CompletionItem {
label: "continue",
source_range: [63; 63),
delete: [63; 63),
insert: "continue;",
kind: Keyword,
},
CompletionItem {
label: "if",
source_range: [63; 63),
delete: [63; 63),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [63; 63),
delete: [63; 63),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [63; 63),
delete: [63; 63),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [63; 63),
delete: [63; 63),
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [63; 63),
delete: [63; 63),
insert: "while $0 {}",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03: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!(
do_keyword_completion(
r"
fn quux() -> i32 {
loop { || { <|> } }
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "if",
source_range: [68; 68),
delete: [68; 68),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [68; 68),
delete: [68; 68),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [68; 68),
delete: [68; 68),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [68; 68),
delete: [68; 68),
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [68; 68),
delete: [68; 68),
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!(
do_keyword_completion(
r"
fn f() {
loop {
match () {
() => br<|>
}
2019-01-08 22:33:36 +03:00
}
}
",
),
2019-11-15 12:56:24 +03:00
@r###"
[
CompletionItem {
label: "break",
source_range: [122; 124),
delete: [122; 124),
insert: "break",
kind: Keyword,
},
CompletionItem {
label: "continue",
source_range: [122; 124),
delete: [122; 124),
insert: "continue",
kind: Keyword,
},
CompletionItem {
label: "if",
source_range: [122; 124),
delete: [122; 124),
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: [122; 124),
delete: [122; 124),
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: [122; 124),
delete: [122; 124),
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: [122; 124),
delete: [122; 124),
insert: "return",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: [122; 124),
delete: [122; 124),
insert: "while $0 {}",
kind: Keyword,
},
]
"###
2019-01-08 22:33:36 +03:00
)
}
}