401: complete `crate` `self` and `super` in use stmt r=matklad a=gfreezy

fixed #387

Co-authored-by: gfreezy <gfreezy@gmail.com>
This commit is contained in:
bors[bot] 2019-01-01 15:18:32 +00:00
commit e4ffd7b317
2 changed files with 68 additions and 0 deletions

View File

@ -40,6 +40,7 @@ pub(crate) fn completions(
complete_fn_param::complete_fn_param(&mut acc, &ctx);
complete_keyword::complete_expr_keyword(&mut acc, &ctx);
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
complete_snippet::complete_expr_snippet(&mut acc, &ctx);
complete_snippet::complete_item_snippet(&mut acc, &ctx);
complete_path::complete_path(&mut acc, &ctx)?;

View File

@ -7,6 +7,38 @@
use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind};
pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
// complete keyword "crate" in use stmt
match (ctx.use_item_syntax.as_ref(), ctx.path_prefix.as_ref()) {
(Some(_), None) => {
CompletionItem::new(CompletionKind::Keyword, "crate")
.kind(CompletionItemKind::Keyword)
.lookup_by("crate")
.snippet("crate::")
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, "self")
.kind(CompletionItemKind::Keyword)
.lookup_by("self")
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, "super")
.kind(CompletionItemKind::Keyword)
.lookup_by("super")
.add_to(acc);
}
(Some(_), Some(_)) => {
CompletionItem::new(CompletionKind::Keyword, "self")
.kind(CompletionItemKind::Keyword)
.lookup_by("self")
.add_to(acc);
CompletionItem::new(CompletionKind::Keyword, "super")
.kind(CompletionItemKind::Keyword)
.lookup_by("super")
.add_to(acc);
}
_ => {}
}
}
fn keyword(kw: &str, snippet: &str) -> CompletionItem {
CompletionItem::new(CompletionKind::Keyword, kw)
.kind(CompletionItemKind::Keyword)
@ -18,6 +50,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
if !ctx.is_trivial_path {
return;
}
let fn_def = match ctx.function_syntax {
Some(it) => it,
None => return,
@ -79,6 +112,40 @@ fn check_keyword_completion(code: &str, expected_completions: &str) {
check_completion(code, expected_completions, CompletionKind::Keyword);
}
#[test]
fn completes_keywords_in_use_stmt() {
check_keyword_completion(
r"
use <|>
",
r#"
crate "crate" "crate::"
self "self"
super "super"
"#,
);
check_keyword_completion(
r"
use a::<|>
",
r#"
self "self"
super "super"
"#,
);
check_keyword_completion(
r"
use a::{b, <|>}
",
r#"
self "self"
super "super"
"#,
);
}
#[test]
fn completes_various_keywords_in_function() {
check_keyword_completion(