From fc2d7d1e1f03faa4566dea8b60eca860bcf9eb04 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 1 Jan 2019 21:55:27 +0800 Subject: complete crate in use stmt --- .../ra_analysis/src/completion/complete_keyword.rs | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index d70fdaada..f51d77f27 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -18,6 +18,16 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte if !ctx.is_trivial_path { return; } + + // complete keyword "crate" in use stmt + if let (Some(use_item), None) = (&ctx.use_item_syntax, &ctx.path_prefix) { + if use_item.use_tree().is_none() { + CompletionItem::new(CompletionKind::Keyword, "crate") + .kind(CompletionItemKind::Keyword) + .add_to(acc); + } + } + let fn_def = match ctx.function_syntax { Some(it) => it, None => return, @@ -269,4 +279,23 @@ mod tests { "#, ) } + + fn completes_crate_in_use_stmt() { + check_keyword_completion( + r" + use <|> + ", + r#" + crate + "#, + ); + // No completion: lambda isolates control flow + check_keyword_completion( + r" + use a<|> + ", + r#" + "#, + ); + } } -- cgit v1.2.3 From f408b1caa3acdaf7b05498963e70da7f662bf2e3 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 1 Jan 2019 22:10:11 +0800 Subject: complete "use crate" for "use cr" too --- crates/ra_analysis/src/completion/complete_keyword.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index f51d77f27..a6fbd1e81 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -21,9 +21,20 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte // complete keyword "crate" in use stmt if let (Some(use_item), None) = (&ctx.use_item_syntax, &ctx.path_prefix) { - if use_item.use_tree().is_none() { + let mut complete_crate = false; + let use_tree = use_item.use_tree(); + if let Some(use_tree) = use_tree { + let tree_str = use_tree.syntax().text().to_string(); + if tree_str != "crate" && "crate".starts_with(&tree_str) { + complete_crate = true; + } + } else { + complete_crate = true; + } + if complete_crate { CompletionItem::new(CompletionKind::Keyword, "crate") .kind(CompletionItemKind::Keyword) + .lookup_by("crate") .add_to(acc); } } @@ -280,13 +291,14 @@ mod tests { ) } + #[test] fn completes_crate_in_use_stmt() { check_keyword_completion( r" use <|> ", r#" - crate + crate "crate" "#, ); // No completion: lambda isolates control flow -- cgit v1.2.3 From 22ea00d5ff2a993735702ec8e28fecfcbf4ffc4f Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 1 Jan 2019 22:45:53 +0800 Subject: complete "self" and "super" --- .../ra_analysis/src/completion/complete_keyword.rs | 64 +++++++++++++++------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index a6fbd1e81..dd81fc008 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -15,28 +15,38 @@ fn keyword(kw: &str, snippet: &str) -> CompletionItem { } pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path { - return; - } - // complete keyword "crate" in use stmt - if let (Some(use_item), None) = (&ctx.use_item_syntax, &ctx.path_prefix) { - let mut complete_crate = false; - let use_tree = use_item.use_tree(); - if let Some(use_tree) = use_tree { - let tree_str = use_tree.syntax().text().to_string(); - if tree_str != "crate" && "crate".starts_with(&tree_str) { - complete_crate = true; - } - } else { - complete_crate = true; - } - if complete_crate { + 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; } let fn_def = match ctx.function_syntax { @@ -292,21 +302,35 @@ mod tests { } #[test] - fn completes_crate_in_use_stmt() { + fn completes_keywords_in_use_stmt() { check_keyword_completion( r" use <|> ", r#" - crate "crate" + crate "crate" "crate::" + self "self" + super "super" "#, ); - // No completion: lambda isolates control flow + + check_keyword_completion( + r" + use a::<|> + ", + r#" + self "self" + super "super" + "#, + ); + check_keyword_completion( r" - use a<|> + use a::{b, <|>} ", r#" + self "self" + super "super" "#, ); } -- cgit v1.2.3 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') 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 From 39dab68404aa5649c45e6d428b8d5edb134ef44c Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 1 Jan 2019 23:12:51 +0800 Subject: place complete_use_tree_keyword in the same mod with complete_expr_keyword --- crates/ra_analysis/src/completion.rs | 3 +- .../ra_analysis/src/completion/complete_keyword.rs | 66 +++++++++++++++++++ .../src/completion/complete_use_tree.rs | 75 ---------------------- 3 files changed, 67 insertions(+), 77 deletions(-) delete mode 100644 crates/ra_analysis/src/completion/complete_use_tree.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index a795dd767..b4ee092b5 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -7,7 +7,6 @@ mod complete_keyword; mod complete_snippet; mod complete_path; mod complete_scope; -mod complete_use_tree; use ra_db::SyntaxDatabase; @@ -41,12 +40,12 @@ 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)?; 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 a381046e9..28194c908 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -7,6 +7,38 @@ use ra_syntax::{ 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) @@ -80,6 +112,40 @@ mod tests { 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( diff --git a/crates/ra_analysis/src/completion/complete_use_tree.rs b/crates/ra_analysis/src/completion/complete_use_tree.rs deleted file mode 100644 index 5f2f6e449..000000000 --- a/crates/ra_analysis/src/completion/complete_use_tree.rs +++ /dev/null @@ -1,75 +0,0 @@ -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