From 9895529d5c7223e572f04ec424d94a187b4983a1 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 1 Jan 2019 23:00:29 +0800 Subject: move to a seperate complete_use_tree_keyword mod --- crates/ra_analysis/src/completion.rs | 2 + .../ra_analysis/src/completion/complete_keyword.rs | 64 ------------------ .../src/completion/complete_use_tree.rs | 75 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 64 deletions(-) create mode 100644 crates/ra_analysis/src/completion/complete_use_tree.rs (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index fe580700f..a795dd767 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -7,6 +7,7 @@ mod complete_keyword; mod complete_snippet; mod complete_path; mod complete_scope; +mod complete_use_tree; use ra_db::SyntaxDatabase; @@ -45,6 +46,7 @@ pub(crate) fn completions( complete_path::complete_path(&mut acc, &ctx)?; complete_scope::complete_scope(&mut acc, &ctx)?; complete_dot::complete_dot(&mut acc, &ctx)?; + complete_use_tree::complete_use_tree_keyword(&mut acc, &ctx); Ok(Some(acc)) } diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index dd81fc008..a381046e9 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -15,36 +15,6 @@ fn keyword(kw: &str, snippet: &str) -> CompletionItem { } pub(super) fn complete_expr_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); - } - _ => {} - } - if !ctx.is_trivial_path { return; } @@ -300,38 +270,4 @@ mod tests { "#, ) } - - #[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" - "#, - ); - } } diff --git a/crates/ra_analysis/src/completion/complete_use_tree.rs b/crates/ra_analysis/src/completion/complete_use_tree.rs new file mode 100644 index 000000000..5f2f6e449 --- /dev/null +++ b/crates/ra_analysis/src/completion/complete_use_tree.rs @@ -0,0 +1,75 @@ +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); + } + _ => {} + } +} + +#[cfg(test)] +mod tests { + use crate::completion::{CompletionKind, check_completion}; + 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" + "#, + ); + } +} -- cgit v1.2.3