aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs59
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs7
-rw-r--r--crates/ra_lsp_server/src/conv.rs1
3 files changed, 63 insertions, 4 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 80d0b1663..9b01eb0ab 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -17,8 +17,9 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca
17 }; 17 };
18 let receiver_ty = infer_result[expr].clone(); 18 let receiver_ty = infer_result[expr].clone();
19 if !ctx.is_call { 19 if !ctx.is_call {
20 complete_fields(acc, ctx, receiver_ty)?; 20 complete_fields(acc, ctx, receiver_ty.clone())?;
21 } 21 }
22 complete_methods(acc, ctx, receiver_ty)?;
22 Ok(()) 23 Ok(())
23} 24}
24 25
@@ -55,6 +56,24 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
55 Ok(()) 56 Ok(())
56} 57}
57 58
59fn complete_methods(
60 acc: &mut Completions,
61 ctx: &CompletionContext,
62 receiver: Ty,
63) -> Cancelable<()> {
64 receiver.iterate_methods(ctx.db, |func| {
65 let sig = func.signature(ctx.db);
66 if sig.has_self_arg() {
67 CompletionItem::new(CompletionKind::Reference, sig.name().to_string())
68 .from_function(ctx, func)
69 .kind(CompletionItemKind::Method)
70 .add_to(acc);
71 }
72 Ok(None::<()>)
73 })?;
74 Ok(())
75}
76
58#[cfg(test)] 77#[cfg(test)]
59mod tests { 78mod tests {
60 use crate::completion::*; 79 use crate::completion::*;
@@ -87,7 +106,8 @@ mod tests {
87 } 106 }
88 } 107 }
89 ", 108 ",
90 r#"the_field "(u32,)""#, 109 r#"the_field "(u32,)"
110 foo "foo($0)""#,
91 ); 111 );
92 } 112 }
93 113
@@ -102,7 +122,8 @@ mod tests {
102 } 122 }
103 } 123 }
104 ", 124 ",
105 r#"the_field "(u32, i32)""#, 125 r#"the_field "(u32, i32)"
126 foo "foo($0)""#,
106 ); 127 );
107 } 128 }
108 129
@@ -118,4 +139,36 @@ mod tests {
118 r#""#, 139 r#""#,
119 ); 140 );
120 } 141 }
142
143 #[test]
144 fn test_method_completion() {
145 check_ref_completion(
146 r"
147 struct A {}
148 impl A {
149 fn the_method(&self) {}
150 }
151 fn foo(a: A) {
152 a.<|>
153 }
154 ",
155 r#"the_method "the_method($0)""#,
156 );
157 }
158
159 #[test]
160 fn test_no_non_self_method() {
161 check_ref_completion(
162 r"
163 struct A {}
164 impl A {
165 fn the_method() {}
166 }
167 fn foo(a: A) {
168 a.<|>
169 }
170 ",
171 r#""#,
172 );
173 }
121} 174}
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index e7fa967a0..9ce778487 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -37,6 +37,7 @@ pub enum CompletionItemKind {
37 Const, 37 Const,
38 Trait, 38 Trait,
39 TypeAlias, 39 TypeAlias,
40 Method,
40} 41}
41 42
42#[derive(Debug, PartialEq, Eq)] 43#[derive(Debug, PartialEq, Eq)]
@@ -183,7 +184,11 @@ impl Builder {
183 self 184 self
184 } 185 }
185 186
186 fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder { 187 pub(super) fn from_function(
188 mut self,
189 ctx: &CompletionContext,
190 function: hir::Function,
191 ) -> Builder {
187 // If not an import, add parenthesis automatically. 192 // If not an import, add parenthesis automatically.
188 if ctx.use_item_syntax.is_none() && !ctx.is_call { 193 if ctx.use_item_syntax.is_none() && !ctx.is_call {
189 if function.signature(ctx.db).args().is_empty() { 194 if function.signature(ctx.db).args().is_empty() {
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 7ca2f437d..22b8e9221 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -69,6 +69,7 @@ impl Conv for CompletionItemKind {
69 CompletionItemKind::TypeAlias => Struct, 69 CompletionItemKind::TypeAlias => Struct,
70 CompletionItemKind::Const => Constant, 70 CompletionItemKind::Const => Constant,
71 CompletionItemKind::Static => Value, 71 CompletionItemKind::Static => Value,
72 CompletionItemKind::Method => Method,
72 } 73 }
73 } 74 }
74} 75}