From 22ea00d5ff2a993735702ec8e28fecfcbf4ffc4f Mon Sep 17 00:00:00 2001
From: gfreezy <gfreezy@gmail.com>
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/ra_analysis/src')

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