]> git.lizzy.rs Git - rust.git/commitdiff
Complete inherent methods
authorFlorian Diebold <flodiebold@gmail.com>
Mon, 7 Jan 2019 18:12:19 +0000 (19:12 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Sat, 12 Jan 2019 14:01:27 +0000 (15:01 +0100)
crates/ra_ide_api/src/completion/complete_dot.rs
crates/ra_ide_api/src/completion/completion_item.rs
crates/ra_lsp_server/src/conv.rs

index 80d0b166396e1d28f37f9b83d9c871ef892dc8f0..9b01eb0aba1f1f05c00466a895067def3399ebc8 100644 (file)
@@ -17,8 +17,9 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca
     };
     let receiver_ty = infer_result[expr].clone();
     if !ctx.is_call {
-        complete_fields(acc, ctx, receiver_ty)?;
+        complete_fields(acc, ctx, receiver_ty.clone())?;
     }
+    complete_methods(acc, ctx, receiver_ty)?;
     Ok(())
 }
 
@@ -55,6 +56,24 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
     Ok(())
 }
 
+fn complete_methods(
+    acc: &mut Completions,
+    ctx: &CompletionContext,
+    receiver: Ty,
+) -> Cancelable<()> {
+    receiver.iterate_methods(ctx.db, |func| {
+        let sig = func.signature(ctx.db);
+        if sig.has_self_arg() {
+            CompletionItem::new(CompletionKind::Reference, sig.name().to_string())
+                .from_function(ctx, func)
+                .kind(CompletionItemKind::Method)
+                .add_to(acc);
+        }
+        Ok(None::<()>)
+    })?;
+    Ok(())
+}
+
 #[cfg(test)]
 mod tests {
     use crate::completion::*;
@@ -87,7 +106,8 @@ fn foo(self) {
                 }
             }
             ",
-            r#"the_field "(u32,)""#,
+            r#"the_field "(u32,)"
+               foo "foo($0)""#,
         );
     }
 
@@ -102,7 +122,8 @@ fn foo(&self) {
                 }
             }
             ",
-            r#"the_field "(u32, i32)""#,
+            r#"the_field "(u32, i32)"
+               foo "foo($0)""#,
         );
     }
 
@@ -118,4 +139,36 @@ fn foo(a: A) {
             r#""#,
         );
     }
+
+    #[test]
+    fn test_method_completion() {
+        check_ref_completion(
+            r"
+            struct A {}
+            impl A {
+                fn the_method(&self) {}
+            }
+            fn foo(a: A) {
+               a.<|>
+            }
+            ",
+            r#"the_method "the_method($0)""#,
+        );
+    }
+
+    #[test]
+    fn test_no_non_self_method() {
+        check_ref_completion(
+            r"
+            struct A {}
+            impl A {
+                fn the_method() {}
+            }
+            fn foo(a: A) {
+               a.<|>
+            }
+            ",
+            r#""#,
+        );
+    }
 }
index e7fa967a03df2688f0ee204910a2c3240328c8a2..9ce7784875f44a2010b726ccfbb272a867188448 100644 (file)
@@ -37,6 +37,7 @@ pub enum CompletionItemKind {
     Const,
     Trait,
     TypeAlias,
+    Method,
 }
 
 #[derive(Debug, PartialEq, Eq)]
@@ -183,7 +184,11 @@ pub(super) fn from_resolution(
         self
     }
 
-    fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder {
+    pub(super) fn from_function(
+        mut self,
+        ctx: &CompletionContext,
+        function: hir::Function,
+    ) -> Builder {
         // If not an import, add parenthesis automatically.
         if ctx.use_item_syntax.is_none() && !ctx.is_call {
             if function.signature(ctx.db).args().is_empty() {
index 7ca2f437dd98ae3619abc5dd89b93bac3d11ce33..22b8e9221bf225a4e561d5a51f229ea9747d2c34 100644 (file)
@@ -69,6 +69,7 @@ fn conv(self) -> <Self as Conv>::Output {
             CompletionItemKind::TypeAlias => Struct,
             CompletionItemKind::Const => Constant,
             CompletionItemKind::Static => Value,
+            CompletionItemKind::Method => Method,
         }
     }
 }